Wednesday, March 7, 2007

How to load balance Tomcat 5.5 with Apache on Windows

It's rather amazing that I could not find a single how-to that could detail the steps for using the Apache web server to load balance multiple Tomcat 5.5 instances on Windows. I found tons of articles- but they were either for older versions of Tomcat or Apache or for Unix flavors and none were accurate enough. Apache itself has fragmented documentation on the subject, and after much trial and error and pulling of hair, I succeeded.

The least I can do is list the steps here for all others who have had trouble getting it right.
The goal of this blog is to set up two Tomcat 5.5 instances with Apache in front, load balancing them. Please note that this does not cover clustering. I'll save that for another blog once I actually do it :-)
Also, this is not a tutorial on load balancing. It will get you up and running- then you might want to look at the Tomcat documentation to find out what exactly all those options do.

So, this is what you need to download:
1. Apache HTTP server 2.2.4 from The Apache HTTP Server Project. I prefer the zip over the Windows Installer. You are free to choose the Windows Installer if you wish, but modify the service instructions below accordingly.

2. Apache Tomcat 5.5.20 from Apache Tomcat downloads

3. Mod JK Tomcat connector from here. Please note: You want to download the binary- click on JK 1.2 Binary Releases --> win32 --> jk-1.2.21 --> mod_jk-apache-2.2.4.so

Now let's start by installing Tomcat first.

1. Extract the Tomcat zip. Hereafter, the directory you extracted to will be referred to as TOMCAT_HOME

2. Test Tomcat to see that it works. Go to TOMCAT_HOME\bin and run startup.bat
You may need to add an environment variable called CATALINA_HOME which is set to TOMCAT_HOME in case Tomcat fails to start.

3. Open up your browser and access http://localhost:8080/
If you see the default page, then Tomcat Instance 1 is working fine. Shut down Tomcat.

4. Now set this up as a service. Go to your command prompt, change directory to TOMCAT_HOME\bin and set an environment variable called CATALINA_BASE. The value of this variable should again, be TOMCAT_HOME. For example:

SET CATALINA_BASE=C:\Tomcat-5.5.20

Then, execute the following: service install Tomcat5
This will set up the Windows service for Tomcat.

5. Go to your Services and see that Tomcat 5 is listed. Start the service, and then verify again that http://localhost:8080 is up.
That's all for the first Tomcat instance. Now for the second.

1. Make a directory called SecondInstance in TOMCAT_HOME

2. Copy the conf, logs, shared, temp, webapps and work directories from the TOMCAT_HOME directory into the SecondInstance directory.

3. Open up SecondInstance\conf\server.xml in a text editor. We've got to change the port numbers so that they don't conflict with the first instance.
I just incremented by 10 and changed them as follows, but you could use other port numbers:

<Server port="8005" shutdown="SHUTDOWN"> to <Server port="8015" shutdown="SHUTDOWN">

<Connector port="8080" maxHttpHeaderSize="8192"... to <Connector port="8090" maxHttpHeaderSize="8192"...

<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" /> to <Connector port="8019" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

Change the SSL port if you need it as well.


4. Now set up the second instance to run as a service. Go to the command prompt, change to the TOMCAT_HOME\bin directory. Set the CATALINA_BASE environment variable to TOMCAT_HOME\SecondInstance. For example:

SET CATALINA_BASE=C:\Tomcat5.5.20\SecondInstance
Then, type:
service install SecondTomcat

Go to Services and start the SecondTomcat service. Test it out by pointing your browser to http://localhost:8090/
Works ok? Great! Your second tomcat instance is now ready to be used.

Next, let's set up the Apache HTTP Server. It's pretty simple...

1. Run the installer you downloaded. The standard install will do.

2. Open the Apache Server Monitor and start the web server if it's not already running.

3. Point your browser to http://localhost/ to verify that Apache is running on port 80.

4. Stop Apache.

Finally, we reach mod JK. Let's set it up first just to delegate requests to the two Tomcat instances, and we'll load balance it a bit later.

1. Copy the mod_jk-apache-2.2.4.so to the modules directory in your Apache installation.

2. Rename it to mod_jk.so

3. Open up httpd.conf in the conf directory of your Apache installation in a text edit, and add the following line at the end of the set of LoadModule statements:
LoadModule jk_module modules/mod_jk.so

4. Create a file called workers.properies in the conf directory. Add these lines to it:


workers.tomcat_home=C:/tomcat-5.5.20

workers.java_home=C:/jdk1.5.0_03

worker.list=worker1,worker2

worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13

worker.worker2.port=8019
worker.worker2.host=localhost

worker.worker2.type=ajp13


This file defines which workers Apache can delegate to. We've listed worker1 and worker 2 to correspond to our two tomcat instances. Remember to set tomcat_home and java_home as well.

5. Specify the worker properties in httpd.conf:

Add these lines just after the LoadModule definitions-

# Path to workers.properties
JkWorkersFile c:/apache2.2/conf/workers.properties

# Path to jk logs
JkLogFile c:/apache2.2/mod_jk.log

# Jk log level [debug/error/info]
JkLogLevel info

# Jk log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions for forwarding
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"


JkMount /jsp-examples worker1
JkMount /jsp-examples/* worker1

JkMount /tomcat-docs worker2
JkMount /tomcat-docs/* worker2

Defining these tells Apache where to look for definitions of its workers and tells it that any requests for the jsp-examples context should be handed off to the Tomcat instance represented by worker 1, and any requests for tomcat-docs context should be handed off to Tomcat Instance 2, represented by worker 2.

5.5 [Added on April 11 2007]
Edit the server.xml for Tomcat and Tomcat's SecondInstance and add a jvmRoute attribute to the Engine element:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
for the first instance and
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2">
for the second.

6. Start Tomcat Instance 1 and 2. Start up the Apache webserver. Point your browser to http://localhost/jsp-examples/ and then to http://localhost/tomcat-docs. You should see the respective pages load. To distinguish which Tomcat is serving you the page, the easiest thing to do is edit the index page in the tomcat-docs and jsp-examples of Tomcat 2 and change the title for example. Then you can verify that tomcat-docs is being served only by the second instance.

Thats it!! Apache is now delegating requests to both Tomcats.
Now for our last task- we will load balance it so that Apache distributes load for jsp-examples between both instances of Tomcat. It also serves as a failover mechanism. If Tomcat 1 is down for whatever reason, Apache will automatically keep delegating to Tomcat 2 so your application remains accessible.

Load balancing is a simple configuration. First shut down your Tomcat instances and Apache as well.

1. Open workers.properties in a text editor.

2. Edit it so it looks like this (changed lines in bold)-

workers.tomcat_home=C:/tomcat-5.5.20
workers.java_home=C:/jdk1.5.0_03

#worker.list=worker1,worker2
worker.list=balancer

worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.lbfactor=1

worker.worker2.port=8019
worker.worker2.host=localhost
worker.worker2.type=ajp13
worker.worker2.lbfactor=1

worker.balancer.type=lb
worker.balancer.balance_workers=worker1,worker2
worker.balancer.method=B

We've changed the worker list to a single worker called balancer, and specified that the worker type of balancer is 'lb' or load balancer. The workers it manages are worker1 and worker2 (these do not need to appear in the workers list). And finally, we set the balance method to 'B' or balance by busy factor. Apache will delegate the next request to the Tomcat instance which is least busy. Please note that there are a couple of options for method- consult the Apache/Tomcat documentation which lists out options for workers properties to help you decide the best method for your type of application.

3. Open httpd.conf and comment out the previous JkMount directives. Replace them with these:

JkMount /jsp-examples balancer
JkMount /jsp-examples/* balancer

Very simple- we've just pointed Apache to a single worker- the balancer.

4. Start up both Tomcats and Apache. Access http://localhost/jsp-examples
You will either be served by Tomcat 1 or Tomcat 2. To prove that both are capable of serving, shut down the first instance and refresh your browser. You should be served by instance two.

Congratulations! You've successfully set up Apache load balancing multiple tomcat instances!

77 comments:

maxblacks said...

nice doc - I plan to do this soon

- Mittal

lazythinker said...

What does local load-balancing really get you? A single Tomcat instance is already multithreaded, and you can bump up its memory allocation to take advantage of the memory you have available.

Is it just so you can shut them down one at a time? (Or restart them manually if an instance dies on you, with no down time... that makes some sense to me, but again it's possibly a lot of setup work for a rare benefit).

Load balancing across multiple servers is more intuitively helpful -- but you aren't covering that here.

Anonymous said...

Hi nice job on putting the doc...I tried to do the same only diff is I am using tomcat 5.0.28. When I edited httpd.conf with the required information I am unable to start apache...the error i get is - Syntax error on line 118 of E:/Apache/conf/httpd.conf:
Can't find the workers file specified
The edits I made were as per your blog -
LoadModule jk_module modules/mod_jk.so

# Path to workers.properties
JkWorkersFile E:/Apache/conf/workers.properties

# Path to jk logs
JkLogFile E:/Apache/logs/mod_jk.log

# Jk log level [debug/error/info]
JkLogLevel info

# Jk log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions for forwarding
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

JkMount /jsp-examples worker1
JkMount /jsp-examples/* worker1

JkMount /tomcat-docs worker2
JkMount /tomcat-docs/* worker2

Can you help me?
OS - 2000 Server, Apache - 2.2.4, Tomcat 5.0.28

Thank you for your time in advance.

thought-bytes said...

Anonymous, drop me a mail at thought dot bytes at gmail dot com and I will try to help out

thought-bytes said...

Anonymous had problems with this config so he renamed mod_jk.so to mod_jk.dll and it worked
He was using Win 2003 Standard with Service Pack 1.
In case you have trouble, you might want to try this out...

thought-bytes said...

One thing I missed:

Edit the server.xml for Tomcat and Tomcat's SecondInstance and add a jvmRoute attribute to the Engine element:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
for the first instance and
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2">
for the second.

Ganapathy said...

Thought Bytes,

This is one of those rare documents which worked perfectly the very first time attempted.

Its people like you who make the web a worthwhile place.

Thank you very much
--GS

AJ said...

People I should give you a standing ovation for this article. One thing I want to ask, say if I want to run the Tomcats on different machines. Ever attempted this? I have to do this. Please help.

thought-bytes said...

Well, I have not tried it yet...when I get to that I'll be sure to post it here. In the meanwhile, a very simple thing to try would be to edit your workers.properties and change the host of the worker there :-)
I have no idea whether that is sufficient, but it's worth a shot :-)
Let me know how it goes...

AJ said...

Alright, the deal is that it did not work. Maybe there is something else we need to do. But if we forget tomcat for a while, I was thinking of replicating Apache itself. Is that possible?

I really appreciate your blog. One thing that I am amazed at is that Apache httpd code is in C/C++ and Tomcat is in Java? Is that true or I am having hallucinations?

thought-bytes said...

AJ, Apache itself can run multiple instances. Create a copy of httpd.conf (and change the port), and start it using httpd -f path-to-new-httpdConf
You can install it as a service as well.

And no, you are not hallucinating...mod_jk is a connector used to bridge that gap.
-Luanne

AJ said...

I worked this thing out perfectly but now my tomcat service at port 8080 does not start.

Says -

"Tomcat5 service started and stopped. It happens with services which do not have to do anything e.g. logging."

AJ said...

Alright, It was this change -

5.5 [Added on April 11 2007]
Edit the server.xml for Tomcat and Tomcat's SecondInstance and add a jvmRoute attribute to the Engine element:

Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1"

for the first instance and

Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2"

for the second.


I removed this and the process started and the loadbalancing works fine.

thought-bytes said...

That's weird. I added that because my sessions were not being maintained...

quietgenie said...

Thanks for posting the guide, thought-bytes. It's a great starting point.

Also, I would like to report that load balancing across multiple machines does indeed work with this setup as the base. All you have to do then is change the hostnames and the port numbers appropriately.

There seems to be a catch, though, if you want apache to serve up your application's static content (and php and perl if you have that, too) while your tomcats serve up your java content. You must have a copy of the application on the machine on which apache is installed in addition to all of the machines on which you have tomcat installed.

Anonymous said...

Cool~ I was longing for such a doc for long, nice job!

PS. I wonder why docs on apache related projects are all sort of.... scattered around... it is the same on using xdoclets...

Anonymous said...

Amazing work.

david said...

I tried to use your notes to setup load balancing between an NTLM IIS and two tomcats. However, even though i set up sticky sessions, the session moves back and forth between two machines. Our application is a struts application and we use request.getSession(false) when setting cookies, which we thought would keep the same sessionId. Any ideas?

david said...

Ok, i can answer my own question thanks to http://www.junlu.com/msg/11523.html.
I needed to name the jvmRoute and loadbalance names of mod_jk to be equivalent

Sameer said...

Hello,

Firstly, thanks for the precise & consolidated document.

I noticed a typo in the line: -

worker.balancer.balance_workers=worker1,worker2

This should be corrected to: -

worker.balancer.balanced_workers=worker1,worker2


Also, you mentioned that for more load balancing methods, we can refer Apache/Tomcat docs but for the life of me, I couldn't find.

worker.balancer.method=B

Do you have a link or more description I could read-up on these methods?

Thanks,

Sameer

thought-bytes said...

Sameer,

balanced_workers used to be the older directive.
Please see: http://tomcat.apache.org/connectors-doc/reference/workers.html

This contains documentation for the other load balancing methods as well.

Anonymous said...

hello there
i did exactly as u said in the deployment sheet but doesn't work
when i try to start the apache2.2 service always receive the following messege

(the requsted operation has faild)

any ideas

JRutenberg said...

This was an amazing tutorial ...Thank you I could not find anything on the web that even compared to this one. The only thing I found that took me a while is that you added the JkMount and such to the http.conf file, while the worker declarations are in the workers.properties file. Are you not supposed to use one or the other? as soon as I changed all my mount commands to the properties file I stopped getting the could not find worker errors. What do you think?

Thanks again for the tutorial ...I am very impressed and cant wait to see what else you are going to do!


here is my workers. properties file:


JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y} "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

JkRequestLogFormat "%w %V %t"

JkMount /admin/index.html oweb
JkMount /index.jsp base



workers.tomcat_home=d:\tomcat
workers.java_home=C:\Program Files\Java\jre1.5.0_12
workers.list=base,oweb


workers.base.port=8009
workers.base.host=localhost
workers.base.type=ajp13


workers.oweb.port=8019
workers.oweb.host=localhost
workers.oweb.type=ajp13

JRutenberg said...

update ...Sorry my mistake ...what had actually fixed the issue was I was calling workers.list ...I have changed it at the same time I was moving declarations around ...So I stand corrected ...you example is flawless :) thanks again!

Anonymous said...

Thanks Luanne.G8 bolg.
I was lost with information from different sites.
But you have given a step by step direction with every small detail.
Hoping to see more such blogs.

Sushama

Anonymous said...

This is very good doc. As mentioned by AJ I have the requirement to do it for Tomcat On different machines (win 2003 Enterprise) (Machine 1 and 2 ) and connect it from a Apache Webserver on Machine 3. Thanks

naveen said...

hi i am creating the second instance with the port 8090
and added into services
start the second instance
but it does not run on the port 8090
please give the solution

thought-bytes said...

Anonymous, please see the earlier comment by quietgenie. You should be able to change the hosts and have it work on different machines.

Naveen, please provide more info. What error do you get? Very difficult to figure out what's going on with the info you provided.

-Luanne

jason dwyer said...

hey there lazythinker,

load balancing in this [nicely written] article is entirely in the apache/mod_jk layer, passing on to the configured tomcat(s) as defined in the workers.properties.

what you might want to consider ( although 'lazythinker' might indicate you might not...:) ) is that a normal deployment topology is for one apache web server fronting several separate tomcat servers.

you'll notice one of the properties for each worker is for the hostname, which is pretty simple to change to the second ( or third..) host on your network.

mod_jk will look at the jvmRoute for the worker, which should match the worker name, and compare that with the incoming appendage to the jsessionid cookie, and pass requests along accordingly, thereby giving session affinity in the load balanced environment.

appspc said...

I have configured the Two tomcat instances and one Apache. All the three (http://localhost, http://localhost:8080/jsp-examples and http://localhost:8090/jsp-examles)are working properly, but when I try to call http://localhost/jsp-examples I get 404. Do I need to change anything else in httpd.conf? or am I missing something

Here is my workers.properties
#######################
workers.tomcat_home=C:/tomcat-5.5.23

workers.java_home=C:/Program Files/Java/jdk1.5.0_12

worker.list=worker1,worker2

worker.worker1.port=8009
worker.worker1.host=servername
worker.worker1.type=ajp13

worker.worker2.port=8019
worker.worker2.host=servername
worker.worker2.type=ajp13

#######################

lines added to the httpd.conf

*******************************
LoadModule jk_module modules/mod_jk.dll

--IfModule !mod_jk.c-- commented it in this post because this tag is not allowed in the post
LoadModule jk_module modules/mod_jk.dll
# Path to workers.properties
JkWorkersFile C:/Program Files/Apache Software Foundation/Apache2.2/conf/workers.properties

# Path to jk logs
JkLogFile C:/Program Files/Apache Software Foundation/Apache2.2/logs/mod_jk.log

# Jk log level [debug/error/info]
JkLogLevel debug

# Jk log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions for forwarding
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

JkMount /jsp-examples worker1
JkMount /jsp-examples/* worker1

JkMount /tomcat-docs worker2
JkMount /tomcat-docs/* worker2
--/IfModule--

**************************************

Lines added to server.xml on both tomcat instances are


Engine name="Catalina" defaultHost="servername" jvmRoute="worker1"


**********************************

thought-bytes said...

Appspc,

A couple of things in your example:
First is that jsp-examples is only served by worker1- not by both Tomcats.
Secondly, your jvmRoute is set to the same worker in both Tomcats. Each Tomcat should be associated with its own, different worker.

-Luanne

Jay said...

thanks luanne for all your help, the loadbalancing config you have specified here is the best, very neat and clean.....and yes it works.

Btw, when I said it was working on chat....it was more my happiness than it actually working which made me see things. It was not working on my XP box...when I tried the same setup on the server directly (win2003) it worked like a charm....!!

AJ said...

Till date my instances of load balancing are running perfectly without this update (I commented out these lines) -


5.5 [Added on April 11 2007]
Edit the server.xml for Tomcat and Tomcat's SecondInstance and add a jvmRoute attribute to the Engine element:

Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1"

for the first instance and

Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2"

for the second.

Any comments as why?

Anonymous said...

Hi,
Nice post.....you have done a wonderful service..Let me add my small contribution.

It's about the section 5

No need to add worker properties in httpd.conf.
A module specific conf file with worker properties can be created in conf folder. So in this case it is
conf/mod-jk.conf

LoadModule, jkmount details etc are to be specified in this file.

Include the module conf file in httpd.conf

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

I tried this in apache_2.0.59 and ofcourse mod_jk-apache-2.0.29.so

This configuration is found to be working in 6.0.10 and 6.0.14

Regards,
Vijesh

thought-bytes said...

AJ,

It will work even without that, but I suspect your sessions might be getting messed up. If your app is not dependent on sessions then you won't notice anything wrong...

-Luanne

AJ said...

I just tried and Axis2 also works fine now. Balancing is perfect. I was wondering if there is a replication mechanism in Axis2 itself and not Tomcat.

Anonymous said...

I have followed the steps you have given but iam getting 404 error. it works when i give http://localhost:8080/jsp-examples
and http://localhost:8090/tomcat-docs but does not work if i give without the port number. Iam not getting any error msgs in the any of the logs. Please help.

Anand said...

I need some help regarding the NameVirtualHost.

Following are the things I have done

1) I installed Apache 2.2 webserver and Tomcat 5.5 server.
2) I am able to access the without using port by using http://localhost/GVL/jsp/index.jsp


Now I have IP Address which is registered with a DNS name. By typing that DNS name,
i need to load the welcome file.
say for an example my IP is 10.11.1.1 and registered with abcd.abc.com .
If I type http://abcd.abc.com, then it should load the URL http://abcd.abc.com/GVL/jsp/index.jsp.
I have configurated the NameVirtualHost but if I tyep http://abcd.abc.com, it says BAD Request.
So, I have to give full URL http://abcd.abc.com/GVL/jsp/index.jsp to access the application.

Following is my virtualHost configuratino in httpd.conf file


< VirtualHost 10.11.1.1 >

ServerName abcd.abc.com
DocumentRoot "C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\GVL"
JkMount /*.jsp wrkr
JkMount /servlet/* wrkr
JkMount /*.do wrkr
JkMount /*.css wrkr
JkMount /*.* wrkr

DirectoryIndex index.jsp


< /VirtualHost >

Please let me know where I am making mistake.

-Anand

Anonymous said...

good...:)

jimmydorsey said...

Have you taken a look at High Availability for the load balancer (apache) yet?

Looking for a windows solution.

Anonymous said...

Hi Launne,
i am configuring these settings, After setting the below changes apache server in not running, there is no error also showing

##############
workers.tomcat_home=C:/tomcat-5.5.20
workers.java_home=C:/jdk1.5.0_03

#worker.list=worker1,worker2
worker.list=balancer

worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.lbfactor=1

worker.worker2.port=8019
worker.worker2.host=localhost
worker.worker2.type=ajp13
worker.worker2.lbfactor=1

worker.balancer.type=lb
worker.balancer.balance_workers=worker1,worker2
worker.balancer.method=B
################
can you help me

Sreejith said...

Hi friend,
I did all the settings, as u specified.in first step , it is working fine getting page also through apache port, But after making the changes listed below , Apache server is not starting


#################
workers.tomcat_home=C:/tomcat-5.5.20
workers.java_home=C:/jdk1.5.0_03

#worker.list=worker1,worker2
worker.list=balancer

worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.lbfactor=1

worker.worker2.port=8019
worker.worker2.host=localhost
worker.worker2.type=ajp13
worker.worker2.lbfactor=1

worker.balancer.type=lb
worker.balancer.balance_workers=worker1,worker2
worker.balancer.method=B

###################

apache server i tried to run thru from prompt httpd,
there is no error also
Can you please help me for solving the problem,
I am using tomacat instances are in one system,

thought-bytes said...

Sreejith, did you complete both step 4 and 5 before attempting to restart?

Sreejith said...

Hi,
Thx , Now it is working
Sreejith

Anonymous said...

You guys rock, I tried it and it worked, I too was pulling my hair

- CurryBastard

Anonymous said...

Austy: THANK YOU HEAPSS!! This is a simple to follow instruction! Been reading uncounted articles non of em brought me anywhere..

HIGHLY RECOMENDED!!!!!!!!!

THANKS AGAIN!!!

Anonymous said...

Thank you very much
You saved me lots of time figuring it out.
very recomended.
Just one thing, I used Tomcat 6 and tried with the .exe NEVER use it it does not have the scripts in the .bin directory unly use the .zip as your source.
Thanks again.

Ashish said...

Perfect Documentation!!!

I have a quick question: Does it take care of the Session Affinity implicitly?

Ashish R said...

Got It!!!
I went through the documentation and found that there is a element sticky_session which is by default true unless set it to false.

claudio said...

This is my problem trying to run Apache:

Syntax error on line 485 of C:/Programmi/Apache Software Foundation/Apache2.2/conf/httpd.conf


Syntax error on line 3 of C:/Programmi/ApacheSoftwareFoundation/Apache2.2/conf/mod-jk.conf

Cannot load C:/Programmi/Apache SoftwareFoundation/Apache2.2/modules/mod_jk.so into server

I followed this tutorial:

http://docs.jboss.org/jbossas/jboss4guide/r4/html/cluster.chapt.html

I'm not sure having select the correct jk_mod from this page:

http://apache.fis.uniroma2.it/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.26/

I downloaded mod_jk-1.2.26-httpd-2.2.4.so and I renamed it to mod_jk.so

My apache version is apache_2.2.8-win32-x86-openssl-0.9.8g


Sorry for my english...

AJ said...

I could perform the multiple machine LB. Just keep all the ports same as Tomcat servers are on different machines and put the IP address in workers.properties instead of localhost. You may need to open certain ports though.

workers.tomcat_home=C:/tomcat-5.5.20

workers.java_home=C:/jdk1.5.0_03

worker.list=worker1,worker2

worker.worker1.port=8009
worker.worker1.host=IP add machine1
worker.worker1.type=ajp13

worker.worker2.port=8009
worker.worker2.host=IP add machine2
worker.worker2.type=ajp13

Antonis said...

At last!!
I try to make it work the whole day.
Thank you a lot!!!

rani said...

Thanks for such a good document.
I have the setup you described above working. I want to know how to handle the https.
Tomcat1 is listening for https in 8443 and tomcat2 at 8444.
How should I forward these requests thru apache?

Anonymous said...

hallelujah!!!

The vendor of our main production applications publishes everything in Tomcat webapps and coming from the IIS world it has been a challenge figuring out how I can create a farm of tomcat servers to handle increasing load.

This site is a godsend! I had to resolve a few errors but it is working!

Anonymous said...

I could perform the multiple machine LB. Just keep all the ports same as Tomcat servers are on different machines and put the IP address in workers.properties instead of localhost. You may need to open certain ports though.

workers.tomcat_home=C:/tomcat-5.5.20

workers.java_home=C:/jdk1.5.0_03

worker.list=worker1,worker2

worker.worker1.port=8009
worker.worker1.host=IP add machine1
worker.worker1.type=ajp13

worker.worker2.port=8009
worker.worker2.host=IP add machine2
worker.worker2.type=ajp13



from that we can create clustering.. but can u tell me which ip i should acess..for acessing web application...
and if we need more configuration for acess then what is it ???
please tell me
Thanx in Advanced

AJ said...

Use any IP address. Usually you can use the first machine. But that's the idea of clustering; you can access any machine and the cluster will decide which one is primary. Got it?

Ray said...

This is brilliant! Thank you for this document.

Tanmoy said...

Nice and useful Blog.

I have done as per your blog and it is working fine.

Now can you please tell me how to use https instead of http for this whole configuration?

Thanks in advance.......

SpaceDragon said...

Great doc! That's excatly what I am looking for, the documentation form Apache is so poor.

In my install, I actually have two tomcats (2 separate JVMs) and one apache all on one machine. After reading your post and a little bit modifications, I was able to get mine to work in my enviroment. I was wondering where I should point my workers.tomcat_home since I have two. I left out workers.tomcat_home, workers.java_home in workers.properties and it still works.

Maybe workers.tomcat_home is not necessary?

Anyway, excellent good job on the tutorial.

win32/apache2.0.59/mod_jk 1.2.14/tomcat 5.5.27

Rocio said...

Thank you very much!

It has been very useful :)

ratheeshekm said...

thanks for your blog.I had spend lot of time to implement load balancing with modjk.I have configured it with your article help and is working very nice.It is very nice to get such a support.I have one request please update the article depending on the new Apache tomcat versions.

123 123 said...

Cool post as for me. It would be great to read more about this matter.
BTW check the design I've made myself Companionship in London

sanjaya said...

This is one of the finest posts I came across regarding load balancing on Apache and Tomcat engine.Keep on Good work

-sanjaya

SundarMurthy said...

Hey, Is it possible to use IIS + ISAPI instead of Apache Http Server will the above configurations work ?
Thanks

NRS said...

Thanks for the steps. When I have installed Apache2.2, try to restart it. It's resulting in
httpd.exe: Could not reliably determine the server's fully qualified domain name, using xxx.xxx.xx.xx for ServerName.

What should I used for the domain name, i tried using computer name, ipaddress - not helping.

thanx in advance

Laura said...

Cool aricles you got here.
It would be useful to find something more concerning this post.
Thank for give that information.
With best regards!!
Ukraine Kiev escort

Anonymous said...

Hi,


I'm tried this with apache-tomcat-6.0.14 and mod_jk-1.2.28-httpd-2.2.3.so. After adding the configuration details in the httpd.conf, it is giving "Windows couldn't start the Apache 2.2 on Local Computer..."

I have also tried by renaming jk-1.2.28-httpd-2.2.3.so to mod_jk.dll. I'm using MS Windows server 2003.

I would appreciate any help.

Thanks

amodg said...

Hello,

I had posted previous comment on September 24, 2010 1:59 PM

For me, after adding JkWorkersFile in double quotes, it is working. JkWorkersFile "E:/Apache/conf/workers.properties"

Thanks
-Amod G

Pritesh said...

You save my time... Thanks

Darker Than Black... said...

when i change the httpd.sonf file iam geeting an error when i start apache server again. can u help me.

Darker Than Black... said...

after i edit the httpd.conf file iam getting an error when i restart it.
here are the lines i added to httpd .conf

LoadModule jk_module mod_jk.so

# Path to workers.properties
JkWorkersFile E:/Apache/conf/workers.properties

# Path to jk logs
JkLogFile E:/Apache/logs/mod_jk.log

# Jk log level [debug/error/info]
JkLogLevel info

# Jk log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions for forwarding
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

JkMount /jsp-examples worker1
JkMount /jsp-examples/* worker1

JkMount /tomcat-docs worker2
JkMount /tomcat-docs/* worker2

when i put '#' before each line its working. otherwise is telling error starting apache

Anonymous said...

Great, accurate article that is still relevant years after it was written. The apache notes on how to do this are fragmented, at best, but after following these clear steps I have a working load balancer! Thanks!

Anonymous said...

i have two separate tomcat installations, and apache web server on a single xp machine. i've created service for both the tomcats, and they are working fine. now, i want to integrate both the tomcats with the apache web server.

But, worker.properties is having only one entry for the tomcat_path

i don't know what to do next

Anonymous said...

Thanks Thought Bytes..awesome blog..we are done successfully by following the steps as specified.

Anonymous said...

Anonymous(January 31, 2012 5:01 PM):

tomcat_path in worker.properties is optional. So u can comment it out.

Nina_O said...

I've just found your blog "How to load balance Tomcat 5.5 with Apache on Windows" from March of 2007 and am quite impressed that people are still using your guide with success. Can the same instructions be used on a Windows 2008 server, using Apache server 2.4.3 (or maybe 2.2.22) and a later version of Tomcat?
Thanks!

Aldrin Misquitta and Luanne Misquitta said...

@Nina_O Not really tested it on your config, but you should just give it a try and let us know how it goes.