Tomcat 配置

本文最后更新于:2024年3月18日 凌晨

Tomcat 配置

路径

1
2
Tomcat home directory : /usr/share/tomcat
Tomcat base directory : /var/lib/tomcat或/etc/tomcat

配置虚拟主机

  • /var/lib/tomcat/conf/server.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<Server port="9638" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<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>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8085" protocol="HTTP/1.1" redirectPort="8490" />
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt" />
</Host>
<Host autoDeploy="true" name="tally.lushan.tech" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Context crossContext="true" docBase="/www/wwwroot/tally.lushan.tech" path="/" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt" />
</Host>
</Engine>
</Service>
</Server>
  • path:相对路径,相对于域名的路径。
  • docbase:表示工程包的物理绝对路径,具体到项目根目录。
  • reloadable:如果这个属性设为true,Tomcat服务器在运行状态下会监视在WEB-INF/classesWeb-INF/lib目录CLASS文件的改动。如果监视到有class文件被更新,服务器自重新加载Web应用。

配置管理员帐号

  • /var/lib/tomcat/conf/tomcat-users.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<tomcat-users>
<!--
NOTE: By default, no user is included in the "manager-gui" role required
to operate the "/manager/html" web application. If you wish to use this app
you must define such a user - the username and password are arbitrary.
-->
<!--
NOTE: The sample user and role entries below are wrapped in a comment
and thus are ignored when reading this file. Do not forget to remove
<!.. ..> that surrounds them.
-->
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
-->
<role rolename="manager-gui" />
<role rolename="manager-script" />
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script" />
</tomcat-users>
  • manager-gui:允许访问html接口(即URL路径为/manager/html/)
  • manager-script:允许访问纯文本接口(即URL路径为/manager/text/)
  • manager-jmx:允许访问JMX代理接口(即URL路径为/manager/jmxproxy/)
  • manager-status:允许访问Tomcat只读状态页面(即URL路径为/manager/status/)
  • 注意:从Tomcat Manager内部配置文件中可以得知,manager-gui,manager-script,manager-jmx均具备manager-status的权限,也就是说,manager-gui,manager-script,manager-jmx三种角色权限无需再额外添加manager-status权限,即可直接访问路径/manager/status/*

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!