Tomcat and Apache Web Server Integration with mod_proxy connector

Use case:

I have only one public IP assigned to my hosting/private cloud, and I want to host multiple websites on a single machine. Scenario as given below. Site can be simple html or php or JSP/Servlet (Java).

Tomcat Apache Integration

Prerequisites: Java, Tomcat, Apache, PHP installed.

Steps to integrate Tomcat and Apache:

  1. Enable mod_proxy, mod_proxy_http, mod_proxy_ajp
  2. Configure virtual host and rewrite modules

Step 1: Enable mod_proxy, mod_proxy_http, mod_proxy_ajp

mod_proxy_http and mod_proxy_ajp modules are available from Apache 2.2 versions. For demo purpose we shall be using both http and ajp modules. Based on module you can use either. Enable modules in httpd.conf file.

Step 2: Configure virtual host and rewrite modules

From following website names relate to scenarios mentioned in above diagram.

<VirtualHost *:80>

DocumentRoot “C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/simplehtml.com”
ServerName simplehtml.com
ServerAlias www.simplehtml.com
ErrorLog “logs/simplehtml.com-error.log”
CustomLog “logs/simplehtml.com-access.log” common
</VirtualHost>

<VirtualHost *:80>
DocumentRoot “C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/phpsite.net”
ServerName phpsite.net
ServerAlias www.phpsite.net
ErrorLog “logs/phpsite.net-error.log”
CustomLog “logs/phpsite.net-access.log” common
</VirtualHost>

<VirtualHost *:80>
ServerName webapp11.com
ServerAlias www.webapp11.com
ProxyRequests off
ErrorLog logs/webapp11.error.log
CustomLog logs/webapp11.log combined
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost Off
ProxyPass / http://localhost:7070/webapp11/
ProxyPassReverse / http://localhost:7070/webapp11/
</VirtualHost>
<VirtualHost *:80>
ServerName webapp21.com
ServerAlias www.webapp21.com
ProxyRequests off
ErrorLog logs/webapp21.error.log
CustomLog logs/webapp21.log combined
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost Off
ProxyPass / http://localhost:8080/webapp21/
ProxyPassReverse / http://localhost:8080/webapp21/
</VirtualHost>
<VirtualHost *:80>
ServerName webapp22.com
ServerAlias www.webapp22.com
ErrorLog logs/webapp22.error.log
CustomLog logs/webapp22.log combined
ProxyPass /webapp22 !
ProxyPass / ajp://localhost:8009/webapp22/
ProxyPassReverse / ajp://localhost:8009/webapp22/
RedirectMatch 301 ^/webapp22/(.*)$ /$1
RedirectMatch 301 ^/webapp22$ /
</VirtualHost>

Note
-> To test in your local machine, add domains to localhost mapping in the file C:\WINDOWS\system32\drivers\etc\hosts
      127.0.0.1   simplehtml.com
      127.0.0.1   phpsite.net
      127.0.0.1   webapp11.com
      127.0.0.1   webapp21.com
      127.0.0.1   webapp22.com
-> If DocumentRoot has been changed update Directory in httpd.conf accordingly
References:
http://wiki.apache.org/tomcat/FAQ/Connectors#Q2
http://rvdb.wordpress.com/2012/04/26/reverse-proxying-tomcat-webapps-behind-apache/

Leave a comment

Your email address will not be published. Required fields are marked *