天天看點

Ubuntu為Tomcat啟用80端口

Ubuntu為Tomcat啟用80端口

一、tomcat部署調試

Update the package repository

sudo apt-get update
sudo apt-get upgrade --show-upgrades       

Install the authbind package

sudo apt-get install authbind      

This package allows non-root users to bind to ports below 1024. Install the tomcat7 package

sudo apt-get install tomcat7      

The tomcat7-docs, tomcat7-examples,and tomcat7-admin packages aren't required to deploy and maintain a web application. These packages can be skipped unless they are truly needed.

A new user "tomcat7" with primary group "tomcat7" will be created.

Enable authbind

Open a text editor like vim and load the default tomcat configuration file

nano /etc/default/tomcat7      

Remove the comment hash "#" in front of the authbind property and set the value to "yes"

AUTHBIND=yes      

Create authbind bind permission files for ports 80 and 443. Authbind will loop through the directory structure and try to find a byPort file referencing the port number attempting to be bound. Authorization will be granted if the user attempting to bind has execute permission.

touch /etc/authbind/byport/80
touch /etc/authbind/byport/443
chmod 0755 /etc/authbind/byport/80
chmod 0755 /etc/authbind/byport/443
chown tomcat7:tomcat7 /etc/authbind/byport/80
chown tomcat7:tomcat7 /etc/authbind/byport/443      

Change the Tomcat Port from 8080/8443 to 80/443

Open a text editor like vim and load the server configuration file

nano /etc/tomcat7/server.xml      

Find the connector for port 8080 to port 80 and the redirect port from 8443 to 443:

<Connector port="80" protocol="HTTP/1.1" 
connectiontimeout="20000" uriencoding="UTF-8" 
redirectport="443">
</Connector>      

Restart the Tomcat service

sudo service tomcat7 restart      

View the catalina.out log after restart and look for any errors. If you see permission denied errors, then you may have missed a step like: 

Forgetting to uncomment the AuthBind setting and putting it to "yes"

Forgetting to restart the tomcat7 service as sudo

最後運作netstat -ant,檢視80端口是否起來 。

二、應用部署

tomcat配置成功後需要将開發的應用部署到tomcat伺服器中。步驟如下:

1、将應用打包為ROOT.war

2、在/usr/share/tomcat7中建立logs目錄

cd /usr/share/tomcat7
sudo mkdir logs
sudo chmod 755 logs      

3、建立ROOT.xml檔案内容如下:

<!-- The contents of this file will be loaded for each web application -->


<Context path="" docBase="/usr/share/tomcat7/ROOT.war" displayName="Tomcat_1" reload="false" 
        debug="0" privileged="true">

    <!-- Default set of monitored resources -->

    <WatchedResource>WEB-INF/web.xml</WatchedResource>

</Context>      

   其中 docBase="/usr/share/tomcat7/ROOT.war"指向應用部署所在檔案位址。

4、 重新開機tomcat

sudo service tomcat7 restart      

 提示資訊如下:

* Stopping Tomcat servlet engine tomcat7
   ...done.
 * Starting Tomcat servlet engine tomcat7
   ...done.
      

 啟動完畢後登陸應用位址檢視。如還不成功請參考上述步驟修改。

繼續閱讀