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配置即可。