Shell programming actual combat Nginx, Tomcat script

Tomcat is used to publish JSP web pages. According to the actual needs of the enterprise, N Tomcat instances will be configured on a single server. At the same time, the instances created by Tomcat will be manually added to the Nginx virtual host. At the same time, restart Nginx, develop Nginx, Tomcat to automatically create Tomcat Examples and Nginx virtual machine management scripts can greatly reduce manual intervention and achieve rapid delivery.

This shell script implements automatic installation of Nginx, virtual host, and automatic addition of Tomcat to the virtual machine.

1. Manually copy the directory where Tomcat is consistent with the script (can be modified by yourself);

2. Manually modify the Tomcat port to 6001, 7001, 8001 (can be modified by yourself);

3. The script specifies the parameter as v1.example.com;

4. Create a v1.example.com Tomcat instance;

5. Modify the Tomcat instance port to ensure that the port is unique;

6. Add the Tomcat instance to the Nginx virtual machine;

7. Repeat the creation of Tomcat instance, the port is automatically increased, and the original Nginx virtual host is added to achieve load balancing;

The specific implementation script is as follows:

#!/bin/bash

#Auto config Nginx and tomcat cluster

#By author falnet

#Define Path variables

NGINX_CONF="/usr/local/nginx/conf"

install_nginx(){

        NGX_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/

be

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

be

       #......................................................................................

       #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

            be

           exit

     be

    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

be

config_tomcat_nginx $1          

   

 

 

Published 14 original articles · Likes0 · Visits 414

Guess you like

Origin blog.csdn.net/falnet/article/details/104707088