34人参与 • 2025-07-28 • ar
apache tomcat提供了一个基于web的管理界面,允许管理员通过图形化界面部署、管理web应用程序。这个功能主要通过两个web应用实现:
我们将重点讲解如何通过manager app部署war包。
编辑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>
安全提示:生产环境中应使用强密码,并定期更换。
编辑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地址。
创建一个简单的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 ..
# 查看war包内容 jar -tf myapp.war
预期输出:
index.html web-inf/ web-inf/web.xml web-inf/classes/
# linux/mac bin/startup.sh # windows bin\startup.bat
使用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"
# 复制war文件到webapps目录 cp myapp.war $catalina_home/webapps/ # tomcat会自动解压并部署 # 或者创建自动部署目录 mkdir $catalina_home/webapps/myapp cp myapp.war $catalina_home/webapps/myapp/myapp.war
# 通过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
打开浏览器访问:http://localhost:8080/myapp/
应该能看到"hello from my test application!"页面。
查看tomcat日志确认部署过程:
# linux/mac tail -f $catalina_home/logs/catalina.out # windows type %catalina_home%\logs\catalina.out
原因:ip地址不在允许列表中
解决方案:
<!-- 修改webapps/manager/meta-inf/context.xml --> <valve classname="org.apache.catalina.valves.remoteaddrvalve" allow="127\.0\.0\.1|::1|your_ip|another_ip" />
原因:用户名或密码错误
解决方案:
conf/tomcat-users.xml
中的用户名和密码manager-gui
已分配给用户解决方案:
# 增加jvm内存 export catalina_opts="-xms512m -xmx1024m -xx:maxpermsize=256m" # 然后启动tomcat
检查项:
web.xml
是否符合规范catalina.out
日志中的具体错误信息<!-- 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
权限)manager-gui
权限)如果不需要host manager,可以删除或重命名:
mv webapps/host-manager webapps/host-manager.bak
<!-- 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
通过不同的上下文路径实现灰度发布:
# 部署新版本到新路径 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"
对于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"
创建监控脚本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
#!/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包的完整流程:
以上就是在tomcat服务器上部署war包的完整流程的详细内容,更多关于tomcat部署war包的资料请关注代码网其它相关文章!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论