Saturday, June 30, 2012


Setting up mod jk connector with Apache Web Server


Purpose – This document is for setting up and configuring mod jk 1.2.37 connector with Apache web serverhttpd installed on 64 bit RHEL5.
This specifies the configuration in such a way that the web server will behave as a load balancer and it will equally balance the load of http/https request between two application servers.
One of the two app servers will entertain the request from web front end on the behalf of web server.
In case one node fails, all requests will be forwarded to the alive node. No request will go to the dead node.
Prerequisites
1.     Apache web server httpd installed in /usr/local/apache2 directory.
2.     Two instances of app servers installed.
Steps -
1.     Download the mod jk connector source package in any directory say /XYZ –
cd /XYZ

2.     Extract the mod jk connector source package in /XYZ-
cd /XYZ
tar -xvzf /XYZ/tomcat-connectors-1.2.37-src.tar.gz

3.     Configure the mod jk connector source –
/XYZ/tomcat-connectors-1.2.37-src/native/configure --with-apxs=/usr/local/apache2/bin/apxs

4.     Build the mod jk connector source –
cd /XYZ/tomcat-connectors-1.2.37-src/native/apache-2.0
make

5.     Copy the mod jk module mod_jk.so file in apache modules directory –
cp /XYZ/tomcat-connectors-1.2.37-src/native/apache-2.0/mod_jk.so /usr/local/apache2/modules/

6.     Include mod jk configuration file in httpd configuration -
Add below lines in httpd.conf -
vi /usr/local/apache2/conf/httpd.conf

# Include mod_jk's specific configuration file 
Include conf/mod-jk.conf

7.     Create mod jk configuration file -
vi /usr/local/apache2/conf/mod-jk.conf

# Load mod_jk module
# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties
JkWorkersFile conf/workers.properties

# Where to put jk logs
JkLogFile logs/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 indicates to send SSK KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat
JkRequestLogFormat "%w %V %T"

# Mount your applications
# The default setting only sends Java application data to mod_jk.
# Use the commented-out line to send all URLs through mod_jk.
 JkMount /* loadbalancer
JkMount /application/* loadbalancer

# Add shared memory.
# This directive is present with 1.2.10 and
# later versions of mod_jk, and is needed for
# for load balancing to work properly
JkShmFile logs/jk.shm

# Add jkstatus for managing runtime data
<Location /jkstatus/>
JkMount status
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>


8.     Create workers.properties file –
vi /usr/local/apache2/conf/workers.properties

# Define list of workers that will be used
# for mapping requests
worker.list=loadbalancer

# Define Node1
# modify the host as your host IP or DNS name.
worker.node1.port=8009
worker.node1.host=<IP or HOSTNAME>
worker.node1.type=ajp13
worker.node1.lbfactor=1

# Define Node2
# modify the host as your host IP or DNS name.
worker.node2.port=8009
worker.node2.host=<IP or HOSTNAME>
worker.node2.type=ajp13
worker.node2.lbfactor=1

# Load-balancing behavior
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=1


Note - We are assuming there is no session replication between the app servers. Thats why we are enabling session stickiness and node1 name and node2 name MUST match with the name of two app servers node name. Say in tomcat node name is configured as jvmRoute in server.xml 

If you are using session replication between the app servers, there is no need to stick the session.  You can remove worker.loadbalancer.sticky_session=1 from workers.properties and no node name configuration is required at app servers side :)

9.     Restart apache httpd service –
/etc/init.d/httpd restart