News Ticker

Set up Tomcat, Apache and mod_jk cluster

I often post about Tomcat, Apache and mod_jk on Twitter. Follow me here

In this article I will go through a common set-up for a small production environment. A single tier, load balanced application server cluster.


A high level overview of what we will be doing.

  1. Downloading and installing Apache HTTP server and mod_jk
  2. Downloading Tomcat
  3. Downloading Java
  4. Configuring two local Tomcat servers
  5. Clustering the two Tomcat servers
  6. Configuring Apache to use mod_jk to forward request to Tomcat
  7. Deploying application to Tomcat server that tests our set-up


What is Apache?

Apache is an HTTP server.

What is mod_jk?

It is an Apache module that allows AJP communication between Apache and a back end application server like Tomcat.

I am running this on Ubuntu 14.04LTS installed on a dual boot PC with Windows 7.

Application Server Cluster


Download Apache2

We are going to use Ubuntu’s APT package maintenance system to obtain and install Apache2.

sudo apt-get install apache2

This will install in /etc/apache2

Download and Install mod_jk

The mod_jk module is not included in the Apache2 download so must be obtained and installed separately. The installation requires that the mod_jk module is visible to Apache and configured to ensure that Apache knows where to look for it and what to do with the requests you want to proxy.

sudo apt-get install libapache2-mod-jk

This will install in /etc/libapache2-mod-jk also two files have been added to the /etc/apache2/mods-available folder.

Download and Install Tomcat 8

At the time of writing this Tomcat 8 does not have a package in APT so you must download the binaries from the tomcat website. select the appropriate binary distribution and extract it as follows.

tar xvzf apache-tomcat-8.0.5.tar.gz

We need two copies of the Tomcat server to be load balanced. I created two directories in the /opt/ location: /opt/tomcat-server1/ and /opt/tomcat-server2/ and copied tomcat into each one.

Download and Install Java

Download Java from APT as follows:

apt-get install openjdk-7-jdk

and set JAVA_HOME in .bashrc

vim ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

Configure Two Local Tomcat Servers

We will edit only the server.xml of the server2 installation of tomcat. We need to change port numbers to avoid conflicts.

We change the following:

<Server port="9005" shutdown="SHUTDOWN">
<Connector port="9009" protocol="AJP/1.3" redirectPort="9443"/>

and comment out the HTTP Connector as we only want the web application to be accessible through the load balancer.

Here is my server2 Tomcat server.xml configuration.

Configure mod_jk

Load balancing is configured in the file, located /etc/libapache2-mod-jk/ where workers represent actual or virtual workers.We will define two actual workers and two virtual workers which map to the Tomcat servers. In the worker.list property I have defined two virtual workers: status and loadbalancer, I will refer to these later in the Apache configuration.

Workers for each server have been defined using values for the server.xml configuration files. I used the port values for the AJP connectors and I have included an lbfactor that sets the preference that the load balancer will show for that server.

Finally we define the virtual workers. The loadbalancer worker is set to type lb and set the workers that represent the Tomcat servers in the balancer_workers properties. The status only needs to be set to type status.

[sourcecode language=”xml” toolbar=”false”]






Ensure that you remove any other worker configuration that are not being used.

Configure Apache Web Server to Forward Requests

You will need to add the following to the Apache configurations located in etc/apache2/sites-enabled/000-default.conf

JkMount /status status
JkMount /* loadbalancer

Verify the Installation

To test that all has been configured correctly we need to deploy an application. A sample application that has been used for years to test such configurations is called the ClusterJSP sample application. You can find it by googling in or from the JBoss site.

Now deploy the war to the webapps folder on both servers and start each server using the start-up script /opt/tomcat-server1/bin/

Go to http://localhost/clusterjsp/HaJsp.jsp and you should see the page show HttpSession information.

Cluster - HA JSP Sample

Now lets look at the mod_jk status page: http://localhost/status. You will see that this page shows information about the load balancer workers and the workers it is balancing.


If everything is working you will see the worker error state show OK or OK/IDLE if they are not currently balancing load.

Things to Try Out

Enable sticky sessions: Configure jvmRoute in the server.xml configuration.

Related Articles

Apache Ant is often configured to automatically compile, deploy and start Tomcat all with a Ant script. I show you how to construct these task in my blog post: Compile, deploy and launch Tomcat with Ant script.

The most significant change in Java Enterprise Edition 8 and Java SE 9 will be support for HTTP/2. Tomcat 9 supports HTTP/2 however the server.xml must be configured to use TLS. Head on over to Configure Tomcat 9 for HTTP/2 to discover how to add the appropriate configurations.

Installing Tomcat and Java on Linux can be tricky but if you follow these simple instructions you won’t have any problems.

Amazon Web Services (AWS) offers free usage for 12 months which allows you to install Tomcat. It consists of 14 services, of which the EC2 service is the most significant to developers. In my tutorial Amazon Free Usage Tier: Installing Tomcat 7 on an EC2 Linux instance I show you how to install and configure Tomcat on an EC2 instance:

One of the most important configuration files in Tomcat is server.xml. I have posted an example of this file in my blog post: Tomcat server.xml example.

Interesting Links

Loadbalancing with mod_jk and Apache
Working with mod_jk
Connecting Apache’s Web Server to Multiple Instances of Tomcat

I often post about Tomcat, Apache and mod_jk on Twitter. Follow me here

2 Comments on Set up Tomcat, Apache and mod_jk cluster

  1. very useful this article

1 Trackback / Pingback

  1. Set up Tomcat, Apache and mod_jk cluster | alex.theedom

Leave a Reply

%d bloggers like this: