November 12, 2006

Load Balance and Cluster Setup for Tomcat via Apache 2.2

for updated version please visit

Hello, here I will try to describe a sample configuration of a multiple tomcats and one APACHE for load balancing and identical tomcat server instances. These tomcat instances will be identical since they serve the same application 'medya'. This sample is based on opensuse 10.1 Remastered with apache 2.2 and tomcat 5.5.20. It’s done for a server with 16GB RAM and 4 CPUs and jvm only enable 2 GB heap size

main purpose is using same tomcat-home for diffrent tomcat-bases for identically same tomcat instances

Part I: Tomcat Cluster and Linux Service definitions

First download and extract the tomcat 5.5.x to /usr/tomcat55 this will the unique tomcat home for our tomcat instances. Same tomcat instance count will be 3 (s1,s2,s3). Then replace server.xml with the same file at the attachment. This file contains simple variable configuration like

<Server port="${tomcat.server.port}1" shutdown="SHUTDOWN"> .... <Connector port="${tomcat.server.port}2 ...

variable are given as external jvm parameters for the same configuration file.

tomcat.base: standard tomcat variable for common base jars files names and shell scripts.
tomcat.server.port: used in the configuration file as port prefix
${tomcat.server.port}1: Shutdown port
${tomcat.server.port}2: http connector port
${tomcat.server.port}3: ajp connector

eg:if tomcat.server.port:771 then shutdown port will be 7711, http will be 7712,ajp will be 7713 tomcat server name for load balancing and cluster

and sample tomcat cluster configuration

then create a directory for first tomcat base. /opt/tb55/s1 and link file system to base tomcat instance I do this sample on a single computer and it can be done over a network since the files are only used at startup for very limit size. and copy same base for more

mkdir /opt/tb55
mkdir /opt/tb55/s1
ln /usr/tomcat55/conf /opt/tb55/s1/conf -s
ln /usr/tomcat55/shared /opt/tb55s1/shared -s
mkdir /opt/tb55/s1/logs
mkdir /opt/tb55/s1/temp
cp /usr/tomcat55/webapps /opt/tb55/s1/webapps -R
cp /opt/tb55/s1 /opt/tb55/s2 -R
cp /opt/tb55/s1 /opt/tb55/s3 -R

Up to we build single tomcat home and three tomcat base. these instances have same library with different applcation base(webapps),log and temp directories.

Part II: Defining and starting tomcats as linux service in a cluster structure.

copy the to /usr/tomcat55/bin it will be standard startup script. this script is based upon the script at link. and copy startup script tb-s1,tb-s2,tb-s3 to /etc/init.d for service definition.

content of tb-s1 (tomcat 1 service script)

export CATALINA_PORT=`echo $PP`1
export CATALINA_HOME=/usr/tomcat55
export JAVA_OPTS="-Duser.language=en -Dtomcat.server.port=$PP$CATALINA_NAME"
export JAVA_HOME=/usr/java/jdk1.5.0_05
su $TOMCAT_USER -m -c "/usr/tomcat55/bin/ $1"

As you see this contains variables for server.xml and jvm options you add or remove yours. this service will be run as a system user tomcat.

now you can add this service as system service

chkconfig --add tb-s1
chkconfig --add tb-s2
chkconfig --add tb-s3

Part III: Defining AJP connector ,proxy and load balancer for APACHE

put mod_proxy_balancer.conf to /etc/apache2/conf.d

this file contains all configuration for

proxy_ajp_module: for tomcat ajp connector used by proxy module

proxy_module: used by apache to support for proxy
proxy_balancer_module: used to load balance application context over different tomcat instances

LoadModule proxy_module /usr/lib/apache2/
LoadModule proxy_balancer_module /usr/lib/apache2/
LoadModule proxy_ajp_module /usr/lib/apache2/
<Location /balancer-manager>
SetHandler balancer-manager
<Proxy balancer://medyaCluster>
BalancerMember ajp://localhost:7713 route=s1
BalancerMember ajp://localhost:7723 route=s2
BalancerMember ajp://localhost:7733 route=s3
<Location /medya>
ProxyPass balancer://medyaCluster/medya stickysession=JSESSIONID

and restart apache2 service

Part IV: Deploying Sample application

simple put you application folder to /home/tomcat/medya

and out medya.xml to /usr/tomcat55/conf/Catalina/localhost/medya.xml

<Context docBase="${user.home}/medya" distributable="true" path="/medya"></Context>

I hope you enjoy your application :) .

Sorry for formating :(


for updated version please visit


Ismael said...

i have a problem with this configuration. I have a application in both tomcats, it 's a loggin page (user and password). But alway apache o alfresco take my request like the first time. Alway i see the loggin page.

Sometimes i can use the application but i return to loggin first like i lost the session.

Altuure said...

you should open singlesignon valve and tomcat clusters

<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"/>
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />

Anonymous said...

Would be great if you'd update this for Tomcat 6 and/or 7...