tanakawho
Photo credit: tanakawho
del.icio.us Digg DZone Reddit StumbleUpon
System Administration

Apache/Tomcat Integration

Let Apache serve static resources while Tomcat focuses on delivering servlet content.

Here's a step-by-step tutorial that shows how to integrate Tomcat 5.5.x with Apache 2.0.x under Windows XP using the AJP 1.3 connector. The exact versions I used for this tutorial were Tomcat 5.5.9 and Apache 2.0.54, but the instructions ought to work for Tomcat 5.5.x and Apache 2.0.x generally.

Preliminaries

Step 1: Decide which directory you want to use to deploy your web app. In a bit we will be pointing both Tomcat and Apache at this directory.

Configure Tomcat

Step 2: In your Tomcat server.xml file, make sure that your AJP 1.3 connector config is not commented out. Mine looks like this:

<Connector port="8009" 
    enableLookups="false"
    redirectPort="8443"
    protocol="AJP/1.3"/>

and it immediately follows the config for the HTTP connector running on port 8080 (Tomcat default). The AJP connector listens on port 8009 for requests that Apache forwards to Tomcat. The idea is that Apache services the requests for static content itself, and forwards requests requiring servlet/JSP processing. Incidentally, if you don't intend to use Tomcat as a standalone server, you should comment out the Coyote standalone connector that listens on port 8080.

Step 3: Also in server.xml, point the Catalina engine at the directory you just chose. In my own case, the config looks like this:

<Engine name="Catalina" defaultHost="localhost">
    <Host name="localhost" appBase="C:/cygwin/home/web/deploy"
          unpackWARs="true" autoDeploy="true"
          xmlValidation="false" xmlNamespaceAware="false">
         <Valve className="org.apache.catalina.valves.AccessLogValve"
                directory="C:/cygwin/home/web/logs" prefix="localhost_access_log."
                suffix=".txt" pattern="common" resolveHosts="false"/>
    </Host>
</Engine>

Configure Apache

Step 4: Download the mod_jk binary from the URL given in the References section below. The specific version I am using is a Windows binary, mod_jk-1.2.14-apache-2.0.54.so. mod_jk is the Apache module that allows Apache to talk to Tomcat.

Step 5: Copy the file you just downloaded into the Apache modules directory, but note that you need to rename it to mod_jk.so.

Step 6: Put a workers.properties file in the Apache configuration directory, right next to httpd.conf. Here is a sample workers.properties file; obviously you'll need to modify it to match your own local environment.

Step 7: Now modify httpd.conf to tell Apache about mod_jk and about our workers.properties file. Note that the worker name you choose here needs to match the worker name that you defined in workers.properties. Also, you will have to decide which requests get forwarded to Tomcat, and specify that here as JK mountpoints. Here's the configuration I'm using; modify as necessary:

# Load mod_jk module
# Update this path to match your modules location
LoadModule jk_module C:/apache-2.0.54/Apache2/modules/mod_jk.so

# Where to find workers.properties
# Update this path to match your conf directory location
# (put workers.properties next to httpd.conf)
JkWorkersFile C:/apache-2.0.54/Apache2/conf/workers.properties

# Where to put jk logs
# Update this path to match your logs directory location
# (put mod_jk.log next to access_log)
JkLogFile C:/cygwin/home/web/logs/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions indicate to send SSL KEY SIZE, 
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format 
JkRequestLogFormat "%w %V %T"

# Specify which requests get forwarded to Tomcat.  (My web app is called
# "mywebapp"; change as necessary.  Note that I'm forwarding Struts *.do
# requests to Tomcat, along with the top-level home page request, which in my
# case happens to be a Struts request.)
JkMount /mywebapp/ ajp13w
JkMount /mywebapp/*.do ajp13w
JkMount /mywebapp/*.jsp ajp13w

Step 8: Modify httpd.conf so that it obtains documents from the same directory you chose in step 1 above.

That should do it. If you access your web app on port 80, things should hopefully work the way you want them to. Important: do not try to access your web app on port 8080, especially if you commented out the Coyote connector in server.xml. The whole point of the above was to put Tomcat behind Apache, and now we have that. Client requests come in on port 80, and if there are requests whose URIs match the JkMount URIs specified in httpd.conf, then Apache forwards those to Tomcat on port 8009 via an AJP 1.3 request.

References

  • The AJP Connector: Explains what you need to do in server.xml if you want to get fancy with your AJP config.
Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon

Comments (5)

I would have a look at mod_proxy_ajp that works in a much more sane way than the old mod_jk.
By Stefan Reuter on Mar 19, 2008 at 8:46 AM PDT
Agreed, mod_proxy_ajp -- though this may depend on the versions of Apache httpd & Tomcat you're using.

A few months ago I was configuring a new deployment for Apache 2.2.3 and Tomcat 6, and based on my research mod_proxy_ajp was the way to go... capabilities and performance are basically the same, and the mod_rewrite approach is significantly simpler and less error-prone.

You set up your AJP connector similarly for Tomcat, then in your apache config you can use mod_rewrite to pass connections to the AJP proxy as needed; mine is something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} /(.*).jsp|.do(.*)
RewriteRule ^/(.*) ajp://127.0.0.1:8009/$1 [P]

I keep meaning to write a good walk-through in detail on my blog (because it was a real headache working it out with not much documentation out there to go on!), but haven't gotten around to it yet.
By Rob Whelan on Mar 19, 2008 at 10:56 AM PDT
It might be useful to point out that you may not want to front Tomcat with Apache at all, as Tomcat might be faster than Apache -
Quote from From Tomcat: The Definitive Guide, 2nd Edition
5.1.2.1.
"Tomcat's web server is faster than Apache httpd
Tomcat's web server is somewhat faster than Apache httpd. In our benchmarks, we observed that Tomcat is at least 23 percent faster than Apache httpd (standalone) at serving static content, at least on Linux. If you're serving mostly dynamic content, this is probably a big problem for you, as the Tomcat web server is faster at running most corporate web sites today. Companies with unusually heavy web traffic need to squeeze every last bit of performance out of their web server machines, and in these cases, Tomcat's performance beats Apache httpd's.

Everyone has their own requirements, experience, and competency, and those should also factor into the decision about which web server to use. There are good reasons to go either way."
By Chris Barham on Mar 19, 2008 at 6:23 PM PDT
That's an interesting point Chris. I picked up the book you mention after having read your post. While the relative performance of various products typically changes over time, your general point is valid--if performance matters, then it behooves one to do the benchmarking rather than to make assumptions.

I will point out that there may be other reasons to do an Apache/Tomcat integration. For instance you may want to do mod_proxy load balancing and/or failover (which, incidentally, the book you mention discusses in detail).

Finally, even if you are really just trying to get static and dynamic resources separated out, there are other approaches as well. For example you can deploy your static resources to a separate server farm and just have your app point to some URL such as http://static.mydomain.com/images/banner.png. In that scenario Apache delivers static content and you hit Tomcat directly for dynamic content. A little more complexity on the deployment side but it may be worth it to you. This gives you some additional flexibility around moving assets around (for example to cloud storage) since now it's just a DNS change.
By Willie Wheeler on Mar 22, 2008 at 10:40 AM PDT

Some custom written essay connecting with will be accomplished for people which would like to have some knowledge very hot texts connecting with home.

By GEMMA25 on Feb 20, 2010 at 2:10 PM PST

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Preview:
By You
Please help us reduce comment spam:
Spring in Practice
My brother and I are writing Spring in Practice for Manning!

What's New?

2009-08-30 - Check out my two-part series on DZone: Spring Integration: A Hands-On Tutorial.
2009-03-25 - My new article Getting Started with Spring Batch 2.0 is available on DZone.
Home | Consulting | Tech Articles | Mailing List | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.