Tomcat这篇是历史文章整理聚合,方便后续统一查阅
一、下载与启动
-
下载 Tomcat
前往官网下载:https://tomcat.apache.org/download-10.cgi
macOS 选择
tar.gz或zip包。

-
添加执行权限
进入 Tomcat 的
bin目录,执行:bashchmod +x startup.sh chmod +x shutdown.sh chmod +x catalina.sh # 或一键授权 chmod u+x *.sh -
启动与关闭
bashcd /Users/你的用户名/Library/ApacheTomcat/bin ./startup.sh # 启动 ./shutdown.sh # 关闭
二、常用配置
1. 修改tomcat密码
编辑 conf/tomcat-users.xml,在 <tomcat-users> 标签内添加:
xml
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="000000" roles="admin-gui,admin,manager-gui,manager,manager-script,manager-jmx,manager-status"/>
2. 修改端口号
编辑 conf/server.xml,找到以下配置修改端口:
xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxParameterCount="1000"
/>
三、常见问题解决
1. 安装tomcat权限报错403
报错类似:
bash
By default the Manager is only accessible from a browser running on the same machine
403 Access Denied
You are not authorized to view this page.
解决:
编辑 webapps/manager/META-INF/context.xml,注释掉 RemoteAddrValve 限制:
xml
<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="xxx"/>
</Context>

修改后重启 Tomcat。
2. 静态资源不可访问报错
表现:直接访问 webapps/files/xxx 目录失败。

解决:编辑 conf/web.xml,找到 default servlet,将:
xml
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
xml
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
修改后无需重启即可生效。
3. 静态资源整体异常、无法访问
若整体静态资源访问异常,可直接替换一份正确的 conf/web.xml 配置文件。
四、Tomcat 文件共享

将文件放在 webapps/files/ 等目录下,访问 http://localhost:8080/files/ 失败,
解决方法同上:修改 conf/web.xml 中 listings 为 true 即可实现目录浏览。
