我們可能會有這種場景,一個tomcat想部署兩個web工程,說白了就是公用一個端口,那怎么辦呢?就是多站點部署,具體步驟如下(這里以linux平台舉例):
1)先修改server.xml(conf/server.xml)
1 <!--For clustering, please take a look at documentation at: 2 /docs/cluster-howto.html (simple how to) 3 /docs/config/cluster.html (reference documentation) --> 4 <!-- 5 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 6 --> 7 8 <!-- Use the LockOutRealm to prevent attempts to guess user passwords 9 via a brute-force attack --> 10 <Realm className="org.apache.catalina.realm.LockOutRealm"> 11 <!-- This Realm uses the UserDatabase configured in the global JNDI 12 resources under the key "UserDatabase". Any edits 13 that are performed against this UserDatabase are immediately 14 available for use by the Realm. --> 15 <Realm className="org.apache.catalina.realm.UserDatabaseRealm" 16 resourceName="UserDatabase"/> 17 </Realm> 18 19 <Host name="localhost" appBase="webapps" 20 unpackWARs="true" autoDeploy="true"> 21 22 <!-- SingleSignOn valve, share authentication between web applications 23 Documentation at: /docs/config/valve.html --> 24 <!-- 25 <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> 26 --> 27 28 <!-- Access log processes all example. 29 Documentation at: /docs/config/valve.html 30 Note: The pattern used is equivalent to using pattern="common" --> 31 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 32 prefix="localhost_access_log" suffix=".txt" 33 pattern="%h %l %u %t "%r" %s %b" /> 34 35 </Host> 36 <Host name="aa.com" appBase="/data/aa/apache-tomcat-8.5.8/webapps/aa" unpackWARs="true" autoDeploy="true"></Host> 37 <Host name="bb.com" appBase="/data/bb/apache-tomcat-8.5.8/webapps/bb" unpackWARs="true" autoDeploy="true"></Host> 38 </Engine>
ops:千萬記得19-35行記得保留,不然war包不會解壓,通過站點名或ip直接去訪問會404.
上面新增的兩個host就是兩個站點,aa和bb分別於不同的web工程。
2)去conf/Catalina下創建相應的站點目錄:
mkdir -p conf/Catalina/aa.com
mkdir -p conf/Catalina/bb.com
這里的aa和bb對應上面server.xml host的name屬性
3)建立域名綁定:
在/etc/hosts中加入域名映射
127.0.0.1 localhost
127.0.0.1 aa.com
127.0.0.1 bb.com
4)將war包上傳至server.xml中host標簽appBase對應的路徑
重啟ok,完畢!