Implement tomcat simple multi project, ssl deployment

 

1. Prepare tomcat version and jdk version

Tomcat version: apache-tomcat-8.5.38.tar.gz

JDK version: jdk-8u121-linux-x64.tar.gz

Deployment script: tomcatconfig.sh

ssl and tomcat configuration directory config

2. script

#!/bin/bash

tomcatzip=apache-tomcat-8.5.38.tar.gz
tomcatName=apache-tomcat-8.5.38
jdkzip=jdk-8u121-linux-x64.tar.gz
configfile=config
jdkfile=jdk1.8.0_121
project=""
pNum=0
childProject=""

function checkJdkConfig(){
    echo `pwd`
    if [ -d `pwd`/$jdkfile ]; then
        echo "JDK Folder does not exist,Please check. JDK Whether the configuration exists"
    else
        echo "JDK Folder does not exist,Create medium........"
        if [ -f `pwd`/$jdkzip ]; then
           echo "JDK Compressed package exists,Decompression"
           tar -xf `pwd`/$jdkzip
           echo "JDK Unzip to finish, go to the next step"
        else
           echo "JDK Compressed package does not exist,Please check if the compressed package exists"
           exit 1
        fi
    fi
}

function checkTomcatConfig(){
    echo `pwd`
    if [ -f `pwd`/$tomcatzip ]; then
        echo "$tomcatzip Already exist"
    else
        echo "$tomcatzip Non-existent,Please check if the compressed package exists"
        exit 1
    fi
}

function checkConfig(){
    checkJdkConfig
    checkTomcatConfig
    buildProject
}

function buildProject(){
    read -p "Please enter project name: " projectName
    project="$projectName"
    echo $project
    makeAdmindir
    read -p "Please enter the number of subprojects: " projectNum
    pNum="$projectNum"
    echo $pNum

    makeChildren
}

function makeAdmindir(){
    if [ -d `pwd`/$project ]; then
        echo "`pwd`/$project Project already exists,Next, create subprojects"
    else
        mkdir `pwd`/$project
        echo "$project Created next, create subproject"
    fi
}

function makeChildren(){
    for((i=1;i<=$pNum;i++));
    do

        read -p "Please enter a sub project name: " childName
        childProject="$childName"
        cpath="`pwd`/$project/$childProject"
        if  [ -d `pwd`/$project/$childProject ]; then
            echo "`pwd`/$project/$childProject Already exist,Next, configure the environment"
        else
            tar -xf `pwd`/$tomcatzip -C `pwd`/$project
            mv `pwd`/$project/$tomcatName $cpath
            chmod 777 -R $cpath
            echo "$project Create the next step, configure the environment"
        fi

        cp -r `pwd`/config/common/* $cpath/bin/

        read -p "Whether to support ssl[yes/no]: " https
        echo $https
        if [ $https = "yes" ]; then
            cp -r `pwd`/config/https/* $cpath/conf/
        else
            cp -r `pwd`/config/http/* $cpath/conf/
            echo "$project Create the next step, configure the environment"
        fi  
        echo "Preparation and improvement of relevant documents,Start configuration parameters" 
        echo "Set up setclasspath.sh in JDK Parameter, setting.........." 
        jdkpath="`pwd`/$jdkfile"
        echo $jdkpath
        sed -i -e "s#project_jdk#$jdkpath#g" $cpath/bin/setclasspath.sh
        echo "Set up setclasspath.sh in JDK Parameter, setting completed, path:`pwd`/$jdkfile"

        echo "Set up catalina.sh Middle path parameter, setting.........."
        
        sed -i -e "s#project_path#$cpath#g" $cpath/bin/catalina.sh

        read -p "Support local upload:[yes/no]: " upload
        echo $upload
        if [ $upload = "yes" ]; then
            sed -i -e "s/project_photo/0022/g" $cpath/bin/catalina.sh
            echo "Set local upload parameters successfully"  
        else
            sed -i -e "s/project_photo/0027/g" $cpath/bin/catalina.sh
        fi  

        echo "Set up catalina.sh The middle path parameter is set, and the path is:`pwd`/$project/$childProject"  
        
        echo "Set up server.xml in SHUTDOWN Port, path $cpath/conf/server.xml" 
        read -p "Please set up shutdown port :" shutdownPort
        if [ $shutdownPort -eq 0 ]; then
            echo "Set up shutdown port"  
            exit 1
        else
            sed -i -e "s/project_8005/$shutdownPort/g" $cpath/conf/server.xml
        fi 

        echo "Set up server.xml in tomcat Start port, path $cpath/conf/server.xml" 
        read -p "Please set up tomcat port: " tomcatPort
        if [ $tomcatPort -eq 0 ]; then
            echo "Set up tomcat port"  
            exit 1
        else
            sed -i -e "s/project_8080/$tomcatPort/g" $cpath/conf/server.xml
        fi    

        if [ $https = "yes" ]; then
            echo "Set up server.xml in https Start port, path $cpath/conf/server.xml"
            read -p "Please set up https port: " httpsPort
            if [ $httpsPort -eq 0 ]; then
                echo "Set up https port"  
                exit 1
            else
                sed -i -e "s/project_8443/$httpsPort/g" $cpath/conf/server.xml
            fi  

            echo "Set up server.xml in ssl Certificate path, path $cpath/conf/server.xml"
            read -p "Please set up ssl Password: " sslpasswd
            if [ $sslpasswd = "" ]; then
                echo "Please set up ssl Password"  
                exit 1
            else
                sed -i -e "s/project_passwd/$sslpasswd/g" $cpath/conf/server.xml
            fi  

            sed -i -e "s#project_path#$cpath#g" $cpath/conf/server.xml
            echo "Certificate path setup complete"

            read -p "Please set the domain name: " netName
            if [ $netName = "" ]; then
                echo "Please set the domain name"  
                exit 1
            else
                sed -i -e "s/project_localhost/$netName/g" $cpath/conf/server.xml
            fi  

            echo "Domain name setting completed"
        else
            sed -i -e "s/project_8443/8443/g" $cpath/conf/server.xml
        fi 

        echo "Set up server.xml in AJP Start port, path $cpath/conf/server.xml" 
        read -p "Please set up AJP port: " AJPPort
        if [ $AJPPort -eq 0 ]; then
            echo "Set up AJP port"  
            exit 1
        else
            sed -i -e "s/project_8009/$AJPPort/g" $cpath/conf/server.xml
        fi

        echo "Set up server.xml Medium heat deployment, path $cpath/conf/server.xml" 
        read -p "Please set the hot deployment folder name: " hotConfig
        if [ $hotConfig != "" ]; then
            hotinfo="$cpath/webapps/$hotConfig"
            sed -i -e "s#project_hot#$hotinfo#g" $cpath/conf/server.xml
        fi
    done
}

checkConfig

3. Prepare tomcat configuration file

config/common config/http config/https

The file configuration file catalina.sh setclasspath.sh in common comes from the original file

catalina.sh add to set tomcat environment:

export JAVA_OPTS="$JAVA_OPTS -Duser.timezone=Asia/shanghai"

# myself : add
export CATALINA_BASE=project_path
export CATALINA_HOME=project_path
export TOMCAT_HOME=project_path

catalina.sh add to set tomcat image upload permission:

# Set UMASK unless it has been overridden
if [ -z "$UMASK" ]; then
    UMASK="project_photo"
fi
umask $UMASK

setclasspath.sh setting up the jdk environment

export JAVA_HOME=project_jdk
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:%{JAVA_HOME}/lib:%{JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

Configure server.xml in http

Configure server.xml web.xml in http

If https web.xml is added

  <security-constraint>
    <!-- Authorization setting for SSL -->
    <web-resource-collection >
        <web-resource-name >SSL</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
  </security-constraint>

Modify server.xml as follows:

Set shutdown port:

<Server port="project_8005" shutdown="SHUTDOWN">

Set http port:

    <Connector port="project_8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="project_8443" />

Set https port:

    <Connector port="project_8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               scheme="https" secure="true" connectionTimeout="30000"  maxThreads="30000" SSLEnabled="true"
               keystoreFile="project_path/tomcat.keystore" keystorePass="project_passwd"
                clientAuth="false" sslProtocol="TLS">
    </Connector>

Set AJP port:

  <Connector port="project_8009" protocol="AJP/1.3" redirectPort="project_8443" />

Set domain name:

    <Engine name="Catalina" defaultHost="project_localhost">

      <Host name="project_localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

Set up hot deployment: under host:

        <Context docBase="project_hot" path="" reloadable="true" />

4. Start at the same level of the above directory

Published 76 original articles, won praise 2, visited 20000+
Private letter follow

Tags: xml Tomcat JDK SSL

Posted on Sat, 14 Mar 2020 13:24:01 -0400 by sw45acp