前言

本文将解决以下问题:

  • 如何将Linux下任意位置的项目(虚拟目录)部署到tomcat?
  • 如何将项目部署到服务器特定端口?
  • 如何在一个服务器上部署多个web应用?

本例中 系统:Linux version 3.10.0-514.el7.x86_64、tomcat版本:8.0.30.0、jdk 版本:jdk1.8.0

场景实例

将任意目录部署到tomcat

实现方法: 修改/增加server.xml文件中以下配置

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/root/tomcat/apache-tomcat-8.0.30/fantasy" debug="0"  privileged="true" reloadable="true"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

解释

  • ntext元素:每个Context元素代表了运行在虚拟主机上的单个Web应用
  • docBase 属性:自己web应用的路径,可以是相对路径也可以是绝对路径,相对路径是相对于host的webapps,绝对路径如上例。
  • path 属性:即要建立的虚拟目录,是tomcat访问这个应用的URL路径,如果为空则代表访问路径为web应用的根目录,访问路径:http://ip:port、如果非空则访问路径:http://ip:port/path
  • reloadable:如果这个属性设为true,Tomcat服务器在运行状态下会监视在WEB-INF/classesWeb-INF/lib目录CLASS文件的改变,如果监视到有class文件被更新,服务器自动重新加载Web应用,这样我们可以在不重启tomcat的情况下改变应用程序

将项目部署到tomcat任意端口

实现方法: 修改server.sml文件中Connector 的 port 属性,访问方式同上

<Connector port="2727" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8445" />

解释:

  • port 属性:指定web应用端口(暴露一个socket端口来accept客户端的链接)
  • protocol 属性:使用的网络协议,表示tomcat使用何种方式来接受和处理client端请求,HTTP/1.1是默认值,等效于org.apache.coyote.http11.Http11Protocol
  • connectionTimeout 属性:当client与tomcat建立连接之后,在”connectionTimeout”时间之内,仍然没有得到client的请求数据,此时连接将会被断开.此值的设定需要考虑到网络稳定型,同时也有性能的考虑.它和tcp的配置选项中的”socket_timeout”仍有区别,connectionTimeout只会在链接建立之后,得到client发送http-request信息前有效.

一个tomcat部署多个项目

实现方法: server.sml文件中增加service元素

<Service name="Catalina2">
    <Connector port="2727" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8445" />
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8445" />
    <Engine name="Catalina" defaultHost="localhost">
        <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
        </Realm>
        <Host name="localhost"  appBase="fantasy" unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/root/tomcat/apache-tomcat-8.0.30/fantasy" debug="0" privileged="true" reloadable="true"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        </Host>
    </Engine>
</Service>

解释:

完整配置

<?xml version="1.0" encoding="UTF-8"?>

<Server port="8005" 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" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/root/tomcat/apache-tomcat-8.0.30/webapps/ROOT" debug="0"  privileged="true" reloadable="true"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
   <Service name="Catalina2">
    <Connector port="2727" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8445" />
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8445" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="fantasy" unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/root/tomcat/apache-tomcat-8.0.30/fantasy" debug="0" privileged="true" reloadable="true"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>
03-22 18:49