Tomcat单机多实例
准备:一台CentOS7机器即可,配置中关闭防火墙,或开放防火墙相应端口
实验效果图:
1、环境首选tomcat的Java环境
操作实验用的是tomcat8.5.35版本
实验的开始安装 jdk 包
rpm -ivh jdk-8u131-linux-x64_.rpm
rpm -qa|grep jdk
[root@ localhost ~]# rpm -qa|grep jdk jdk1.8.0_131-1.8.0_131-fcs.x86_64
2、解压tomcat包到指定目录,(实验目录选取 /opt/ )
tar -zxf apache-tomcat-8.5.35.tar.gz -C /opt/
由于实验单机多实例,提前把 tomcat1&2 相对文件copy
mv conf/ webapps/ temp/ logs/ work/ -t /opt/tomcat1
mv conf/ webapps/ temp/ logs/ work/ -t /opt/tomcat2
3、配置多实例的相对端口:
同一个服务器部署不同 Tomcat 要设置不同的端口,不然会报端口冲突,所以我们只需要修改conf/server.xml中的其中前三个端口就行了。
Server Port:该端口用于监听关闭tomcat的shutdown命令,默认为8005
Connector HTTP Port:该端口用于监听HTTP的请求,默认为8080
Connector AJP Port:该端口用于监听AJP( Apache JServ Protocol )协议上的请求,通常用于整合Apache Server等其他HTTP服务器,默认为8009
Redirect Port:重定向端口,出现在Connector配置中,如果该Connector仅支持非SSL的普通http请求,那么该端口会把 https 的请求转发到这个Redirect Port指定的端口,默认为8443;
去掉注释的配置文件如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <Server port="8005" shutdown="SHUTDOWN"> 3 <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> 4 <!-- Security listener. Documentation at /docs/config/listeners.html 5 <Listener className="org.apache.catalina.security.SecurityListener" /> 6 --> 7 <!--APR library loader. Documentation at /docs/apr.html --> 8 <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> 9 <!-- Prevent memory leaks due to use of particular java/javax APIs--> 10 <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> 11 <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> 12 <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> 13 14 <GlobalNamingResources> 15 <Resource name="UserDatabase" auth="Container" 16 type="org.apache.catalina.UserDatabase" 17 description="User database that can be updated and saved" 18 factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 19 pathname="conf/tomcat-users.xml" /> 20 </GlobalNamingResources> 21 22 <Service name="Catalina"> 23 <Connector port="8080" protocol="HTTP/1.1" 24 connectionTimeout="20000" 25 redirectPort="8443" /> 26 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 27 28 29 <Engine name="Catalina" defaultHost="localhost"> 30 31 <Realm className="org.apache.catalina.realm.LockOutRealm"> 32 <Realm className="org.apache.catalina.realm.UserDatabaseRealm" 33 resourceName="UserDatabase"/> 34 </Realm> 35 36 <Host name="localhost" appBase="webapps" 37 unpackWARs="true" autoDeploy="true"> 38 39 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 40 prefix="localhost_access_log" suffix=".txt" 41 pattern="%h %l %u %t "%r" %s %b" /> 42 43 </Host> 44 </Engine> 45 </Service> 46 </Server>
4、编辑tomcat启动脚本
1 #!/bin/bash 2 export CATALINA_HOME=/opt/apache-tomcat-8.5.35 3 export CATALINA_BASE=${1%/} 4 5 echo $CATALINA_BASE 6 7 TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2}'` 8 9 10 if [ -n "$TOMCAT_ID" ] ; then 11 echo "tomcat(${TOMCAT_ITOMCAT_ID}) still running now , please shutdown it firest"; 12 exit 2; 13 fi 14 15 TOMCAT_START_LOG=`$CATALINA_HOME/bin/startup.sh` 16 17 18 if [ "$?" = "0" ]; then 19 echo "$0 ${1%/} start succeed" 20 else 21 echo "$0 ${1%/} start failed" 22 echo $TOMCAT_START_LOG 23 fi
5、编辑tomcat关闭脚本,优化可以做一个重启脚本
1 [root@ localhost ~]# cat /opt/tomcat_shell/stop_tomcat.sh 2 #!/bin/bash 3 export CATALINA_HOME=/software/apache-tomcat-8.5.35 4 export CATALINA_BASE=${1%/} 5 6 echo $CATALINA_BASE 7 8 TOMCAT_ID=`ps aux |grep "java"|grep "[D]catalina.base=$CATALINA_BASE "|awk '{ print $2}'` 9 10 if [ -n "$TOMCAT_ID" ] ; then 11 TOMCAT_STOP_LOG=`$CATALINA_HOME/bin/shutdown.sh` 12 else 13 echo "Tomcat instance not found : ${1%/}" 14 exit 15 16 fi 17 18 19 if [ "$?" = "0" ]; then 20 echo "$0 ${1%/} stop succeed" 21 else 22 echo "$0 ${1%/} stop failed" 23 echo $TOMCAT_STOP_LOG 24 fi
6、进入tomcat脚本目录,进行tomcat的启动
./start_tomcat.sh /opt/tomcat1
./start_tomcat.sh /opt/tomcat2
[root@ localhost opt]# cd tomcat_shell/ [root@ localhost tomcat_shell]# ls start_tomcat.sh stop_tomcat.sh [root@ localhost tomcat_shell]# ./start_tomcat.sh /opt/tomcat1 /opt/tomcat1 ./start_tomcat.sh /opt/tomcat1 start succeed [root@ localhost tomcat_shell]# ./start_tomcat.sh /opt/tomcat2 /opt/tomcat2 ./start_tomcat.sh /opt/tomcat2 start succeed
实验完毕
注意:
tomcat启动较慢,等待时间稍微较长,会显示网页无法正常运行属于服务启动ing。