背景
眾所周知,我們平時將JavaEE項目開發完后,需要將項目部署到服務器的tomcat上。常用的部署方式是將項目打包成war包放到tomcat的webapps下,然后重啟tomcat,然后通過ip地址+端口號訪問。這樣部署本身是沒問題的,但問題在于,如果還是在生產環境下的話,需要頻繁的更改優化項目,那么就需要頻繁的將項目打war包,替換webapps下的war包,操作繁瑣。
接下來我們講述如何實現本地編程,然后部署項目到遠程服務器的tomcat上,實現熱部署。
所用技術&工具
maven(項目構建和依賴管理) tomcat7插件 (部署到tomcat的插件) tomcat服務器 (web服務器) 編譯器推薦使用IDEA1.確保本地具有遠程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 DeniedYou 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.
新聞熱點
疑難解答