Apache Webserver and Tomcat Virtual Hosts


published by Edmund Haselwanter on Monday, August 25, 2008
Problem description: You want to server multiple web applications with one Tomcat instance. The brute force variant is to just "rewrite" or ProxyPass the request from e.g. www.myhost.com to the local tomcat with localhost:8080/myhost. But this leads to context urls of ressources in the served html pages. You can either deploy you webapp in the ROOT context (but there is just ONE ROOT context, so this does not work). So let us use virtual hosts on both, the apache webserver and the tomcat servlet container.

create a app location in $CATALINA_HOME
mkdir www.your-server-name.net

deploy the webapp in ROOT context

edit server.xml according
http://confluence.atlassian.com/display/DOC/Guide+to+using+Apache+Tomcat%27s+Virtual+Hosts
http://tomcat.apache.org/tomcat-6.0-doc/

in Tomcat server.xml

<Engine name="Catalina" defaultHost="www.internal.your-server-name.net">
     
<Host name="www.internal.your-server-name.net" appBase="www.your-server-name.net">
     
</Host>
</Engine>

edit Apache config according:

In Apache conf
http://confluence.atlassian.com/display/DOC/Using+Apache+with+virtual+hosts+and+mod_proxy

<VirtualHost *>
   
ServerName
www.your-server-name.net
      
ProxyRequests Off
   
<Proxy *>
       
Order deny,allow
       
Allow from all
   
</Proxy>
    ProxyPass / http://www.internal.your-server-name.net:8080/
   
ProxyPassReverse / http://www.internal.your-server-name.net:8080/
   
<Location />
       
Order allow,deny
       
Allow from all
   
</Location>
</VirtualHost>

and in /etc/hosts

127.0.0.1   localhost  www.internal.your-server-name.net

restart tomcat
restart apache

that's it :-)