Using ajp to forward requests to Tomcat from Apache

Since there are limited tcp ports on a computer, and we wanted apache (php) and tomcat (jsp) on the same development machine, it is necessary to share ports. To do this, we get apache to listen on the standard port 80, and configure it to forward any requests to certain directories to Tomcat.

It will use mod_jk, and create a tcp connection between apache and tomcat, and forward the request using ajp protocol.

Following are notes on how I installed it. Apache had already been compiled and was running nicely.

Install mod_jk into apache

Go to your closest apache distribution, inside the jakarta/tomcat-connectors/jk2/ directory get the source and unpack it.

You will have to specify where existing apache source code is.

$ cd jk/native
$ ./
$ ./configure --with-apache=/home/compile/apache_1.3.29
$ make
$ make install

This will install some stuff into the target apache source tree. Reconfigure and compile apache again, then install:

$ cd /home/compile/apache_1.3.29
$ ./config.status --activate-module=src/modules/jk/libjk.a \
                  --enable-module=dir \
$ make
$ make install

Restart apache and check that your old setup is still working


Read up on what a worker is. In our case it is a tomcat instance that will do some work (by processing certain requests) on behalf of the web server.

bash-2.05a$ pwd
bash-2.05a$ cat



You have to add some lines to the apache conf now.

JkWorkersFile /usr/local/apache/conf/
JkLogFile /usr/local/apache/logs/mod_jk.log
JkLogLevel debug
# 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 /examples to worker named worker1
JkMount /examples/* aelst

Restart the server

You can now restart apache and test that request urls under /examples are forwarded to tomcat.

Published: Saturday, 15 May 2004