脚本安装nging实现高可用加负载

ssh.sh免密脚本

#!/bin/bash
VIP=10.0.0.30
IP_0=10.0.0.41
IP_1=10.0.0.42
PORT1=10.0.0.43:8080
PORT2=10.0.0.43:8081
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xzf nginx-1.16.1.tar.gz
cd nginx-1.16.1
yum -y install gcc gcc-c++ zlib-devel pcre-devel keepalived
./configure && make && make install
c_port=`cat /etc/keepalived/keepalived.conf | grep 'connect_port 80' | wc -l`
if [ $c_port -ne 1 ];then
sed -i '/notification_email {/,/vrrp_garp_interval 0/d' /etc/keepalived/keepalived.conf
sed -i 's/interface eth0/interface ens33/' /etc/keepalived/keepalived.conf
sed -i '/192.168.200.16/d' /etc/keepalived/keepalived.conf
sed -i '/192.168.200.17/d' /etc/keepalived/keepalived.conf
sed -i "s/192.168.200.18/$VIP/" /etc/keepalived/keepalived.conf
sed -i "s/virtual_server 192.168.200.100 443 {/virtual_server $VIP 80 {/" /etc/keepalived/keepalived.conf
sed -i '/persistence_timeout 50/d' /etc/keepalived/keepalived.conf
sed -i "s/real_server 192.168.201.100 443 {/real_server $IP_0 80 {/" /etc/keepalived/keepalived.conf
sed -i '/weight 1/anotify_down /root/nginx.sh' /etc/keepalived/keepalived.conf
sed -i 's/SSL_GET {/TCP_CHECK {/' /etc/keepalived/keepalived.conf
sed -i '/url {/,/connect_timeout 3/d' /etc/keepalived/keepalived.conf
sed -i '/TCP_CHECK {/aconnect_timeout 3' /etc/keepalived/keepalived.conf
sed -i '/delay_before_retry 3/aconnect_port 80' /etc/keepalived/keepalived.conf
sed -i '/virtual_server 10.10.10.2 1358/,$d' /etc/keepalived/keepalived.conf
fi
echo '#!bin/bash
systemctl stop keepalived' > /root/nginx.sh
chmod +x /root/nginx.sh
up_tom=`cat /usr/local/nginx/conf/nginx.conf | grep 'upstream tomcats {' | wc -l`
if [ $up_tom -ne 1 ];then
sed -i "/#gzip  on;/aupstream tomcats {\nserver $PORT1;\nserver $PORT2;\n}" /usr/local/nginx/conf/nginx.conf
sed -i 's/index  index.html index.htm;/index  index.jsp index.html index.htm;/' /usr/local/nginx/conf/nginx.conf
sed -i '/404.html;/alocation ~ \\.jsp$ {\nindex index.jsp;\nproxy_pass http://tomcats;\n}' /usr/local/nginx/conf/nginx.conf
fi
/usr/local/nginx/sbin/nginx
systemctl restart keepalived

chmod +x /root/ssh.sh
sh /root/ssh.sh

scp /root/nginx-1.16.1.tar.gz root@$IP_1:/root
/usr/bin/expect<<EOF
spawn ssh root@$IP_1
expect "~" {send "tar -xzf nginx-1.16.1.tar.gz\n"}
expect "~" {send "cd nginx-1.16.1\n"}
set timeout 1000
expect "nginx-1.16.1" {send "yum -y install gcc gcc-c++ zlib-devel pcre-devel keepalived\n"}
expect "nginx-1.16.1" {send "./configure && make && make install\n"}
expect "nginx-1.16.1" {send "exit\n"}
expect eof
EOF
scp /etc/keepalived/keepalived.conf root@$IP_1:/etc/keepalived/keepalived.conf
ssh root@$IP_1 "sed -i 's/state MASTER/state BACKUP/' /etc/keepalived/keepalived.conf;sed -i 's/priority 100/priority 90/' /etc/keepalived/keepalived.conf"
ssh root@$IP_1 "sed -i 's/real_server $IP_0 80 {/real_server $IP_1 80 {/' /etc/keepalived/keepalived.conf"

scp /usr/local/nginx/conf/nginx.conf root@$IP_1:/usr/local/nginx/conf/nginx.conf
ssh root@$IP_1 "/usr/local/nginx/sbin/nginx;systemctl restart keepalived"
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值