我是靠谱客的博主 复杂毛豆,最近开发中收集的这篇文章主要介绍部署LNMP报错怎么办?别担心,教你想报错都难的LNMP脚本目录结构图创建脚本验证总结,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录结构图

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

注意:使用脚本第一步,看看IP地址一定要改成自己的主机。其他文件有没有发生变化。不同版本,配置文件不一样,仅供特定版本适用。
在这里插入图片描述

创建被管理主机列表文件

注意:第二次推送,会提示找不到文件,因为原脚本,将本地主机公钥重命名了。
在这里插入图片描述
本地主机也要连互联网,配置yum源


创建脚本

第一种:不带选择菜单
思路:所有的变量都在主脚本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 "yesn";exp_continue }
        "password" { send "$passwdn" }
        }
        expect eof

EOF
        done < /root/hosts   
}

vim /lnmp/include/init_os.sh

echo " = = = = = = = = = = = = = = = = 所有节点拷贝文件 = = = = = = = = = = = = = = = = "
while read line
do
	nip1=`echo $line | cut -d " " -f 1`
	DIR=/lnmp/src
	#cd $DIR  
	scp -rp $DIR  $nip1:/root

done	< /root/hosts
echo " = = = = = = = = = = = = = = = = 完成 = = = = = = = = = = = = = = = = "

#!/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 = = = = = = = = = = = = = = = = "
for i in `cat /root/nginx_hosts | cut -d " " -f 1`
do

scp -rp /lnmt/src  root@$i:/root
ssh $i << EOF
yum -y localinstall /root/src/nginx-rpm/*
EOF

done

echo " = = = = = = = = = = = = = nginx安装成功 = = = = = = = = = = = = = ="
}

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

#!/usr/bin/env bash
#install mysql
mysql() {
echo " = = = = = = = = = = = = = = = = 安装mysql= = = = = = = = = = = = = = = = "
for i in `cat /root/mysql_hosts | cut -d " " -f 1`
do

scp -rp /lnmp/src  root@$i:/root
ssh $i << EOF
rm -rf /var/run/yum.pid
yum -y localinstall /root/src/mysql5.6-rpm/*
systemctl start mysqld
#设置数据库密码及创建数据库,授权
mysqladmin password '123.com' 
mysql -uroot -p123.com -e "create database wordpress;"
mysql -uroot -p123.com -e "grant all on wordpress.* to lisi@'%' identified by '123456';"
EOF

done

echo " = = = = = = = = = = = = = 安装mysql成功 = = = = = = = = = = = = = ="
}
mysql

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
#配置Nginx
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
systemctl start nginx
else
	echo " = = = = = = = = = = = = = nginx 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目录的所有文件授予执行权
在这里插入图片描述
第二种:带菜单
适用场景,网络不佳情况,提前下载好安装包;放在SOFT_DIR=/root/lnmp/;目录设成变量方便灵活更改

#引用函数
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#菜单
cat<<EOF
#########################
0. Check Network
1. Install Nginx      
2. Install PHP       
3. Install Mysql
4. Deploy  WordPress
5. Exit           
#########################
EOF
#定义变量
SOFT_DIR=/root/lnmp/
#定义函数
Start() {
    read -p "是否启动[yes|no]: " Confirm
    case $Confirm in
        y|Y|yes|Yes)
            systemctl restart $Service &>/dev/null && sleep 2
            if [ $? -eq 0 ];then
                action  "${Service}服务启动成功!" /bin/true
            else
                action "${Service}服务启动失败!" /bin/false
            fi
            ;;
        n|N|no|No)
            echo "你可以手动启动${Service}服务!"
            ;;
        *)
            echo "你输入的不符合要求!请重新输入!"
    esac
}
Whether_Install() {
    #判断是否已安装
    systemctl status ${Service} &>/dev/null
    local Return=$?
    if [ $Return -eq 0 ];then
        action "${Service}服务已经安装!并正在运行中....." /bin/true
    elif [ $Return -eq 3 ];then
        action "${Service}服务已经安装!但是没有启动!" /bin/false
        Start
    else
        echo "系统没有安装${Service}服务!可以正常下载安装!"
        return 66
    fi
}
Install_Nginx() {
    #判断Nginx是否已安装
    local Service=nginx
    Whether_Install
    if [ $? -eq 66 ];then
	rm -rf /var/run/yum.pid 
        cd $SOFT_DIR/nginx-rpm && yum localinstall -y *.rpm
        if [ $? -eq 0 ];then
            echo "${Service}服务下载安装成功!"
            Start
        fi
    fi
}
Install_PHP() {
    #判断PHP是否已安装
    local Service=php-fpm
    Whether_Install
    if [ $? -eq 66 ];then
        cd $SOFT_DIR/php-rpm && yum localinstall -y *.rpm
        if [ $? -eq 0 ];then
            echo "${Service}服务下载安装成功!"
            Start
        fi
    fi
}   
Install_Mysql() {
    #判断Mysql是否已安装
    local Service=mysql
    Whether_Install
    if [ $? -eq 66 ];then
	rm -rf /var/run/yum.pid
        cd $SOFT_DIR/mysql5.6-rpm && yum localinstall -y *.rpm 
        if [ $? -eq 0 ];then
            echo "${Service}服务下载安装成功!"
            Start
            #设置数据库密码及创建数据库
            mysqladmin password '123.com' &>/dev/null && mysql -uroot -p123.com -e "create database wordpress;"
            if [ $? -eq 0 ];then
                echo "数据库密码及库已经创建成功!"
            else
                echo "数据库密码及库已经创建失败!"
            fi
        fi
    fi
}
Deploy_WordPress() {
    	#条件检测WordPress
        #解压到站点目录
        cd $SOFT_DIR/ && unzip wordpress-4.9.4-zh_CN.zip  
		chmod -R 777 $SOFT_DIR/wordpress
        mv  -t /   $SOFT_DIR/wordpress
    if [ $? -eq 0 ];then
        echo "WordPress安装成功!"

    else
        echo "WordPress安装失败!"
    fi
}
while true
do
    read -p "请输入你要安装的服务:" Num
    case $Num in
        0)
            echo "开始测试网络情况!"
            ping -c1 -W1 www.baidu.com &>/dev/null
            if [ $? -eq 0 ];then
                action "网络状况良好!可以正常下载安装!" /bin/true
            else
                action "网络出现异常情况!无法正常下载安装!" /bin/false
            fi
            continue
            ;;
        1)
            echo "下面开始安装部署Nginx服务!"
            Install_Nginx
            ;;
        2)
            echo "下面开始安装部署PHP服务!"
            Install_PHP
            ;;
        3)
            echo "下面开始安装部署Mysql服务!"
            Install_Mysql
            ;;
        4)
            echo "下面开始安装部署WordPress!"
            Deploy_WordPress
            ;;
        5)
            echo "脚本程序退出!"
            exit
            ;;
        *)
            echo "你输入的不符合要求!请重新输入!"
            continue
    esac
done

验证

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

阶段验证:
1、 测试初始化系统脚本(创建临时主脚本,只调用执行一个测试函数)
在这里插入图片描述
只执行加载某一个模块即可
执行结果:
在这里插入图片描述
会有正常提示信息没有允许分配一个终端,因为,我们是做的免密远程登录,没用终端,忽略
在这里插入图片描述
在远程主机验证:成功,脚本就是让将拷贝到远程主机root宿主目录的文件,移动到yum文件路径
在这里插入图片描述
完全验证:
2、 整体模块加到一起,和女朋友聊会天,回来测试即可!

报错:验证PHP网页打开错误提示,Nginx却可以。

解决:定位错误,脚本里的Nginx.conf配置文件,添加PHP支持项,填写的php主机IP地址更改,要改成现有的。
在这里插入图片描述


总结

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

最后

以上就是复杂毛豆为你收集整理的部署LNMP报错怎么办?别担心,教你想报错都难的LNMP脚本目录结构图创建脚本验证总结的全部内容,希望文章能够帮你解决部署LNMP报错怎么办?别担心,教你想报错都难的LNMP脚本目录结构图创建脚本验证总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部