Apache和Tomcat共用80端口

在默认情况下,Apache服务器安装后使用的是80端口,而Tomcat默认使用的是8080端口,当一台服务器上二者都安装时,想要访问Tomcat提供的JSP页面,需要在域名后加上:8080,这样不太方便同时影响域名地址美观体验。下面介绍通过简单设置,使用80端口可以同时访问Apache提供的PHP和Tomcat提供的JSP。

说明:

  • 下文将假定已安装好Apache和Tomcat服务器
  • 示例域名使用的是http://he-kai.com
  • 主机为Linode VPS (Ubuntu 12.04 64-bit)

设置步骤

1. 启用proxy和proxy_http模块

终端(或putty、SSH等)中使用a2enmod开启proxy和proxy_http模块

sudo a2enmod proxy
sudo a2enmod proxy_http

2. Apache增加虚拟主机并指向Tomcat服务器

假设Apache中已有http://he-kai.com,Tomcat中已有http://he-kai.com:8080

现增加一个http://tomcat.he-kai.com的虚拟主机,并设置别名为http://t.he-kai.com方便用户输入

sudo vi /etc/apache2/sites-available/tomcat.he-kai.com.conf

按下i或Insert键进入输入模式,将如下内容复制进去(请根据实际情况更改)

# domain: tomcat.he-kai.com
# public: /home/hekai/public/tomcat.he-kai.com/

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin [email protected]
  ServerName  tomcat.he-kai.com
  ServerAlias t.he-kai.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php index.jsp
  DocumentRoot /home/hekai/public/tomcat.he-kai.com/public

  ProxyPass / http://he-kai.com:8080/
  ProxyPassReverse / http://he-kai.com:8080/

  # Log file locations
  LogLevel warn
  ErrorLog  /home/hekai/public/tomcat.he-kai.com/log/error.log
  CustomLog /home/hekai/public/tomcat.he-kai.com/log/access.log combined
</VirtualHost>

最后使用a2ensite启用此虚拟主机并重启apache2服务器

sudo a2ensite tomcat.he-kai.com.conf
sudo service apache2 restart

也可以使用reload而不重启apache2服务器

到此,可以使用http://tomcat.he-kai.com或者http://t.he-kai.com来访问之前http://he-kai.com:8080的内容了,用户在浏览器的地址栏中再也不会看到8080字样了。

注意事项:

设置的重点在于使用ProxyPass和ProxyPassReverse进行代理,若只设置ProxyPass,若访问http://t.he-kai.com/xxx和http://t.he-kai.com/xxx/的效果会不一样,最后带有反斜杠的地址显示正常,而不带反斜杠的会在地址栏中显示http://he-kai.com:8080/xxx

参考文献:

  1. https://library.linode.com/hosting-website
  2. https://sites.google.com/a/ci2s.com.ar/wiki/technics/how-to-run-apache-httpd-and-tomcat-on-port-80-using-mod-proxy

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注