How to configure the module mod_jk with Apache and Jonas ?

Apache is often the web server used in front of an application server (for instance Jonas).
You need a module like mod_jk to configure Apache with Jonas. Here are the details to configure that module (under RedHat Linux) :

1) Download and install the module mod_jk
mod_jk is an Apache module which can be used to forward a client HTTP request to an internal application server, using the Apache JServ Protocol (AJP).
To get it, type the following commands :

cd /usr/lib64/httpd/modules
wget http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.31/x86_64/mod_jk-1.2.31-httpd-2.2.x.so
chmod 755 mod_jk.so
/etc/init.d/httpd restart

The module will show up in the modules folder of Apache : /etc/httpd/modules

2) Configure Apache and mod_jk
First, you need to load the mod_jk module into Apache when it starts.
In the main Apache configuration file, /etc/httpd/conf/httpd.conf, add the following line :

LoadModule jk_module modules/mod_jk.so

Then you need to specify the path to the workers properties file and configure the contexts and the workers which will handle these contexts :

<IfModule jk_module>

JkWorkersFile /etc/httpd/conf/workers.properties
JkShmFile /etc/httpd/logs/mod_jk.shm
JkLogFile /etc/httpd/logs/mod_jk.log
JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkRequestLogFormat "%w %m %V %T"

JKMountCopy All

# Send requests for context /blabla/* to worker named worker1
JkMount /blabla/* worker1
# Send requests for context /blabla* to worker named worker1
jkMount /blabla* worker1
# Send requests for context /blabla to worker named worker1
jkMount /blabla worker1
# Send requests for context /blabla/ to worker named worker1
jkMount /blabla/ worker1
# Send requests for context /anotherContext* to worker named worker1
jkMount /anotherContext* worker1

The line JKMountCopy All is very important. It copies the mount point definitions in all the virtual hosts.
Create and add the file workers.properties to /etc/httpd/conf/ :

# Worker list
worker.list=worker1
# Define worker1
worker.worker1.port=8009
worker.worker1.host=127.0.0.1
worker.worker1.type=ajp13
worker.worker1.lbfactor=1
worker.worker1.cachesize=10
# Load-balancer
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=worker1
worker.loadbalancer.sticky_session=1
worker.loadbalancer.local_worker_only=1

Here I have created only one worker (worker1) because I just want to forward requests to an instance of Jonas. There is no load-balancing in this configuration since there is only one single instance.
loadbalancer is not a real worker, it is responsible for the management of several “real” workers.

3) Configure Jonas
To enable AJP connections to the 8009 port of the Jonas server, you need to create an AJP connector in the $Jonas_Base/conf/tomcat6-server.xml and $Jonas_Base/conf/tomcat7-server.xml files :

  <!-- Define an AJP 1.3 Connector on port 9009 -->    
    <Connector port="8009" protocol="AJP/1.3" redirectPort="9043" />
  <!-- An Engine represents the entry point (within JOnAS/Tomcat) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host). -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="JOnAS" defaultHost="localhost" jvmRoute="worker1">

Here is a schema that sums things up:

Links :
http://tomcat.apache.org/connectors-doc/ajp/ajpv13a.html
http://jonas.ow2.org/current/doc/doc-en/integrated/configuration_guide.html#N11C7D
http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html