Thursday, October 21, 2010

Configuring Apache2.2 with Tomcat 6.0.x on Linux with mod_proxy_ajp

This article should describe how to use the HTTPD Server Apache 2.2 to handle all request on port 80 and in certain cases (depending on the requested url) pass on the request to the Apache Tomcat 6 Server (6.0.x). So your Apache can handle requests for your static pages and for example php projects and your tomcat handles java web applications in for example .war-files.

How to let Apache 2 forward requests for different URLs to different folders is described in a former article.

These setting were used and tested in Linux Debian 5 Lenny. They should also work in other distributions like RedHad, Suse and Ubuntu, as well as Windows and Mac OS systems.

Make sure mod_proxy_ajp is installed

Look into /etc/apache2/mods-available for proxy.load and proxy_ajp.load to make sure the according mods are available.
Look into /etc/apache2/mods-enabled for proxy.load and proxy_ajp.load to make sure both mods are enabled.

From Apache2.2 this should normally be all set already, if not read here on how to enable mod_proxy_ajp.

Apache2 configuration

As described in a former post I use virtual host to handle requests for different URLs and forward them to certain folders in the /var/www folder. To forward requests to a certain URL to tomcat can also be done by creating a virtual host.

ProxyPass and ProxyPassReverse are classic reverse proxy directives used to forward the stream to another location. ajp://… is the AJP connector location (your tomcat’s server host/port)

The changes have to be done in the sites-enabled/000-default file in the apache2 home folder. Use for example nano or vim to edit the file and at below block.
nano /etc/apache2/sites-enabled/000-default
#Virtual Host setup
<VirtualHost *:80>
   ServerAlias *
   DocumentRoot "/usr/local/tomcat/webapps/yourapp"
   DirectoryIndex index.html
   <Proxy *>
      AddDefaultCharset Off
      Order deny,allow
      Allow from All
   ProxyPass / ajp://localhost:8009/
   ProxyPassReverse / ajp://localhost:8009/

Tomcat configuration

In Tomcat 6 we have to do some changes as well. All changes are done in the server.xml file.
nano /usr/local/tomcat/conf/server.xml
The first line might already be in your file. Please check this and in case only add the missing parameters, like enableLookups="false". This line will enable the AJP connections to the 8009 port of your tomcat 6 server.
<Connector enableLookups="false" port="8009" protocol="AJP/1.3" redirectPort="8443" />
Add this inbetween the tags:

<Host name="" debug="0" appBase="/usr/local/tomcat/webapps"
 unpackWARs="true" autoDeploy="true">
   <Context path= "" docBase="/usr/local/tomcat/webapps/yourapp" debug=”1″/>
   <Valve className=”org.apache.catalina.valves.AccessLogValve”
    directory=”logs” prefix=”home_access_log.” suffix=”.txt”
    pattern=”common” resolveHosts=”false”/>

Now if you point your domain( to the IP of the server Apache should do the rest and Tomcat should serve you the web app.

Restart Apache and Tomcat

Restart Apache:
/etc/init.d/apache2 restart
Restart Tomcat:
/etc/init.d/tomcat restart


Post a Comment