目标一:
提示:为什么要用它? LNMP是一套由Linux + Nginx + MySQL + PHP组成的动态网站系统解决方案,具有免费、高效、扩展性强且资源消耗低等优良特性。
目标二:
提示:LNMP是什么? L:表示Linux系统,提高操作系统 N:表示Nginx服务,提供web网站服务 M:表示Mysql数据库,为平台架构提供数据存储 P:表示PHP、python、perl等等编程语言,是在服务器端执行的脚本语言
目标三:
提示:如何搭建LNMP
1、 手工搭建(此方法,适合新手,不常用,省略) 2、 脚本自动搭建(可以有好多种形式的脚本,给大家介绍一个相对复杂,但功能更完善的脚本)
分析:LNMP是这个架构的基础,保障能更好的运行,加入keepalived,实现高可用。比如:一台Nginx服务器故障,keepalived可以自动切换到另一台Nginx服务器,保障业务不中断;expect是实现免交互的一个服务,调用expect做免密登录,同时部署多台服务器,WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站;当然,好比造一辆高端的车,比造一台普通的车,难度高,我的另一个教程没这两个服务,部署相对简单,需要可看玩转Linux系统之shell脚本轻松搭建LNMP平台架构 脚本及注释如下图:
#!/bin/bash
echo "关闭防火墙,关闭SELinux,开启ens33"
systemctl stop firewalld && setenforce 0 && ifup ens33
echo " = = = = = = = = = = = = = = = = 免密交互 = = = = = = = = = = = = = = = = "
cat > /root/ip.txt << E
192.168.2.2 root 123456
192.168.2.3 root 123456
E
echo " = = = = = = = = = = = = = = = = expect正在安装 = = = = = = = = = = = = = = = = "
rm -rf /var/run/yum.pid && yum -y install expect &> /dev/null //删除yum的进程id文件,并安装expect
echo " = = = = = = = = = = = = = = = = expect已安装 = = = = = = = = = = = = = = = = "
if [ ! -f /root/.ssh/id_rsa ];then //如果id_rsa文件不存在,则生成文件
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys //重命名为authorized_keys
echo "id_rsa已创建成功"
else
mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
echo "id_rsa已存在"
fi
while read line //依次读取ip.txt文件的值赋给变量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 "password" { send "$passwd\n" }
EOF
done < ip.txt
echo " = = = = = = = = = = = = = = = = 所有节点正在安装nginx = = = = = = = = = = = = = = = = "
while read -r line
do
nip1=`echo $line | cut -d " " -f 1`
scp -rp /root/qrl/nginx-rpm root@${nip1}:/root
ssh $nip1 << hhh
rm -rf /var/run/yum.pid
cd nginx-rpm/
yum -y localinstall *.rpm
hhh
done < ip.txt
echo " = = = = = = = = = = = = = = = = 所有配置web节点 = = = = = = = = = = = = = = = = "
cat ip.txt | cut -d " " -f 1 |awk ' NR==1 { print $1 }' >> aa.txt //截取ip.txt文件,以空格分隔第一列,第一行,重定向到aa.txt文件
cat ip.txt | cut -d " " -f 1 |awk ' NR==2 { print $1 }' >> aa.txt
ccip=`cat ip.txt | cut -d " " -f 1 |awk ' NR==1 { print $1 }'` //定义变量ccip,并赋值为
hostip=`ifconfig ens33 | awk ' NR==2 { print $2 }'` //定义变量ccip,并赋值为查看网卡信息,过滤出的第二行
while read -r line
do
nip2=`echo $line | cut -d " " -f 1`
scp -rp /root/qrl/wordpress-4.9.4-zh_CN.zip root@$nip2:/
ssh $nip2 << nnn
rm -f /etc/nginx/conf.d/*
cat > /etc/nginx/conf.d/blog.conf << aaa
server {
listen 80;
server_name blog.benet.com;
root /wordpress;
index index.php index.html;
location ~ \.php$ {
root /wordpress;
fastcgi_pass ${hostip}:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME ?document_root?fastcgi_script_name;
include fastcgi_params;
}
}
aaa
sed -i 's/?/$/g' /etc/nginx/conf.d/blog.conf
cd /
unzip wordpress-4.9.4-zh_CN.zip
chmod -R 777 /wordpress
chown -R nginx:nginx /wordpress
cd
systemctl start nginx
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = web节点nginx启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = web节点nginx启动失败 = = = = = = = = = = = = = "
fi
nnn
done < aa.txt
echo " = = = = = = = = = = = = = = = = 正在配置lb节点 = = = = = = = = = = = = = = = = "
scp -rp /wordpress/ root@$ccip:/
cat ip.txt | cut -d " " -f 1 |awk ' NR==3 { print $1 }' >> bb.txt //截取ip.txt文件,以空格分隔第一列,第一行,重定向到bb.txt文件
cat ip.txt | cut -d " " -f 1 |awk ' NR==4 { print $1 }' >> bb.txt
ngip1=`cat aa.txt | cut -d " " -f 1 |awk ' NR==1 { print $1 }'` //截取aa.txt文件,以空格分隔第一列,第一行
ngip2=`cat aa.txt | cut -d " " -f 1 |awk ' NR==2 { print $1 }'`
read -p "请输入你当前属于几网段" wd
while read line
do
nip3=`echo $line | cut -d " " -f 1`
ssh $nip3 << ggg
rm -f rm -f /etc/nginx/conf.d/*
cat > /etc/nginx/conf.d/lb${nip3}.conf << aaa //自动修改配置文件
upstream web_cluster {
server ${ngip1}:80;
server ${ngip2}:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://web_cluster;
include nginx_params;
}
}
aaa
cat > /etc/nginx/nginx_params << yyy
proxy_set_header Host ?http_host;
proxy_set_header X-Real-IP ?remote_addr;
proxy_set_header X-Forwarded-For ?proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
yyy
sed -i 's/?/$/' /etc/nginx/nginx_params
systemctl start nginx
rm -rf /etc/keepalived/keepalived.conf
rm -rf /var/run/yum.pid && yum -y install keepalived
cat > /etc/keepalived/keepalived.conf << kkk
global_defs {
router_id lb$nip3
}
vrrp_script check_nginx_proxy {
script “/sh/check_nginx_proxy.sh”
interval 2
weight 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.${wd}.254
}
}
track_script {
check_nginx_proxy
}
}
kkk
systemctl start nginx //启动nginx
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = lb节点nginx启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = lb节点nginx启动失败 = = = = = = = = = = = = = "
fi
mkdir /sh
cat > /sh/check_nginx_proxy.sh << sss //编辑检查nginx脚本
#!/bin/bash
killall -0 nginx
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
sss
chmod +x /sh/check_nginx_proxy.sh
systemctl start keepalived
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = keepalived启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = keepalived启动失败 = = = = = = = = = = = = = "
fi
ggg
done < bb.txt
gpzip=`cat bb.txt |cut -d " " -f 1 | awk ' NR==2 { print $1 }'`
ssh $gpzip << ppp
sed -i 's/100/99/' /etc/keepalived/keepalived.conf
sed -i 's/MASTER/BACKUP/' /etc/keepalived/keepalived.conf //把MASTER替换成BACKUP
systemctl restart keepalived
ppp
echo " = = = = = = = = = = = = = = = = 安装配置mysql,php = = = = = = = = = = = = = = = = "
cd mysql5.6-rpm/
yum -y localinstall *.rpm //安装以.rpm结尾的程序
cd /root/qrl/php-rpm/
yum -y localinstall *.rpm
systemctl start mysql
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = mysql启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = mysql启动失败 = = = = = = = = = = = = = "
fi
mysql -e "create database blog;" //执行mysql命令,创建数据库blog
mysql -e "grant all on blog.* to lisi@'%' identified by '123456';" //授权lisi用户以123456登录管理blog数据库,具有所有权限
echo "192.168.${wd}.254 blog.benet.com" >> /etc/hosts //将主机ip与域名重定向到hosts文件
sed -i "s/listen = 127.0.0.1:9000/listen = "$hostip":9000/" /etc/php-fpm.d/www.conf //替换listen行内容
sed -i "s@listen.allowed_clients = 127.0.0.1@listen.allowed_clients = "$ngip1","$ngip2"@" /etc/php-fpm.d/www.conf
systemctl start php-fpm //启动php-fpm守护进程
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = php启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = php启动失败 = = = = = = = = = = = = = "
fi
目标四:
提示:执行脚本前,必须先部署环境
部署环境 1、拷贝所需文件到master服务器,并为其设置权限 2、保障网络连通,(配置好yum仓库,略) 3、 执行脚本
#!/bin/bash
echo " = = = = = = = = = = = = = = = = 免密交互 = = = = = = = = = = = = = = = = "
cat > /root/ip.txt << E
192.168.2.2 root 123456
192.168.2.3 root 123456
E
echo " = = = = = = = = = = = = = = = = expect正在安装 = = = = = = = = = = = = = = = = "
yum -y install expect &> /dev/null
echo " = = = = = = = = = = = = = = = = 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 "password" { send "$passwd\n" }
EOF
done < ip.txt
echo " = = = = = = = = = = = = = = = = 所有节点正在安装nginx = = = = = = = = = = = = = = = = "
while read -r line
do
nip1=`echo $line | cut -d " " -f 1`
scp -rp /root/qrl/nginx-rpm root@${nip1}:/root
ssh $nip1 << hhh
rm -rf /var/run/yum.pid
cd nginx-rpm/
yum -y localinstall *.rpm
hhh
done < ip.txt
echo " = = = = = = = = = = = = = = = = 所有配置web节点 = = = = = = = = = = = = = = = = "
cat ip.txt | cut -d " " -f 1 |awk ' NR==1 { print $1 }' >> aa.txt
cat ip.txt | cut -d " " -f 1 |awk ' NR==2 { print $1 }' >> aa.txt
ccip=`echo ip.txt | cut -d " " -f 1 |awk ' NR==1 { print $1 }'`
hostip=`ifconfig ens33 | awk ' NR==2 { print $2 }'`
while read -r line
do
nip2=`echo $line | cut -d " " -f 1`
scp -rp /root/qrl/wordpress-4.9.4-zh_CN.zip root@$nip2:/
ssh $nip2 << nnn
rm -f /etc/nginx/conf.d/*
cat > /etc/nginx/conf.d/blog.conf << aaa
server {
listen 80;
server_name blog.benet.com;
root /wordpress;
index index.php index.html;
location ~ \.php$ {
root /wordpress;
fastcgi_pass ${hostip}:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME ?document_root?fastcgi_script_name;
include fastcgi_params;
}
}
aaa
sed -i 's/?/$/g' /etc/nginx/conf.d/blog.conf
cd /
unzip wordpress-4.9.4-zh_CN.zip
chmod -R 777 /wordpress
cd
systemctl start nginx
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = web节点nginx启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = web节点nginx启动失败 = = = = = = = = = = = = = "
fi
nnn
done < aa.txt
echo " = = = = = = = = = = = = = = = = 正在配置lb节点 = = = = = = = = = = = = = = = = "
scp -rp root@$ccip:/wordpress /
cat ip.txt | cut -d " " -f 1 |awk ' NR==3 { print $1 }' >> bb.txt
cat ip.txt | cut -d " " -f 1 |awk ' NR==4 { print $1 }' >> bb.txt
ngip1=`cat aa.txt | cut -d " " -f 1 |awk ' NR==1 { print $1 }'`
ngip2=`cat aa.txt | cut -d " " -f 1 |awk ' NR==2 { print $1 }'`
read -p "请输入你当前属于几网段" wd
while read line
do
nip3=`echo $line | cut -d " " -f 1`
ssh $nip3 << ggg
rm -f rm -f /etc/nginx/conf.d/*
cat > /etc/nginx/conf.d/lb${nip3}.conf << aaa
upstream web_cluster {
server ${ngip1}:80;
server ${ngip2}:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://web_cluster;
include nginx_params;
}
}
aaa
cat > /etc/nginx/nginx_params << yyy
proxy_set_header Host ?http_host;
proxy_set_header X-Real-IP ?remote_addr;
proxy_set_header X-Forwarded-For ?proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
yyy
sed -i 's/?/$/' /etc/nginx/nginx_params
systemctl start nginx
rm -rf /etc/keepalived/keepalived.conf
rm -rf /var/run/yum.pid && yum -y install keepalived
cat > /etc/keepalived/keepalived.conf << kkk
global_defs {
router_id lb$nip3
}
vrrp_script check_nginx_proxy {
script “/sh/check_nginx_proxy.sh”
interval 2
weight 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.${wd}.254
}
}
track_script {
check_nginx_proxy
}
}
kkk
systemctl start nginx
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = lb节点nginx启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = lb节点nginx启动失败 = = = = = = = = = = = = = "
fi
mkdir /sh
cat > /sh/check_nginx_proxy.sh << sss
#!/bin/bash
killall -0 nginx
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
sss
chmod +x /sh/check_nginx_proxy.sh
systemctl start keepalived
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = keepalived启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = keepalived启动失败 = = = = = = = = = = = = = "
fi
ggg
done < bb.txt
gpzip=`cat bb.txt |cut -d " " -f 1 | awk ' NR==2 { print $1 }'`
ssh $gpzip << ppp
sed -i 's/100/99/' /etc/keepalived/keepalived.conf
sed -i 's/MASTER/BACKUP/' /etc/keepalived/keepalived.conf
systemctl restart keepalived
ppp
echo " = = = = = = = = = = = = = = = = 安装配置mysql,php = = = = = = = = = = = = = = = = "
cd mysql5.6-rpm/
yum -y localinstall *.rpm
cd /root/qrl/php-rpm/
yum -y localinstall *.rpm
systemctl start mysql
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = mysql启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = mysql启动失败 = = = = = = = = = = = = = "
fi
mysql -e "create database blog;"
mysql -e "grant all on blog.* to lisi@'%' identified by '123456';"
echo "192.168.${wd}.254 blog.benet.com" >> /etc/hosts
sed -i "s/listen = 127.0.0.1:9000/listen = "$hostip":9000/" /etc/php-fpm.d/www.conf
sed -i "s@listen.allowed_clients = 127.0.0.1@listen.allowed_clients = "$ngip1","$ngip2"@" /etc/php-fpm.d/www.conf
systemctl start php-fpm
if [ $? -eq 0 ];then
echo " = = = = = = = = = = = = = php启动成功 = = = = = = = = = = = = = "
else
echo " = = = = = = = = = = = = = php启动失败 = = = = = = = = = = = = = "
fi
4、常见报错处理: 免密交互,报错:连接被拒绝 解决方案: 重启目标服务器sshd服务,等几秒钟测试 出现以下提示,证明已经解决此问题
|