Questions ‘n’ Answers – Technology

Just another WordPress.com weblog

How to configure Apache/Tomcat in Load Balanced configuration?

Posted by qnaguru on July 21, 2008

You will need to configure both Apache and Tomcat for this purpose. Let us assume you want to load balance two Tomcat Servers against an Apache instance. Apache, first tomcat, second tomcat, all can be on the same machine(hardware) OR they can be in three different machines (each on its own); this configuration will work.

The below mentioned configuration shows all three running on the same host(myhost.mycompany.com). (You must install tomcat twice ofcourse in different directories, so that you have two tomcats, ofcourse since they are on the same host both tomcats must run on different http ports, say first one on 8080, and second one on 9090. This config is present in <Tomcat_Home>/conf/server.xml file).

Apache Configuration

———-<Apache_Home>/conf/workers2.properties file————–

[shm]
info=Scoreboard. Requried for reconfiguration and status with multiprocess servers.
file=anon

# Defines a load balancer named lb. Use even if you only have one machine.
[lb:lb]
worker=ajp13:myhost.mycompany.com:8009
worker=ajp13:myhost.mycompany.com:9009
timeout=30
attempts=2
recovery=90
StickySession=1
noWorkersMsg=Server Busy please retry after some time.
noWorkerCodeMsg=503

# Example socket channel, override port and host.
[channel.socket:myhost.mycompany.com:8009]
port=8009
host=myhost.mycompany.com
tomcatId=server1
group=lb
lbfactor=100

[channel.socket:myhost.mycompany.com:9009]
port=9009
host=myhost.mycompany.com
tomcatId=server2
group=lb
lbfactor=100

[ajp13:myhost.mycompany.com:8009]
channel=channel.socket:server1

[ajp13:myhost.mycompany.com:9009]
channel=channel.socket:server2

# Map the Tomcat webapp to the Web server uri space

[uri:/myapp/*]
group=lb

[status:]
info=Status worker, displays runtime information

[uri:/jk-stinc/*]
info=The Tomcat /jkstatus handler
group=status:

———end of file——————-

Tomcat Confugration

The only file you will have to modify here is:<Tomcat_Home>/conf/server.xml, yes on both the tomcat servers you are trying to load balance. You have to set up connector port (8009 on first tomcat, 9009 on second tomcat), and jvmRoute (server1 on first tomcat, server2 on second tomcat). You will notice that these values you had defined earlier in workers2.properties file.

So, here is how you would update on first tomcat, the file:<Tomcat_Home>/conf/server.xml

So, AJP 1.3 Connector to be defined as follows(if it is commented out, uncomment it). The relevant line should look like this:

 <Connector port=”8009″ protocol=”AJP/1.3″ protocolHandlerClassName=”org.apache.jk.server.JkCoyoteHandler” redirectPort=”8443″ />

jvmRoute is set up as follows:

   <Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”server1″>

Similar changes you must make to the second tomcat.

You are all set.

Tested on: Apache 2.2.6, Tomcat 6, Linux

Enjoy.

One Response to “How to configure Apache/Tomcat in Load Balanced configuration?”

  1. [...] details on how to setup Apache/Tomcat to talk to one another, see the relevant post here. Possibly related posts: (automatically generated)ILWACOBusselton JettyCycle Trip to East Coast [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>