1. 程式人生 > >Tomcat 7.0多埠配置

Tomcat 7.0多埠配置

修改tomcat/conf目錄裡面server.xml檔案

例如下面這樣新增一個8081埠,設定下appBase目錄,這樣就可以用一個tomcat監聽多個埠,每個埠都可以放應用了。我這樣新增下面這個配置以後,tomcat就監聽了2個埠(8080,8081)

webapps目錄和webapps1目錄的應用都會啟動,可以根據不同的埠進行訪問裡面的應用

[html] view plain copy print?
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <Serverport="8005"shutdown="SHUTDOWN">
  3.   <Listener
    className="org.apache.catalina.startup.VersionLoggerListener"/>
  4.   <ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on"/>
  5.   <ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  6.   <ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"
    />
  7.   <ListenerclassName="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
  8.   <GlobalNamingResources>
  9.     <Resourcename="UserDatabase"auth="Container"
  10.               type="org.apache.catalina.UserDatabase"
  11.               description="User database that can be updated and saved"
  12.               factory
    ="org.apache.catalina.users.MemoryUserDatabaseFactory"
  13.               pathname="conf/tomcat-users.xml"/>
  14.   </GlobalNamingResources>
  15.   <Servicename="Catalina">
  16.     <Connectorport="8080"protocol="HTTP/1.1"
  17.                connectionTimeout="20000"
  18.                redirectPort="8443"/>
  19.     <Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"/>
  20.     <Enginename="Catalina"defaultHost="localhost">
  21.       <RealmclassName="org.apache.catalina.realm.LockOutRealm">
  22.         <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
  23.                resourceName="UserDatabase"/>
  24.       </Realm>
  25.       <Hostname="localhost"appBase="webapps"
  26.             unpackWARs="true"autoDeploy="true">
  27.         <ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"
  28.                prefix="localhost_access_log"suffix=".txt"
  29.                pattern="%h %l %u %t "%r" %s %b" />
  30.       </Host>
  31.     </Engine>
  32.   </Service>
  33.   <Servicename="Catalina2">
  34.     <Connectorport="8081"protocol="HTTP/1.1"
  35.                connectionTimeout="20000"
  36.                redirectPort="8445"/>
  37.     <Connectorport="8010"protocol="AJP/1.3"redirectPort="8445"/>
  38.     <Enginename="Catalina"defaultHost="localhost">
  39.       <RealmclassName="org.apache.catalina.realm.LockOutRealm">
  40.         <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
  41.                resourceName="UserDatabase"/>
  42.       </Realm>
  43.       <Hostname="localhost"appBase="webapps2"
  44.             unpackWARs="true"autoDeploy="true">
  45.         <ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"
  46.                prefix="localhost_access_log"suffix=".txt"
  47.                pattern="%h %l %u %t "%r" %s %b" />
  48.       </Host>
  49.     </Engine>
  50.   </Service>
  51. </Server>
<?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="8080" 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">
 
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      </Host>
    </Engine>
  </Service>
  
  <Service name="Catalina2">
 
    <Connector port="8081" 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="webapps2"
            unpackWARs="true" autoDeploy="true">
 
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

OK

把專案war包改名為ROOT放進appBase屬性指定的路徑下啟動服務即可。

附:Tomcat埠說明:

1. 採用記事本開啟Tomcat安裝目錄下的conf資料夾下的server.xml檔案。 
2. 在server.xml檔案中找到以下程式碼: 
(1)<connector port="8080"protocol="HTTP/1.1"connectionTimeout="20000" 
redirectPort="8443"/> 
此聯結器是負責建立HTTP連線。再通過瀏覽器訪問Tomcat伺服器的web應用時使用的就是這個埠,如果此埠被佔用可將其修改一個未被佔用的埠,例如 將上面程式碼中的port="8080"修改為port="8081",即可將Tomcat的預設埠設定為8081。在修改埠時,應避免與公用埠衝突。建議採用預設的8080埠,不要修改,除非8080埠被其他程式所佔用。 
(2) <Server port="8005" shutdown="SHUTDOWN"> 
8005埠是用來關閉Tomcat服務的埠,如果此埠被佔用,可在配置檔案中將8005改成其他埠 
(3)<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />    
 此聯結器負責和其他的HTTP伺服器建立連線,再把Tomcat與其他的HTTP服務整合時需要使用此聯結器。如果此埠被佔用,可在配置檔案中將8009改成其他埠 
(4)<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" 
maxThreads="150" scheme="https" secure="true" clientAuth="false" 
keystoreFile="D:\Tomcat6.0\conf\tomcat.key" keystorePass="123456" 
sslProtocol="TLS" /> 
此聯結器的作用是啟動HTTPS服務,一般的情況此段內容是被註釋掉的,只有開啟HTTPS服務時才會放開使用,如果此埠被佔用則修改8443埠為其他沒被佔用的埠,並且需要將(1)和(3)中的也要修改 
3.  修改成功後,為了使新設定的埠生效,還需要重新啟動Tomcat伺服器。