部署LNMP报错怎么办?别担心,教你想报错都难的LNMP脚本

Posted Friends of the wind

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了部署LNMP报错怎么办?别担心,教你想报错都难的LNMP脚本相关的知识,希望对你有一定的参考价值。

目录结构图


创建项目目录
提示:将需要的不同类型的文件归到不同分类,需要哪个调用那个,一旦报错,只需到对应的部分脚本去找,后期,需要扩展或更改也方便。


创建脚本

思路:所有的变量都在主脚本lnmp.sh定义,将各个部分的脚本定义成函数,载入主脚本,调用即可;
所需文件放在/lnmp目录下相应位置,一定不能乱,本地主机也要先配置好yum仓库,手工在/root/hosts下创建远程主机列表。

1、 创建免密交互、初始化系统脚本
vim /lnmp/include/secret_free.sh

#!/usr/bin/env bash
#ssh secret free

secret_free() {
echo " = = = = = = = = = = = = = = = = 免密交互 = = = = = = = = = = = = = = = = "
yum -y install expect  

if [ ! -f /root/.ssh/id_rsa ];then   
	ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa  
	mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys  
	 echo "id_rsa创建成功"
else
	mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
 	echo "id_rsa已存在"
fi

while read line
 do
   user=`echo $line | cut -d " " -f 2`    
   ip=`echo $line | cut -d " " -f 1`
   passwd=`echo $line | cut -d " " -f 3`
expect  <<EOF
        set     timeout 10
        spawn   scp     -r /root/.ssh $ip:/root
        expect  {
        "yes/no" { send "yes\\n";exp_continue }
        "password" { send "$passwd\\n" }
        }
        expect eof

EOF
        done < /root/hosts   
}

vim /lnmp/include/init_os.sh

#!/usr/bin/env bash
#init os

init_os() {
#YUM
	cd  /lnmp/src
	scp -rp /lnmp/src  root@${nip1}:/root
	cd /lnmp/conf
        scp -rp /lnmp/conf  root@${nip1}:/root
	ssh $nip1 << E
if	[ -d /etc/yum.repos.d/bak/ ] || mkdir /etc/yum.repos.d/bak/
then
	cd  /etc/yum.repos.d/
	mv /etc/yum.repos.d/*.repo  /etc/yum.repos.d/bak
fi
	rm -f /var/run/yum.pid
	cd /root/conf/
	mv /root/conf/yum.repo /etc/yum.repos.d/

#SELinux
	systemctl stop firewalld
	setenforce 0
E
}

2、 创建安装nginx脚本
vim /lnmp/include/nginx.sh

#!/usr/bin/env bash
#install nginx

nginx() {
echo " = = = = = = = = = = = = = = = = 安装nginx = = = = = = = = = = = = = = = = "
while read line
do	
	ssh $nip1 << E
	rm -f /var/run/yum.pid
	cd /root/src/nginx-rpm/
	yum -y localinstall *.rpm
E
done < /root/hosts
	cd /root/conf/
	mv /root/conf/nginx.conf  /etc/nginx/conf.d
	mv /root/conf/index.php  /usr/share/nginx/html/
	cd /etc/nginx/conf.d/
	mv default.conf default.conf.bak
echo " = = = = = = = = = = = = = nginx安装成功 = = = = = = = = = = = = = ="
}

3、 创建安装mysql脚本
vim /lnmp/include/mysql.sh

#!/usr/bin/env bash
#install mysql
mysql() {
ssh $nip1 << E
cd /root/src/
cd mysql5.6-rpm/
yum -y localinstall *.rpm
systemctl start mysqld
if [ $? -eq 0 ];then
	echo " = = = = = = = = = = = = = mysql启动成功 = = = = = = = = = = = = = "
	mysql -e "create database blog;"
	mysql -e "grant all on blog.* to lisi@'%' identified by '123456';"
	else
	echo " = = = = = = = = = = = = = mysql启动失败 = = = = = = = = = = = = = "
fi
E
}

4、 创建安装php脚本
vim /lnmp/include/php.sh

#!/usr/bin/env bash
#install php

php() {
ssh $nip1 << E
cd /root/src/
cd /root/src/php-rpm/
yum -y localinstall *.rpm
systemctl start php-fpm
if [ $? -eq 0 ];then
	echo " = = = = = = = = = = = = = php启动成功 = = = = = = = = = = = = = "
	sed -i "s/listen = 127.0.0.1:9000/listen = "$nip1":9000/" /etc/php-fpm.d/www.conf
	sed -i "s@listen.allowed_clients = 127.0.0.1@listen.allowed_clients = "$nip1"@" /etc/php-fpm.d/www.conf
systemctl start nginx
else
	echo " = = = = = = = = = = = = = php启动失败 = = = = = = = = = = = = = "
fi
E
}

5、 创建安装lnmp主脚本
vim /lnmp/lnmp.sh

#!/usr/bin/env bash
#lnmp install
nip1=`cat /root/hosts  | cut -d " " -f 1`

. include/secret_free.sh
. include/init_os.sh
. include/nginx.sh
. include/mysql.sh
. include/php.sh

secret_free
init_os
nginx
mysql
php

为include目录的所有文件授予执行权


验证

提示:为防止报错,可以先逐个测试,没问题再整体,执行。

阶段验证:
1、 测试初始化系统脚本(创建临时主脚本,只调用执行一个测试函数)

只执行加载某一个模块即可
执行结果:

会有正常提示信息没有允许分配一个终端,因为,我们是做的免密远程登录,没用终端,忽略

在远程主机验证:成功,脚本就是让将拷贝到远程主机root宿主目录的文件,移动到yum文件路径

完全验证:
2、 整体模块加到一起,配女朋友聊会天,回来测试即可!


总结

提示:此方法适合复杂的功能的场景需求,优势是扩展性强,结构清晰,不易出错,即便出错,利于排查,还可以进行优化,定义变量,软件版本变化,或者精细化,安装目录可以定义成变量;如果增加lvs负载均衡和高可用,都很轻松的添加脚本,稍微修改即可!

以上是关于部署LNMP报错怎么办?别担心,教你想报错都难的LNMP脚本的主要内容,如果未能解决你的问题,请参考以下文章

部署LNMP架构时安装MySQL的报错(--initialize specified but the data directory has files in it. Aborting.)!

日常部署服务遇到的问题及报错 - 不定期更新

初学编程:遇到程序报错问题怎么解决?教你正确操作!

小白第一次安装黑苹果,然后卡代码,别着急,教你一个通用步骤,一个一个排查,大部分是可以解决的,毕竟安装不是最难的一个步骤

老司机教你使用shel脚本15分钟部署LNMP

手把手教你LNMP和Zabbix部署搭建