1. 程式人生 > >Lunix下tomcat通過配置server.xml配置檔案實現執行非webapps目錄下的專案

Lunix下tomcat通過配置server.xml配置檔案實現執行非webapps目錄下的專案

1、首先普及一個觀點:tomcat對於webapps下的專案會自動部署,不需要配置server.xml,但對於非webapps目錄下的專案則需要手動配置server.xml檔案,例如在搭建nginx伺服器均衡負載時,往往不會把專案放在webapps目錄下。

2、server.xml配置詳解

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

<!--

Licensed to the Apache Software Foundation (ASF) under one or more

contributor license agreements. See the NOTICE file distributed with

this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0

(the "License"); you may not use this file except in compliance with

the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

--><!-- Note: A "Server" is not itself a "Container", so you may not

define subcomponents such as "Valves" at this level.

Documentation at /docs/config/server.html

--><Server port="8005" shutdown="SHUTDOWN">

<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>

<!-- Security listener. Documentation at /docs/config/listeners.html

<Listener className="org.apache.caectalina.security.SecurityListener" />

-->

<!--APR library loader. Documentation at /docs/apr.html -->

<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>

<!-- Prevent memory leaks due to use of particular java/javax APIs-->

<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>

<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>

<!-- Global JNDI resources

Documentation at /docs/jndi-resources-howto.html

-->

<GlobalNamingResources>

<!-- Editable user database that can also be used by

UserDatabaseRealm to authenticate users

-->

<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>

</GlobalNamingResources>

<!-- A "Service" is a collection of one or more "Connectors" that share

a single "Container" Note: A "Service" is not itself a "Container",

so you may not define subcomponents such as "Valves" at this level.

Documentation at /docs/config/service.html

-->

<Service name="Catalina">

<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

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

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

<Realm className="org.apache.catalina.realm.LockOutRealm">

<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>

</Realm>

<!-- 訪問路徑:www.test.com.cn/test-one

www.test.com.cn/test-two

 -->

<Host appBase="webapps" autoDeploy="true" name="www.test.com.cn" unpackWARs="true">

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>

<Context docBase="D:\tomcat\webapps\test-one" path="/test-one" reloadable="true" allowLinking="true""/>

<Context docBase="D:\tomcat\webapps\test-two" path="/test-two" reloadable="true"allowLinking="true"/>

</Host>

</Engine>

</Service>

</Server>

主要配置上面配置檔案紅色的<Context>部分:

docBase:專案的全路徑

path:專案的訪問路徑

注意:

(一)、ppBase和docBase的區別

我們先看appBase,這個目錄表示:

1 這個目錄下面的子目錄將自動被部署為應用。

2 這個目錄下面的.war檔案將被自動解壓縮並部署為應用

而docBase只是指向了你某個應用的目錄,這個可以和appBase沒有任何關係。

(二)、如果是多個tomcat的負載均衡,那每個tomcat的server.xml都要做上述配置。

(三)、修改server.xml失敗,考慮是沒有修改許可權,輸入sudo chmod -R 777 /opt/Tomcat配置即可。