Using ajp to forward requests from Apache to Tomcat
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
Section titled “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 installThis 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 installRestart apache and check that your old setup is still working
Configuration
Section titled “Configuration”workers.properties
Section titled “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.
workers.apache_log=/usr/local/apache/logsworkers.tomcat_home=/usr/local/tomcatworkers.java_home=/usr/local/java/jdk1.2.2/
worker.list=aelstworker.aelst.type=ajp13worker.aelst.host=localhostworker.aelst.port=8009worker.aelst.lbfactor=50worker.aelst.cachesize=10worker.aelst.cache_timeout=600worker.aelst.socket_keepalive=1worker.aelst.socket_timeout=300httpd.conf
Section titled “httpd.conf”Add some lines to the apache conf.
JkWorkersFile /usr/local/apache/conf/workers.propertiesJkLogFile /usr/local/apache/logs/mod_jk.logJkLogLevel debug# Select the log formatJkLogStampFormat "[%a %b %d %H:%M:%S %Y]"# JkOptions indicate to send SSL KEY SIZE,JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories# JkRequestLogFormat set the request formatJkRequestLogFormat "%w %V %T"# Send servlet for context /examples to worker named worker1JkMount /examples/* aelstRestart the server
Section titled “Restart the server”Restart apache and test that request URLs under /examples are forwarded to tomcat.