项目组有好几个项目需要运行,之前项目少,一个tomcat对应一个项目还能应付,但现在项目多了,要是再一个tomcat对应一个项目的话,一方面看起来很业余,一方面也加大服务器的维护难度。所以现在需要对tomcat进行整理,用一个tomcat来启动多个应用实例,每个应用相互独立,互不干扰。 
准备工作:下载的apache-tomcat-7.0.26.zip,上传到linux服务器下/opt/apps/目录下。 

一、操作步骤 
1、解压apache-tomcat-7.0.26.zip。 
2、在apache-tomcat-7.0.26新建一个应用实例目录,如test,在test目录下创建三个目录:conf、temp和webapps. 
3、将原tomcat目录下的conf的server.xml、tomcat-user.xml和web.xml拷贝到test/conf/,将原tomcat目录下的webapps/manager拷贝到test/webapps. 
4、修改test/conf/server.xml.修改的地方如下: 

<Server port="${tomcat.server.port}" shutdown="SHUTDOWN">   
    
<Connector port="${tomcat.http.port}" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

<!-- 
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 
--> 

<Engine name="Catalina" defaultHost="localhost" jvmRoute="${tomcat.instance.name}"> 

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/opt/app/logs/${tomcat.instance.name}" prefix="localhost_access_log." suffix=".txt" pattern="%h %{x-forwarded-for}i %l %u %t "%r" %s %b" /> 
5、在/opt/apps/下新建logs/test目录 
6、在/opt/apps/下新建bin目录: 
创建test-start.sh文件,文件内容如下: 
#!/bin/bash 
export LD_LIBRARY_PATH="/opt/apps/apache-tomcat-7.0.26/bin/native" 
#export JAVA_HOME="/opt/apps/jdk1.7.0_05" 

export CATALINA_OPTS="-Dtomcat.server.port=8005 -Dtomcat.http.port=8010 -Dtomcat.instance.name=test -Xms1024m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError" 
export CATALINA_OUT="/opt/apps/logs/test/catalina.$(date +'%Y-%m-%d').out" 
export CATALINA_PID="/opt/apps/apache-tomcat-7.0.26/tomcat-test.pid" 
export CATALINA_BASE="/opt/apps/apache-tomcat-7.0.26/test" 

rm -rf /opt/apps/apache-tomcat-7.0.26/test/work 
/opt/apps/apache-tomcat-7.0.26/bin/catalina.sh start 

exit $? 

创建test-stop.sh文件,文件内容如下: 
#!/bin/bash 
export JAVA_HOME="/opt/apps/jdk1.7.0_05" 
export JAVA_OPTS="-Dtomcat.server.port=8005" 
export CATALINA_PID="/opt/apps/apache-tomcat-7.0.26/tomcat-test.pid" 
/opt/apps/apache-tomcat-7.0.26/bin/catalina.sh stop 0 -force 

7、将应用拷贝到apache-tomcat-7.0.26/test/webapps/,通过test-start.sh来启动应用实例,test-stop.sh来停止应用实例。 
8、如要再创建一个实例,重复进得2~7操作。 

二、遇到的问题: 
1、Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 
   1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 
   2. Shutdown the earlier cacheManager before creating new one with same name. 
解决方案:copy apache-tomcat-7.0.26/conf/web.xml到apache-tomcat-7.0.26/test/conf/ 

2、利用jenkins自动部署时,报错:Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8010/manager/text/list 
解决方案:copy apache-tomcat-7.0.26/webapps/manager到apache-tomcat-7.0.26/test/webapps/,同时需要在test/conf/tomcat-user.xml加上用户配置。