Saturday, January 2, 2010

Simple Apache 2 Tomcat 5 mod_jk Integration

Let’s start with installing Apache2 and mod_jk. On Debian
#apt-get install libapache2-mod-jk

Installing mod_jk this way will ensure that the mod_jk module is enabled for Apache2. You can check and verify that with
#ls -l /etc/apache2/mods-enabled/jk.load
if output like this:
lrwxrwxrwx 1 root root 25 2009-08-12 14:18 /etc/apache2/mods-enabled/jk.load -> ../mods-available/jk.load

In case the module is missing from the monds-enabled directory, you can enable it with
#a2enmod jk


Now create the workers.properties file in your Apache2 root directory.
#touch /etc/apache2/workers.properties

Next, open the workers.properties file and add the following

workers.tomcat_home=/usr/lib/apache-tomcat
workers.java_home=/usr/lib/jdk
ps=/
worker.list=worker1

worker.default.port=8009
worker.default.host=localhost
worker.default.type=ajp13
worker.default.lbfactor=1

Save and close the file.

Now we need to open the /etc/apache2/apache2.conf file and add the following lines at the bottom.

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so

# Where to find workers.properties
JkWorkersFile /etc/apache2/workers.properties

# Where to put jk logs
JkLogFile /var/log/apache2/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"


# Send servlet for context / jsp-examples to worker named worker1
JkMount /jsp-examples worker1
# Send JSPs for context /jsp-examples/* to worker named worker1
JkMount /jsp-examples/* worker1


Save and close the file.
Now stop and start Tomcat:
#cd ~/tomcat/bin
#./shutdown.sh
#./startup.sh

And restart Apache:
#/etc/init.d/apache2 restart
You are done.

Testing:
In this test, we are directing all URLs that begin with "/jsp-examples" to Tomcat.
In a real world situation, we might only direct JSPs or servlets to the JK worker.

Make sure no other server is running on the default Tomcat ports of 8005, 8009 and 8080.
Make sure no other server is running on the Apache port, which is normally 8080 or 80.

Start Tomcat first: Always start Tomcat first and then start Apache.
If you have to bounce Tomcat, remember to take down Apache first and restart it after Tomcat restarts.
Start Apache: Point your browser to http://localhost and verify that you get the default Apache page. Substitute "localhost" for the actual machine name/IP if necessary.

Point your browser to http://localhost:8080 and verify that you get the default Tomcat page.

Point your browser to http://localhost/jsp-examples/ and verify that you get the index page for the Tomcat examples.

This will be served by Apache and will indicate that you have completed your integration of Apache and Tomcat successfully.

No comments:

Post a Comment