一、tomcat管理后台介绍
apache tomcat提供了一个基于web的管理界面,允许管理员通过图形化界面部署、管理web应用程序。这个功能主要通过两个web应用实现:
- manager app:用于部署、启动、停止、重新加载和删除web应用程序
- host manager:用于管理虚拟主机
我们将重点讲解如何通过manager app部署war包。
二、环境准备
1. tomcat版本要求
- 建议使用tomcat 8.5.x、9.x或10.x版本
- 本文以tomcat 9.0.85为例进行演示
- 下载地址:https://tomcat.apache.org/download-90.cgi
2. 系统环境
- 操作系统:windows/linux/macos
- java版本:jdk 8或更高版本
- 内存:建议至少1gb可用内存
三、配置tomcat管理权限
1. 配置manager-gui角色和用户
编辑conf/tomcat-users.xml
文件,添加以下内容:
<tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://tomcat.apache.org/xml tomcat-users.xsd" version="1.0"> <!-- 添加管理角色 --> <role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <!-- 添加管理员用户 --> <user username="admin" password="strongpassw0rd!" roles="manager-gui,manager-script,manager-jmx,manager-status"/> </tomcat-users>
安全提示:生产环境中应使用强密码,并定期更换。
2. 配置访问限制(可选但推荐)
编辑webapps/manager/meta-inf/context.xml
文件,配置ip访问限制:
<?xml version="1.0" encoding="utf-8"?> <context antiresourcelocking="false" privileged="true" > <valve classname="org.apache.catalina.valves.remoteaddrvalve" allow="127\.0\.0\.1|::1|your_ip_address" /> <manager sessionattributevalueclassnamefilter="java\.lang\.(?:boolean|integer|long|number|string)|org\.apache\.catalina\.filters\.csrfpreventionfilter\$lrucache(?:\$1)?|java\.util\.(?:linked)?hashmap"/> </context>
将your_ip_address
替换为允许访问的ip地址。
四、准备war包
1. 创建测试war包
创建一个简单的web应用示例:
# 创建目录结构 mkdir -p myapp/web-inf/classes mkdir myapp/images # 创建web.xml cat > myapp/web-inf/web.xml << 'eof' <?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>my test application</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> eof # 创建index.html cat > myapp/index.html << 'eof' <!doctype html> <html> <head> <title>my test application</title> <meta charset="utf-8"> </head> <body> <h1>hello from my test application!</h1> <p>deployed successfully at: <span id="time"></span></p> <script> document.getelementbyid("time").innerhtml = new date().tostring(); </script> </body> </html> eof # 打包成war文件 cd myapp jar -cvf ../myapp.war * cd ..
2. 验证war包结构
# 查看war包内容 jar -tf myapp.war
预期输出:
index.html web-inf/ web-inf/web.xml web-inf/classes/
五、部署war包的三种方式
方式一:通过web界面部署(推荐用于学习)
- 启动tomcat服务器
# linux/mac bin/startup.sh # windows bin\startup.bat
- 访问管理界面
- 打开浏览器访问:http://localhost:8080/manager/html
- 使用前面配置的用户名和密码登录
- 部署war包
- 在"deploy"部分,选择"war file to deploy"
- 点击"choose file"选择你的war文件
- 点击"deploy"按钮
方式二:通过manager api部署(推荐用于自动化)
使用curl命令通过rest api部署:
# 部署war包 curl -u admin:strongpassw0rd! \ -t myapp.war \ "http://localhost:8080/manager/text/deploy?path=/myapp&update=true" # 检查部署状态 curl -u admin:strongpassw0rd! \ "http://localhost:8080/manager/text/list" # 重新加载应用(如果已存在) curl -u admin:strongpassw0rd! \ "http://localhost:8080/manager/text/reload?path=/myapp" # 停止应用 curl -u admin:strongpassw0rd! \ "http://localhost:8080/manager/text/stop?path=/myapp" # 启动应用 curl -u admin:strongpassw0rd! \ "http://localhost:8080/manager/text/start?path=/myapp" # 卸载应用 curl -u admin:strongpassw0rd! \ "http://localhost:8080/manager/text/undeploy?path=/myapp"
方式三:直接放置到webapps目录(最简单)
# 复制war文件到webapps目录 cp myapp.war $catalina_home/webapps/ # tomcat会自动解压并部署 # 或者创建自动部署目录 mkdir $catalina_home/webapps/myapp cp myapp.war $catalina_home/webapps/myapp/myapp.war
六、验证部署结果
1. 检查应用状态
# 通过manager api检查 curl -u admin:strongpassw0rd! "http://localhost:8080/manager/text/list"
输出示例:
ok - listed applications for virtual host localhost /myapp:running:0:myapp /manager:running:0:manager /host-manager:running:0:host-manager /root:running:0:root
2. 访问应用
打开浏览器访问:http://localhost:8080/myapp/
应该能看到"hello from my test application!"页面。
3. 检查日志
查看tomcat日志确认部署过程:
# linux/mac tail -f $catalina_home/logs/catalina.out # windows type %catalina_home%\logs\catalina.out
七、常见问题排查
1. 403 access denied错误
原因:ip地址不在允许列表中
解决方案:
<!-- 修改webapps/manager/meta-inf/context.xml --> <valve classname="org.apache.catalina.valves.remoteaddrvalve" allow="127\.0\.0\.1|::1|your_ip|another_ip" />
2. 401 unauthorized错误
原因:用户名或密码错误
解决方案:
- 检查
conf/tomcat-users.xml
中的用户名和密码 - 确认角色
manager-gui
已分配给用户
3. 部署失败:内存不足
解决方案:
# 增加jvm内存 export catalina_opts="-xms512m -xmx1024m -xx:maxpermsize=256m" # 然后启动tomcat
4. war包无法自动部署
检查项:
- 确认war包格式正确
- 检查
web.xml
是否符合规范 - 查看
catalina.out
日志中的具体错误信息
八、安全最佳实践
1. 生产环境安全配置
<!-- conf/tomcat-users.xml --> <tomcat-users> <role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="deploy-user" password="$2a$10$hashedpassword" roles="manager-script"/> <user username="admin-user" password="$2a$10$anotherhashed" roles="manager-gui"/> </tomcat-users>
原则:
- 部署使用专用用户(仅
manager-script
权限) - gui管理使用另一用户(
manager-gui
权限) - 使用强密码并定期更换
2. 禁用不必要的管理应用
如果不需要host manager,可以删除或重命名:
mv webapps/host-manager webapps/host-manager.bak
3. 配置https
<!-- conf/server.xml --> <connector port="8443" protocol="org.apache.coyote.http11.http11nioprotocol" maxthreads="150" sslenabled="true"> <sslhostconfig> <certificate certificatekeystorefile="conf/localhost-rsa.jks" type="rsa" /> </sslhostconfig> </connector>
生成证书:
keytool -genkeypair -alias localhost -keyalg rsa -keysize 2048 \ -keystore conf/localhost-rsa.jks -validity 3650
九、自动化部署脚本
创建一个自动化部署脚本deploy.sh
:
#!/bin/bash # 配置变量 tomcat_url="http://localhost:8080" username="admin" password="strongpassw0rd!" war_file="myapp.war" context_path="/myapp" # 函数:检查应用是否存在 check_app_exists() { local status=$(curl -s -u "$username:$password" \ "$tomcat_url/manager/text/list" | \ grep "$context_path" || echo "not_found") if [[ "$status" != "not_found" ]]; then echo "exists" else echo "not_exists" fi } # 函数:部署应用 deploy_app() { echo "deploying $war_file to $context_path..." local response=$(curl -s -u "$username:$password" \ -t "$war_file" \ "$tomcat_url/manager/text/deploy?path=$context_path&update=true") if [[ "$response" == *"ok"* ]]; then echo "deployment successful!" return 0 else echo "deployment failed: $response" return 1 fi } # 函数:检查部署状态 check_status() { echo "checking deployment status..." curl -u "$username:$password" "$tomcat_url/manager/text/list" | \ grep "$context_path" } # 主程序 main() { if [ ! -f "$war_file" ]; then echo "error: war file '$war_file' not found!" exit 1 fi local app_status=$(check_app_exists) if [ "$app_status" == "exists" ]; then echo "application already exists. will update..." else echo "application does not exist. will create new deployment..." fi if deploy_app; then echo "verifying deployment..." sleep 2 check_status echo "deployment completed successfully!" else echo "deployment failed!" exit 1 fi } # 执行主程序 main "$@"
使用方法:
chmod +x deploy.sh ./deploy.sh
十、高级部署技巧
1. 灰度发布
通过不同的上下文路径实现灰度发布:
# 部署新版本到新路径 curl -u admin:strongpassw0rd! \ -t myapp-v2.war \ "http://localhost:8080/manager/text/deploy?path=/myapp-new&update=true" # 测试新版本 curl http://localhost:8080/myapp-new # 如果测试通过,替换旧版本 curl -u admin:strongpassw0rd! \ "http://localhost:8080/manager/text/undeploy?path=/myapp" curl -u admin:strongpassw0rd! \ -t myapp-v2.war \ "http://localhost:8080/manager/text/deploy?path=/myapp&update=true"
2. 集群环境部署
对于tomcat集群,可以使用以下策略:
# 定义集群节点 nodes=("node1:8080" "node2:8080" "node3:8080") # 并行部署到所有节点 for node in "${nodes[@]}"; do curl -u admin:strongpassw0rd! \ -t myapp.war \ "http://$node/manager/text/deploy?path=/myapp&update=true" & done # 等待所有部署完成 wait echo "deployed to all cluster nodes"
十一、监控与维护
1. 监控脚本
创建监控脚本monitor.sh
:
#!/bin/bash tomcat_url="http://localhost:8080" app_path="/myapp" check_interval=60 # 检查间隔(秒) while true; do # 检查应用健康状态 http_code=$(curl -s -o /dev/null -w "%{http_code}" "$tomcat_url$app_path") if [ "$http_code" != "200" ]; then echo "$(date): application is down! http code: $http_code" # 可以添加告警通知,如发送邮件或短信 # send_alert "application is down" else echo "$(date): application is up" fi sleep $check_interval done
2. 自动化备份
#!/bin/bash # 备份已部署的应用 backup_dir="/opt/tomcat/backups" date=$(date +%y%m%d_%h%m%s) mkdir -p "$backup_dir" # 备份特定应用 cp "$catalina_home/webapps/myapp.war" "$backup_dir/myapp_$date.war" cp -r "$catalina_home/webapps/myapp" "$backup_dir/myapp_$date" # 保留最近7天的备份 find "$backup_dir" -name "myapp_*" -mtime +7 -delete
十二、总结
通过以上详细步骤,您已经掌握了tomcat后台部署war包的完整流程:
- 基础部署:通过web界面或api部署war包
- 安全配置:合理配置用户权限和访问控制
- 自动化:使用脚本实现一键部署和监控
- 生产实践:遵循安全最佳实践,确保系统稳定
以上就是在tomcat服务器上部署war包的完整流程的详细内容,更多关于tomcat部署war包的资料请关注代码网其它相关文章!
发表评论