Tomcat 配置
# 03.Tomcat 配置
接下来我们来讲讲简单的配置 Tomcat。
# Tomcat 后台管理
默认情况下,我们安装 Tomcat 之后,访问 Tomcat 主页面,出现如下图所示界面:
当我们需要访问 Manager App 或者 Host Manager 时:
提示需要密码:
不输入会提示出现如下所示的 HTTP 401 错误:
# 增加用户
401 表示无权限。根据提示,我们需要在 Tomcat 安装目录下的 conf/tomcat-users.xml 配置文件中增加用户角色和用户。
另外,还需要修改 webapps/manager/META-INF/context.xml
配置文件,修改访问地址的限制。
一、增加用户角色和用户:conf/tomcat-users.xml,我们在 <tomcat-users>
标签体里增加如下角色和一个 admin
用户:
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<role rolename="manager-jmx"/>
<role rolename="manager-script"/>
<role rolename="manager-status"/>
<user username="admin" password="123456" roles="admin-gui,manager-gui,manager-jmx,
manager-script,manager-status"/>
1
2
3
4
5
6
7
2
3
4
5
6
7
二、修改地址访问限制:webapps/manager/META-INF/context.xml
修改前,只允许本机访问(第 22 行里配置了只允许 127.0.0.1 访问,也就是本机)
<?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.
-->
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
更改为允许任何 IP 访问:
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="^.*$" />
</Context>
1
2
3
4
2
3
4
更改配置之后,重启 Tomcat,访问 Manager App 和 Host Manager 就没有问题了。登录 Manager App 后:
Host Manager:
# server.xml 其他配置
server.xml 文件当中可配置很多东西。
例如配置端口号(大概第 69 行处):
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
1
2
3
4
2
3
4
配置域名:修改 localhost 标签里的 name 属性(151 行处左右)
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
1
2
2
——未完待续——
上次更新: 2024/9/4 21:34:04