How to run Apache Httpd and Tomcat on port 80 using mod proxy
This how to assumes you already have installed Apache Httpd and Apache Tomcat running on a Debian/Ubuntu compatible box. Sometimes is it useful to run Apache and Tomcat side by side running on the standard http port (80) in case you need to run other scripts than JSP pages or Servlets. There are several ways to do this. The tomcat project exposes them in the Connector Docs. But in this case we are going to use mod_proxy which is a general propose proxy module. The main idea is to setup a VirtualHost exclusive for tomcat on a given path of our site.
Ensure we have mod_proxy enabled
You need to enable 2 modules: proxy and proxy_http using a2enmod
root@server# a2enmod proxy
Module proxy installed; run /etc/init.d/apache2 force-reload to enable.
root@server# a2enmod proxy_http
Enabling proxy as a dependency
This module is already enabled!
Module proxy_http installed; run /etc/init.d/apache2 force-reload to enable.
Create your VirtualHost
root@server# cd /etc/apache2/sites-available
root@server# vi mysite.com
This is the content of mysite.com
<VirtualHost XXX.XXX.XXX.XXX:80>
ServerAdmin webmaster@localhost
ServerName http://www.mysite.com/
DocumentRoot /home/httpd/publics/mysite.com/
ErrorLog /var/log/apache2/mysite.com-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/mysite.com-access.log combined
ServerSignature Off
<Directory /home/httpd/publics/mysite.com/>
Options FollowSymLinks
AllowOverride All
Options -MultiViews
</Directory>
</VirtualHost>
Now we need to enable this VirtualHost
root@server# a2ensite mysite.com
Site mysite.com installed; run /etc/init.d/apache2 force-reload to enable.
Add your Tomcat VirtualHost
Here lets assume we want to have mysite.com and tomcat.mysite.com where all tomcat request will go
root@server# cd /etc/apache2/sites-available
root@server# vi tomcat.mysite.com
This is the content of tomcat.mysite.com
<VirtualHost XXX.XXX.XXX.XXX:80>
ServerAdmin webmaster@localhost
ServerName tomcat.mysite.com
DocumentRoot /home/httpd/publics/tomcat.mysite.com/
ErrorLog /var/log/apache2/tomcat.mysite.com-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/tomcat.mysite.com-access.log combined
ServerSignature Off
# lets indicate the proxy what path do we want
# to forward to tomcat
ProxyPass / http://localhost:8080/
<Directory /home/httpd/publics/tomcat.mysite.com/>
Options FollowSymLinks
AllowOverride All
Options -MultiViews
</Directory>
</VirtualHost>
Now we need to enable this VirtualHost too
root@server# a2ensite tomcat.mysite.com
Site tomcat.mysite.com installed; run /etc/init.d/apache2 force-reload to enable.
Restart Apache
root@server# apache2ctl restart
The trick
The main trick of this configuration resides on the line
ProxyPass / http://localhost:8080/
Where we tell the proxy what path should be forwarded to Tomcat. In the same way we could modify this to our needs. Lets say we would like to have our path on /tomcat/ all we should do is:
ProxyPass /tomcat/ http://localhost:8080/
The advanced way
We may want to run both VirtualHost in the same configuration and only forward to tomcat requests starting on a given path
<VirtualHost XXX.XXX.XXX.XXX:80>
ServerAdmin webmaster@localhost
ServerName mysite.com
DocumentRoot /home/httpd/publics/mysite.com/
ErrorLog /var/log/apache2/mysite.com-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/mysite.com-access.log combined
ServerSignature Off
# lets indicate the proxy what path do we want
# to forward to tomcat
ProxyPass /java/ http://localhost:8080/
<Directory /home/httpd/publics/mysite.com/>
Options FollowSymLinks
AllowOverride All
Options -MultiViews
</Directory>
</VirtualHost>
With the above scheme all requests will be served by apache unless they start with /java/
Will be served by the PHP engine. But however:
Will be forwarded to Tomcat