Apache Httpd Load Balancer Configuration using mod_jk

Why wew need Apache Load Balancer

We can use Apache Load Balancer module (mod_jk) to optimizes resource use, maximizes throughput, minimizes response time, and avoids overload as well as for auto failover.

How it can be utilized

Let us assume that you have two tomcat web applications running on two different servers. Now you want to make your application highly available and also want to distribute traffic across both tomcat application servers. So here we can configure one web server(apache) with mod_jk module, which will be act as a frontend server and two tomcat application servers will act as backend server.

Client request for your application will come to a Web server(apache). Based on mod_jk configuration, Apache will send the request to both tomcat applications. Cool!!!!

How to configure mod_jk on Apache Web server.

1. First download mod_jk source from below mentioned link. Please choose package according to your server architecture(32bit or x64).

For Linux:

For Windows:

 If you are using windows then you need to simply copy mod_jk.so file into your apache’s module directory.
If you are using linux then you will have to make mod_jk.so file using below steps.
Extract tomcat-connectors-1.2.37-src.tar.gz.
1.1  #”tar -zxvf tomcat-connectors-1.2.37-src.tar.gz”

1.2 Now configure it using below command.
#./configure –with-apxs=/usr/sbin/apxs

1.3 Now run make and then make install.
“make && make install”
Note: If you are getting any error, then please check if “httpd-devel” package is installed or not.

1.4 If above 3 commands run successfully, then it would have created mod_jk.so file into /etc/httpd/modules/ directory.

2. Now Load that module in apache’s httpd.conf file using string. You can copy that string at bottom of httpd.conf file.
# Load mod_jk
LoadModule jk_module modules/mod_jk.so

3. Now you need to specify workers.properties file path in httpd.conf file, so that apache can read the configuration of both tomcat applications.

JkWorkersFile conf/workers.properties

4. you can specify log file location, log level, log format too using string in httpd.conf file.

JkLogFile logs/mod_jk.log
JkLogLevel warn
JkLogStampFormat “[%a %b %d %H:%M:%S %Y]”

5. Now create workers.properties file using the content.

– worker.jvm1.port is ajp port of tomcat application server configured in server.xml file,
– worker.jvm1.host is IP address of tomcat application server,
– worker.jvm1.type is the ajp protocol version,
– worker.jvm1.lbfactor is to assign weight to tomcat application,
– worker.jvm1.max_packet_size is to specify maximum packet size.
Please note that here we have used jvm1 and jvm2. For tomcat server 1, we have used jvm1 and for another tomcat server, we have used jvm2.worker.loadbalancer.balance_workers is used to mention the name of tomcat application server’s worker name.
worker.loadbalancer.sticky_session – This will enable sticky session.
worker.loadbalancer.method – This will set load balancing method.

6. Now you need to mount Load Balancer in httpd.conf file. You can use below string in httpd.conf file to mount load balancer.

JkMount /* loadbalancer

If you want to exclude any directory then it can be specified before JkMount tag as shown below.

JkUnMount /balancer-manager loadbalancer
JkMount /* loadbalancer

7. Once all above-mentioned configuration is done, you can restart apache web server and test.

For Sticky Session testing:

If your application works on the session, then you may need to configure sticky session at Apache Load Balancer. In workers.properties we have already set worker.loadbalancer.sticky_session to 1. But we need configuration at tomcat too. In both application server edit tomcat/conf/server.xml file and change property as shown below.
<Engine name=”Catalina” defaultHost=”localhost” >
<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”jvm1″>
In tomcat app server 1 you can use jvm1 and in second tomcat you can use jvm2.

Installing Java on Linux

Installing Java on Linux


Download the latest version of JDK from http://www.java.sun.com. I have downloaded jdk-1_5_0_01-Linux-i586.bin for this tutorial.
Change to the directory where you downloaded the JDK ( I downloaded it in my home directory /home/maddy) and make the self-extracting binary executable:
chmod +x jdk-1_5_0_01-Linux-i586.bin

Run the self-extracting binary, this will display the License agreement text and will ask you to accept the agreement:
Above command should create a directory called jdk1.5.0_01 in the /home/maddy directory. Move the JDK directory to /opt . Here is the command used:
mv jdk1.5.0_01 /opt
Set the JAVA_HOME environment variable, by modifying /etc/profile so it includes the following:

export JAVA_HOME

save /etc/profile file. Then run following command to take effect.
source /etc/profile
Check to make sure JAVA_HOME is defined correctly using the command below. You should see the path to your Java JDK.
Output should be
Now need to set out new Java by default through the alternative utility. First, we will install alternative java then we will configure it make it as default java.
To install java using the alternative command:
alternatives –install /usr/bin/java java /opt/jdk1.5.0_01/bin/java 2
Now need to configure it to make it as default Java location:

alternatives -config java

Above command will given output. There we will have to select latest java location.
[root@alpeshpc opt]#alternatives –config java

There are 3 programs which provide ‘java’.

Selection Command
* 1 /usr/lib/jvm/jre-1.4.2-gcj/bin/java
2 /opt/jdk1.5.0_01/bin/java

Give there number 2, then press enter.

Thats it.