shell 编程实战Nginx、Tomcat脚本

shell脚本实现Nginx自动安装、虚拟主机及自动将Tomcat加入至虚拟主机编程思路:
手动复制Tomcat与脚本一致目录;
手动修改Tomcat端口为6001、7001、8001;
脚本指定参数www.wuyutang.com;
创建www.wuytutang.com实例;
修改Tomcat实例端口,保证port唯一;
将Tomcat实例加入Nginx虚拟主机;
重复创建Tomcat实例,商品自动增加,并加入原Nginx虚拟主机,实现负载均衡。
shell脚本实现Nginx自动安装、虚拟主机及自动将Tomcat加入至虚拟主机:
#!/bin/bash
#Auto config Nginx amdtomcat cluster
#By auther tang.com2018
#Define Path variables
NGINX_CONF="/usr/local/nginx/conf"
install_nginx(){
NGINX_FILE=nginx-1.14.1.tar.gz
NGINX_DIR=`echo$NGINX_FILE |sed 's/.tar*.*//g'`
NGINX_MAKE="--user=www--group=www --prefix=/usr/local/nginx --with-http_stub_status_module--with-http_ssl_module"
wget-c http://nginx.org/download/$NGINX_FILE
yuminstall pcre-devel pcre -y
rm-rf $NGINX_DIR;tar xf $NGINX_FILE
cd$NGINX_DIR;useradd www -s /usr/sbin/nologin -M;./configure $NGINX_MAKE
make&& make install
cd../
}
install_tomcat(){
JDK_FILE="jdk1.7.0_25.tar.gz"
JDK_DIR=`echo$JDK_FILE|sed 's/.tar.*//g'`
tar-xzf $JDK_FILE;mkdir -p /usr/java/;mv $JDK_DIR /usr/java/
sed-i '/JAVA_HOME/d;/JAVA_BIN/d;/JAVA_OPTS/d' /etc/profile
cat>> /etc/profile <<EOF
exportJAVA_HOME=/export/servers/$JAVA_DIR
exportJAVA_BIN=/export/servers/$JAVA_DIR/bin
exportPATH=\$JAVA_HOME/bin:\$PATH
exportCLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
exportJAVA_HOME JAVA_BIN PATH CLASSPATH
EOF
source/etc/profile;java -version
#installtamcat start
lstomcat
}
config_tomcat_nginx()
{
#configtomcat nginx vhosts
grep"include domains" $NGINX_CONF/nginx.conf >>/dev/null
if [$? -ne 0 ];then
sed-i "$d" $NGINX_CONF/nginx.conf
sed-e "\ninclude domains/*;\n}">> $NGINX_CONF/nginx.conf
mkdir-p $NGINX_CONF/domains/
fi
VHOSTS=$1
NUM=`ls/usr/local/|grep -c tomcat`
if [$NUM -eq 0 ];then
cp -r tomcat /usr/local/tomcat_$VHOSTS
cp -r www.wuyutang.com $NGINX_CONF/domains/$VHOSTS
#sed-i "s/www/$VHOSTS/g" $NGINX_CONF/domains/$VHOSTS
exit0
fi
#-----------------------------------------------------
#VHSOTS=$1
VHOSTS_NUM=`ls$NGINX_CONF/domains/|grep -c $VHOSTS`
SERVER_NUM=`grep-c "127" $NGINX_CONF/domains/$VHOSTS`
SERVER_NUM_1=`expr$SERVER_NUM + 1`
rm-rf /tmp/.port.txt
fori in `find /usr/local/ -maxdepth 1 -name "tomcat"`;do
grep"port" $i/conf/server.xml |egrep -v"\--|8080|SSLEnabled"|awk '{print $2}'|sed's/port=//g;s/\"//g'|sort -nr >>/tmp/.port.txt
done
MAX_PORT=`cat/tmp/.port.txt|grep -v 8443|sort -nr|head -1`
PORT_1=`expr$MAX_PORT - 2000 + 1`
PORT_2=`expr$MAX_PORT - 1000 + 1`
PORT_3=`expr$MAX_PORT + 1`
if [$VHOSTS_NUM -eq 1 ];then
read-p "The $VHOSTS is exists,You sure create mulit Tomcat for the $VHOSTS?yes or no" INPUT
if [$INPUT == "y" -o $INPUT == "yes" ];then
cp-r tomcat /usr/local/tomcat_${VHOSTS}_${SERVER_NUM_1}
sed-i "s/6001/$PORT_1/g"/usr/local/tomcat_${VHOSTS}_${SERVER_NUM_1}/conf/server.xml
sed-i "s/7001/$PORT_2/g"/usr/local/tomcat_${VHOSTS}_${SERVER_NUM_1}/conf/server.xml
sed-i "s/8001/$PORT_3/g"/usr/local/tomcat_${VHOSTS}_${SERVER_NUM_1}/conf/server.xml
sed-i "/^upstream/aserver 127.0.0.1:${PORT_2} weight=1 max_fails=2fail_timeout=30s;" $NGINX_CONF/domains/$VHOSTS
exit0
fi
exit
fi
cp -r tomcat /usr/local/tomcat_$VHOSTS
cp -r www.wuyutang.com $NGINX_CONF/domains/$VHOSTS
sed-i "s/VHOSTS/$VHOSTS/g" $NGINX_CONF/domains/$VHOSTS
sed-i "s/www/$VHOSTS/g" $NGINX_CONF/domains/$VHOSTS
sed-i "s/7001/${PORT_2}/g" $NGINX_CONF/domains/$VHOSTS
#########configtamcat
sed-i "s/6001/$PORT_1/g" /usr/local/tomcat_${VHOSTS}/conf/server.xml
                sed -i"s/7001/$PORT_2/g" /usr/local/tomcat_${VHOSTS}/conf/server.xml
                sed -i"s/8001/$PORT_3/g" /usr/local/tomcat_${VHOSTS}/conf/server.xml
}
if [ ! -d $NGINX_CONF-o ! -d /usr/java/$JDK_DIR ];then
install_nginx
install_tomcat
fi
config_tomcat_nginx $1