前面有寫過Centos下安裝Tomcat。那麼若是但願在一臺機器上跑多個Tomcat實例,該怎麼辦呢?html
安裝好以後,進入安裝目錄查看目錄結構:java
簡單介紹一下各個文件夾及文件:linux
一般,咱們在同一臺服務器上對 Tomcat 部署需求能夠分爲如下幾種:單實例單應用,單實例多應用,多實例單應用,多實例多應用。實例的概念能夠理解爲上面說的一個 Tomcat 目錄。web
webapps
目錄下,執行啓動 Tomcat 的腳本就好了。webapps
目錄下,執行啓動 Tomcat 的腳本,訪問不一樣項目加上不一樣的虛擬目錄。這種方式要慎用在生產環境,由於重啓或掛掉 Tomcat 後會影響另一個應用的訪問。
新建2個tomcat實例,ins01和ins02,文件目錄以下:apache
主要須要修改的配置爲conf/server.xml文件:flask
ins01配置:windows
<Server port="8005" shutdown="SHUTDOWN"> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector executor="tomcatThreadPool" port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
ins02配置:緩存
<Server port="8015" shutdown="SHUTDOWN"> <Connector port="8082" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector executor="tomcatThreadPool" port="8082" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />
啓停腳本tomcat
start.shbash
#!/bin/bash export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.5.31 export CATALINA_BASE=${1%/} echo $CATALINA_BASE TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2}'` if [ -n "$TOMCAT_ID" ] ; then echo "tomcat(${TOMCAT_ITOMCAT_ID}) still running now , please shutdown it first"; exit 2; fi TOMCAT_START_LOG=`$CATALINA_HOME/bin/startup.sh` if [ "$?" = "0" ]; then echo "$0 ${1%/} start succeed" else echo "$0 ${1%/} start failed" echo $TOMCAT_START_LOG fi
stop.sh
#!/bin/bash export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.5.31 export CATALINA_BASE=${1%/} echo $CATALINA_BASE TOMCAT_ID=`ps aux |grep "java"|grep "[D]catalina.base=$CATALINA_BASE "|awk '{ print $2}'` if [ -n "$TOMCAT_ID" ] ; then TOMCAT_STOP_LOG=`$CATALINA_HOME/bin/shutdown.sh` else echo "Tomcat instance not found : ${1%/}" exit fi if [ "$?" = "0" ]; then echo "$0 ${1%/} stop succeed" else echo "$0 ${1%/} stop failed" echo $TOMCAT_STOP_LOG fi
[root@localhost tomcat]# /usr/local/tomcat/tomcat-start-stop/start.sh /usr/local/tomcat/tomcat-ins01
/usr/local/tomcat/tomcat-ins01
/usr/local/tomcat/tomcat-start-stop/start.sh /usr/local/tomcat/tomcat-ins01 start succeed
[root@localhost tomcat]# /usr/local/tomcat/tomcat-start-stop/start.sh /usr/local/tomcat/tomcat-ins02
/usr/local/tomcat/tomcat-ins02
/usr/local/tomcat/tomcat-start-stop/start.sh /usr/local/tomcat/tomcat-ins02 start succeed
[root@localhost tomcat]#
查看啓動狀況
部署ins01
cat tomcat-ins01/webapps/ROOT/index.jsp <html> <title>Tomcat-1</title> <body> Hello Boy! from Tomcat-1. </body> </html>
部署ins02
[root@localhost tomcat]# cat tomcat-ins02/webapps/ROOT/index.jsp <html> <title>Tomcat-1</title> <body> Hello Boy! from Tomcat-2. </body> </html>