功能简介
Tomcat用于发布JSP Web页面,根据企业实际需求,会在单台服务器配置N个Tomcat实例,同时手动将Tomcat创建后的实例加入至Nginx虚拟主机中,同时重启Nginx,开发Nginx、Tomcat自动创建Tomcat实例及Nginx虚拟机管理脚本能大大减轻人工的干预,实现快速交付。
本篇shell脚本实现Nginx自动安装、虚拟主机及自动将Tomcat加入虚拟机,实现思路如下:
- 手动拷贝Tomcat与脚本一致的目录(可自行修改);
- 手动修改Tomcat端口为6001、7001、8001(可自行修改);
- 脚本指定参数为v1.example.com;
- 创建v1.example.com Tomcat实例;
- 修改Tomcat实例端口,保证Port唯一;
- 将Tomcat实例加入Nginx虚拟
具体实现
具体实现脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #!/bin/bash
#Auto config Nginx and tomcat cluster
#By author falnet
#Define Path variables
NGINX_CONF="/usr/local/nginx/conf"
install_nginx(){
NGINX_FILE=nginx-1.16.0.tar.gz
NGINX_DIR='echo $NGINX_FILE|sed 's/.tar.*//g''
wget -c http://nginx.org/download/$NGINX_FILE
yum install pcre-devel pcre -y
rm -rf $NGINX_DIR; tar xf $NGINX_FILE
cd $NGINX_DIR; useradd www; ./configure --user=www --group=www --prefix=/usr/local/nginx2 --with-http_stub_status_module --with-http_ssl_module
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
export JAVA_HOME=/export/servers/$JAVA_DIR
export JAVA_BIN=/export/servers/$JAVA_DIR/bin
export PATH=\$JAVA_HOME/bin:\$PATH
export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
EOF
source /etc/profile; java -version
#install tomcat start
ls tomcat
}
config_tomcat_nginx(){
#config tomcat nginx vhost
grep " include domains" $NGINX_CONF/nginx.conf >>/dev/null 2>&1
if [ $? -ne 0 ]; then
sed -i '$d' $NGINX_CONF/nginx.conf
echo -e "\n include 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 xxx.example.com $NGINX_CONF/domains/$VHOSTS
#sed -i "s/VHOSTS/$VHOSTS/g" $NGINX_CONF/domains/$VHOSTS
sed -i "s/xxx/$VHOSTS/g" $NGINX_CONF/domains/$VHOSTS
exit 0
fi
#......................................................................................
#VHOSTS=$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
for i in 'find /usr/local/ -maxdepth 1 - name "tomcat"'; do
grep "port" $i/conf/server.xml | e grep -v "\--|8080||SSL Enabled"|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 == "YES" -o $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/a server 127.0.0.1:${PORT_2} weight=1 max_fails=2 fail_timeout=30s; " $NGINX_CONF/domains/$VHOSTS
exit 0
fi
exit
fi
cp -r tomcat /usr/local/tomcat_$VHOSTS
cp -r xxx.example.com $NGINX_CONF/domains/$VHOSTS
sed -i "s/VHOSTS/$VHOSTS/g" $NGINX_CONF/domains/$VHOSTS
sed -i "s/xxx/$VHOSTS/g" $NGINX_CONF/domains/$VHOSTS
sed -i "s/7001/${PORT_2}/g" $NGINX_CONF/domains/$VHOSTS
##########config tomcat
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
|