【shell语言学堂】bash脚本实现Nginx负载均衡Tomcat

#!/bin/bash

# 停止防火墙并关闭SELinux
systemctl stop firewalld
setenforce 0

# 安装Nginx依赖
echo "正在安装Nginx依赖..."
yum -y install wget vim net-tools &>/dev/null

# 安装Nginx
echo "正在安装Nginx……"
yum install -y epel-release &>/dev/null && yum install -y nginx &>/dev/null

# 检查并添加upstream tomcats配置
grep "upstream tomcats" /etc/nginx/nginx.conf &>/dev/null
if [ $? -ne 0 ]; then
    sed -i -e '39a upstream tomcats {\n\tserver 192.168.99.155:8080 weight=1;\n\tserver 192.168.99.140:8081 weight=1;\n\tserver 192.168.99.140:8082 weight=1;\n\t}' /etc/nginx/nginx.conf
fi

# 检查并添加server_name配置
grep "server_name 192.168.99.137;" /etc/nginx/nginx.conf &>/dev/null
if [ $? -ne 0 ]; then
    echo -i -e 'listen 80;\n\tserver_name 192.168.99.137;\n\tlocation ~ .*\.jsp$ {\n\t\tproxy_pass http://tomcats;\n\t\tproxy_set_header Host $host;\n\t\tproxy_set_header X-Real-IP $remote_addr;\n\t\tproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\t}' /etc/nginx/conf.d/tomcatweb.conf
fi

# 重启Nginx服务
systemctl restart nginx

# 在/opt目录下安装三个Tomcat实例
cd /opt
for i in {1..3}; do
    {
        # 创建并配置Tomcat安装脚本
        cat >/opt/tomcat$i.sh <<EOF
#!/bin/bash
cd /opt
rpm -ivh  /opt/jdk-8u371-linux-x64.rpm
echo "export JAVA_HOME=/usr/java/jdk1.8.0-x64
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH" >> /etc/profile.d/java.sh
# 使环境变量配置立即生效
source /etc/profile.d/java.sh
tar xf apache-tomcat-9.0.78.tar.gz
mv -f apache-tomcat-9.0.78 tomcat$i
cat >/opt/tomcat$i/webapps/ROOT/index.jsp <<EOF$i
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test$i page</title>
</head>
<body>
<% out.println("动态页面 $i");%>
</body>
</html>
EOF$i
grep "<Context docBase=" /opt/tomcat$i/conf/server.xml &>/dev/null
if [ $? -eq 0 ];then
sed -i -e '163d' /opt/tomcat$i/conf/server.xml
fi
/opt/tomcat$i/bin/startup.sh
if [ $? -eq 0 ]; then
    echo "tomcat$i启动成功"
else
    echo "tomcat$i启动失败"
    exit $i
fi
EOF
        chmod +x /opt/tomcat$i.sh
    }
done

# 安装expect工具,用于自动化交互式命令行操作
yum install expect -y &>/dev/null

# 创建并配置expect脚本,用于自动部署Tomcat到远程服务器
cat >/opt/tomcat1.exp <<EOF
#!/usr/bin/expect
set timeout 300
spawn scp /opt/tomcat1.sh /opt/apache-tomcat-9.0.78.tar.gz /opt/jdk-8u371-linux-x64.rpm root@192.168.99.155:/opt
expect {
    "*yes/no*" {send "yes\r";exp_continue}
    "*password*" {send "root\r";exp_continue}
}
spawn   ssh root@192.168.99.155 "bash /opt/tomcat1.sh"
expect {
    "*password*" {send "root\r";exp_continue}
}
EOF
cat >/opt/tomcat2.exp <<EOF
#!/usr/bin/expect
set timeout 300
spawn scp /opt/tomcat2.sh /opt/tomcat3.sh /opt/apache-tomcat-9.0.78.tar.gz /opt/jdk-8u371-linux-x64.rpm root@192.168.99.140:/opt
expect {
    "*yes/no*" {send "yes\r";exp_continue}
    "*password*" {send "root\r";exp_continue}
    }
spawn   ssh root@192.168.99.140 "bash /opt/tomcat2.sh && bash /opt/tomcat3.sh"
expect {
    "*password*" {send "root\r";exp_continue}
}
EOF

chmod +x /opt/tomcat1.exp
chmod +x /opt/tomcat2.exp
/opt/tomcat1.exp
/opt/tomcat2.exp
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Shell脚本是指在Unix或类Unix操作系统中使用的脚本,用于执行一系列的命令。它可以包含各种命令、逻辑控制结构和变量定义等。Shell脚本可以用于自动化任务、系统管理、数据处理等多种用途。 Bash脚本是一种Shell脚本的变种,它是基于Bourne Shell的改进版本。Bash脚本具有更强大的功能和更丰富的语法,同时保留了兼容性。因此,大多数Linux系统使用Bash作为默认的Shell解释器。 引用提到了一些bash脚本的编写流程、代码规范和编写习惯的培养等内容,这对于那些没有太强的bash脚本编程基础的人来说是非常有帮助的。 引用和引用讲述了在bash脚本中使用双引号和单引号包围变量时的不同效果。双引号会解析变量和命令,而单引号则原样输出变量和命令。 综上所述,Shell脚本是一种用于执行一系列命令的脚本语言,而Bash脚本是一种基于Bourne Shell的改进版本,具有更强大的功能和更丰富的语法。在编写bash脚本时,可以根据需要使用双引号或单引号来处理变量和命令。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Bash Shell脚本实战11例](https://download.csdn.net/download/phoenixkiki/10585048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [bashshell脚本](https://blog.csdn.net/cyuyangundan/article/details/124772034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值