Using ajp to forward requests to Tomcat from Apache

Published: Saturday, 15 May 2004

Since there are limited tcp ports on a server, 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
$ ./buildconf.sh
$ ./configure --with-apache=/home/compile/apache_1.3.29
$ make
$ make install

This will install the module 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 \
                  --disable-shared=dir
$ make
$ make install

Restart apache and check that your old setup is still working

Configuration

workers.properties

A worker, in our case, is a Tomcat instance that will do some work (by processing certain requests) on behalf of the web server.

bash-2.05a$ pwd
/usr/local/apache/conf
bash-2.05a$ cat workers.properties
workers.apache_log=/usr/local/apache/logs
workers.tomcat_home=/usr/local/tomcat
workers.java_home=/usr/local/java/jdk1.2.2/

worker.list=aelst
worker.aelst.type=ajp13
worker.aelst.host=localhost
worker.aelst.port=8009
worker.aelst.lbfactor=50
worker.aelst.cachesize=10
worker.aelst.cache_timeout=600
worker.aelst.socket_keepalive=1
worker.aelst.socket_timeout=300

httpd.conf

Add some lines to the apache conf.

JkWorkersFile /usr/local/apache/conf/workers.properties
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

Restart apache and test that request URLs under /examples are forwarded to tomcat.

Links