当前位置 主页 > 服务器问题 > win服务器问题汇总 >

    SSM项目频繁打成war包部署,使用tomcat和maven实现热部署配置

    栏目:win服务器问题汇总 时间:2019-12-03 09:10

    背景

    众所周知,我们平时将JavaEE项目开发完后,需要将项目部署到服务器的tomcat上。常用的部署方式是将项目打包成war包放到tomcat的webapps下,然后重启tomcat,然后通过ip地址+端口号访问。这样部署本身是没问题的,但问题在于,如果还是在生产环境下的话,需要频繁的更改优化项目,那么就需要频繁的将项目打war包,替换webapps下的war包,操作繁琐。

    接下来我们讲述如何实现本地编程,然后部署项目到远程服务器的tomcat上,实现热部署。

    所用技术&工具

    maven(项目构建和依赖管理) tomcat7插件 (部署到tomcat的插件) tomcat服务器 (web服务器) 编译器推荐使用IDEA

    1.确保本地具有远程tomcat的使用权限

    修改Tomcat下{TOMCAT_HOME}conf/tomcat-users.xml配置文件,添加用户名、密码、权限。

    <role rolename="manager-gui" />
    <role rolename="manager-script" />
    <role rolename="admin-gui" />
    <role rolename="admin-script" />
    <user username="tomcat" password="tomcat" roles="manager-gui,manager-script,admin-gui,admin-script"/>

    2.配置Tomcat允许远程访问

    在远程服务器的{TOMCAT_HOME}conf/Catalina/localhost/目录下创建一个manager.xml文件,配置如下内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context privileged="true" antiResourceLocking="false" docBase="${catalina.home}/webapps/manager">
         <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
    </Context>

    注:如果只想部分用户使用,可在allow配置IP,例如

    allow="192.168.0.102"

    3.重启远程Tomcat

    在tomcat的bin目录下依次执行

    //关闭tomcat
    ./shutdown.sh
    //启动tomcat
    ./startup.sh

    4.测试是否具有使用权限

    访问tomcat,例如http://192.168.0.102:8080(使用自己的服务器或是虚拟机的ip地址)
    点击Manager APP


    在这里插入图片描述

    输入刚才配置的tomcat的账号和密码

    在这里插入图片描述

    如果跳转到这个页面证明配置完成

    在这里插入图片描述

    当然也可以在当前页面实现war的部署和替换,这也是另一种部署方式,不过依然没有热部署方便

    问题:如果出现403报错如下

    403 Access Denied
    
    You are not authorized to view this page.
     
    By default the Manager is only accessible from a browser running on the same machine as Tomcat. If you wish to modify this restriction, you'll need to edit the Manager's context.xml file.
     
    If you have already configured the Manager application to allow access and you have used your browsers back button, used a saved book-mark or similar then you may have triggered the cross-site request forgery (CSRF) protection that has been enabled for the HTML interface of the Manager application. You will need to reset this protection by returning to the main Manager page. Once you return to this page, you will be able to continue using the Manager application's HTML interface normally. If you continue to see this access denied message, check that you have the necessary permissions to access this application.
     
    If you have not changed any configuration files, please examine the file conf/tomcat-users.xml in your installation. That file must contain the credentials to let you use this webapp.