Sunday, January 28, 2007

Using Apache as a Proxy for Tomcat Applications

I spent the day trying some configurations to make the integration between Apache HTTP Server and Tomcat.

The configuration that worked best for me basically was to setup a VirtualHost in Apache as a proxy with the same name as the application in Tomcat engine. As a resume, below tou will find some of the steps I went through and what I changed in the configuration files (I am using Apache 2.2 and Tomcat 5.5).

In Apache it is necessary to enable the mod_proxy Module and setup a NameVirtualHost. The httpd.conf (found in %APACHE_HOME%/conf) configuration file should be changed as below.

#Uncomment or add the lines below into the general session of your httpd.conf file.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

# Add the Virtual Host for your application at the end of the file
NameVirtualHost *

<VirtualHost *>
  ServerAdmin serveradmin@domain.com
  DocumentRoot "C:/java/Tomcat5.5/webapps/"
  ServerName myapplication.domain.com
  ServerAlias *.myapplication.domain.com

  ProxyPass /foo-app http://localhost:8080/foo-app
  ProxyPassReverse /foo-app http://localhost:8080/foo-app
</VirtualHost>

After setting this file in your apache server, restart it and proceed to configuring the Tomcat Server, by editing the server.xml file (found in %CATALINA_HOME%/conf). Simply add the lines below at the end of the file, but inside the <server> tag.

<Service name="Tomcat-Apache">
    
    <Engine name="Apache" defaultHost="myapplication.domain.com" debug="0">
    <Host name="myapplication.domain.com" debug="0"
            appBase="c:/java/Tomcat5.5/webapps/foo-app" 
            unpackWARs="true" autoDeploy="true">

        <Context path="/foo-app" docBase="c:/java/Tomcat5.5/webapps/foo-app" debug="1" reloadable="true"/>
    </Host>

    </Engine>

  </Service>  

Now, open your browser and type the URL http://myapplication.domain.com/foo-app and you should be redirected to your application after passing by Apache and Tomcat.

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.