1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > nginx+tomcat负载均衡 反向代理一键部署脚本

nginx+tomcat负载均衡 反向代理一键部署脚本

时间:2018-08-06 15:14:54

相关推荐

nginx+tomcat负载均衡 反向代理一键部署脚本

实验准备:

系统环境:CentOS7.9

代理服务器:nginx

Tomcat Web服务器两台

nginx反向代理tomcat,实现tomcat后端服务器负载均衡

此脚本是交互式选择性部署服务,可在任何一台机器上使用该脚本,并选择所需部署的服务!

tomcat及jdk依赖包放在文章末尾了,自取

#!/bin/bash#function:nginx+tomcat反向代理#author:墨染function qianti {if [ $USER != root ];thenecho "当前不是root用户,请切换至root用户再次运行脚本"exitfiping -c3 -i0.1 -W1 &> /dev/nullif [ $? != 0 ];thenecho "当前无网络,请保证网络畅通再次运行脚本"exitelseecho "当前网络畅通,即将开始运行脚本"fisleep 2systemctl stop firewalld.service && systemctl disable firewalld.service sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/configsetenforce 0echo "防火墙和selinux高级权限管理已关闭"} ############################################全局生效###############################dir="/usr/local/src"function PD {if [ ! -e $dir/$1 ];thenecho -e "\033[31m$1不存在\033[0m" && sleep 3 && echo -e "\033[31m请将$1上传到/usr/local/src目录下\033[0m"exitfi}############################################tomcat_install###############################function tomcat_install {echo "开始部署Tomcat"sleep 2JDK=/usr/local/src/jdk-8u311-linux-x64.tar.gzTomcat=/usr/local/src/apache-tomcat-9.0.64.tar.gzif [ ! -e $JDK ];thenecho "JDK安装包不存在,请讲安装包上传至/usr/local/src目录,再运行脚本"exitfiif [ ! -e $Tomcat ];thenecho "Tomcat安装包不存在,请讲安装包上传至/usr/local/src目录,再运行脚本"exitfiecho "开始解压依赖JDK安装包"sleep 2cd /usr/local/srctar -xvf $JDKLJ=/usr/local/src/jdk1.8.0_311echo "正在更改环境变量"sleep 2echo "export JAVA_HOME="$LJ >> /etc/profileecho "export CLASSPATH=.:JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar" >> /etc/profileecho "export PATH=$PATH:$JAVA_HOME/bin" >> /etc/profileif [ $? -eq 0 ];thensource /etc/profile && source /etc/profileelseecho "未成功更改环境,请及时排查"exit fiecho "开始安装Tomcat"tar -xvf $TomcatKZ=/usr/local/src/apache-tomcat-9.0.64/binecho "正在将tomcat注册为系统服务"cat>>/usr/lib/systemd/system/tomcat.service<< EOF[Unit]Description=Tomcatno)After=syslog.target network.target remote-fs.target nss-lookup.target[Service]Type=oneshotExecStart=$KZ/startup.shExecStop=$KZ/shutdown.shRemainAfterExit=yes[Install]WantedBy=multi-user.targetEOFmv /usr/local/src/apache-tomcat-9.0.64/webapps/ROOT/index.jsp /usr/local/src/apache-tomcat-9.0.64/webapps/ROOT/index.jsp_bakecho "<h1>`ifconfig | grep inet | cut -d " " -f 10 | head -1`_Tomcat</h1>" > /usr/local/src/apache-tomcat-9.0.64/webapps/ROOT/index.jspecho "服务部署成功"systemctl start tomcat.service && systemctl enable tomcat.service && systemctl status tomcat.serviceecho "端口启动成功"netstat -ntlp | grep 8080echo "请在浏览器输入`ifconfig | grep inet | cut -d " " -f 10 | head -1`:8080测试"}############################################nginx_install###############################function nginx_install {echo "正在安装依赖包,请稍等...."yum -y install gcc-c++ && yum install -y pcre pcre-devel && yum install -y zlib zlib-devel && yum install -y openssl openssl-devel cd /usr/local/srcecho "正在下载压缩包,请稍等...."wget /download/nginx-1.20.2.tar.gztar xvfz nginx-1.20.2.tar.gzecho "正在配置,请稍等...."sleep 2cd nginx-1.20.2 && ./configure --prefix=/usr/local/nginxecho "正在安装服务请稍等..."sleep 2make && make installecho "正在将nginx注册为系统服务"sleep 2cat>>/lib/systemd/system/nginx.service<< EOF[Unit]Description=nginxAfter=network.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true[Install]WantedBy=multi-user.targetEOFsystemctl start nginx.service && systemctl enable nginx.service && systemctl status nginx.serviceecho "端口已开启成功" && netstat -ntlp | grep 8080echo -e "服务及开机自启部署成功,请输入\033[32m`ifconfig | grep inet | cut -d " " -f 10 | head -1`\033[0m测试"}##########################################agency#######################################function agency {echo "开始配置nginx+tomcat反向代理..." && sleep 5read -p "请输入您的第一台tomcat的IP地址和端口号;(如:192.168.100.128:8080):" TOM1read -p "请输入您的第二台tomcat的IP地址和端口号;(如:192.168.100.128:8080):" TOM2sed -i "33a\ upstream tomcatserver {" /usr/local/nginx/conf/nginx.confsed -i "34a\ server ${TOM1} weight=1;" /usr/local/nginx/conf/nginx.confsed -i "35a\ server ${TOM2} weight=1;" /usr/local/nginx/conf/nginx.confsed -i "36a\ }" /usr/local/nginx/conf/nginx.confsed -i "49a\ proxy_pass http://tomcatserver;" /usr/local/nginx/conf/nginx.confsleep 2/usr/local/nginx/sbin/nginx -t 2> /root/.nginx_config.errorif [ $? != 0 ];thenecho -e "\033[31mnginx配置文件出错,详情请查看/root/.nginx_config.error文件 \033[0m"sleep 5exitelse sleep 2echo -e "\033[32mnginx_config........OK! \033[0m"sleep 3service nginx restartecho -e "请在浏览器中输入:\033[32m`ifconfig | grep inet | cut -d " " -f 10 | head -1`\033[0m测试反向代理!"fi }sleep 3 && echo "agency代理只支持部署nginx服务上,如果想要部署agency代理,请先部署nginx服务" && sleep 3read -p "请输入你想要部署的服务(nginx/tomcat/agency):" XZsleep 8case $XZ intomcat)qiantitomcat_install;;nginx)qiantinginx_install;;agency)agency;;*)echo "请明确输入想要部署的服务";;esac

最终效果:访问nginx代理服务器就相当于访问了两个tomcta后端web服务器,刷新一下,就会发现两个tomcat服务器同时在提供服务;

安装包:

链接:/s/1BTgwpJs4_m7CWS60m4jPIw

提取码:AK47

--来自百度网盘超级会员V1的分享

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。