为了多个应用互不影响,一个tomcat实例只会部署一个应用,而该应用可部署到多个tomcat实例上负载,对应多个tomcat实例可以在一个或多个主机上

运行tomcat有两个重要目录

CATALINA_HOME指向安装目录,存放启动实例通用的lib和可执行文件

CATALINA_BASE指向活跃的静态配置路径,可自定义

默认这两个目录都是指向的安装目录,将这两个目录分开可方便升级且不同实例的配置互不影响,CATALINA_HOME只需包含bin和lib,其余的移动到一个新建的目录然后让CATALINA_BASE指向这个目录即可

应用配置

  • 新建$CATALINA_HOME/conf/Catalina/localhost/ROOT.xml,指明应用目录和应用对应的URI,因为一个实例只部署一个应用所以为空串。如果多个实例启动同一个应用,docBase指向相同目录即可,无需拷贝
<?xml version="1.0" encoding="UTF-8"?>
<Context path="" docBase="/export/App/www.6zeroq.com"  >
</Context>
  • 实例配置,修改server.xml启动和停止端口
<?xml version="1.0" encoding="UTF-8"?>
<Server port="9001" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />


  <Service name="Catalina">
          <Connector port="8001" protocol="HTTP/1.1" address="127.0.0.1"
                        maxThreads="20" minSpareThreads="5" acceptCount="20"
                        enableLookups="false"
                        connectionTimeout="20000" disableUploadTimeout="false" connectionUploadTimeout="60000" 
                        URIEncoding="utf-8" useBodyEncodingForURI="true"/>

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

          <Host name="localhost"  appBase="webapps"
                        unpackWARs="false" autoDeploy="false">

                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                        prefix="localhost_access_log" suffix=".txt"
                        pattern="%{X-Forwarded-For}i %l %u %t "%r" %s %b" />
                <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" />
          </Host>
        </Engine>
  </Service>
</Server>
  • 启动和停止脚本,停止脚本类似,传入的config参数表示实例的配置文件路径
export CATALINA_HOME=/usr/bin/tomcathome
export CATALINA_BASE=/export/Domains/www.6zeroq.com/server1
export CATALINA_OPTS="-server -Xms32m -Xmx192m -XX:MaxMetaspaceSize=64m -Djava.awt.headless=true"
#export CATALINA_PID=$CATALINA_BASE/bin/instance.pid

$CATALINA_HOME/bin/startup.sh -config $CATALINA_BASE/conf/server.xml

  • 扩展阅读

https://tomcat.apache.org/tomcat-8.5-doc/RUNNING.txt