In this article, I am going to explain how we can configure multiple Tomcat instances with single Apache Web Server.
There are many solutions posted on the web, but it is the simplest and easiest way to do this.
Prerequisite:
- Apache HTTP Server 2.x
- Tomcat 6.x
- mod_jk.so module for Apache Web server
- Any text editor, i.e. Notepad, gedit, etc.
if you do not have Apache HTTP server goto http://httpd.apache.org/ and download the appropriate version and install on your machine.
if you do not have Apache Tomcat Server goto http://tomcat.apache.org/ and download the appropriate version and install on your machine.
if you do not have mod_jk.so module, goto http://tomcat.apache.org/download-connectors.cgi and download the appropriate connector for Apache Web Server.
Step 1: Configure Tomcat
If you having one machine per Tomcat, you don’t need to do this. but if you don’t have, you have to configure each tomcat instance for different port to run successfully.
To configure Tomcat’s port, goto conf directory of any tomcat instance you have and open the server.xml file in the text editor.
search these lines in the server.xml and make the value of port property unique for each instance of tomcat.
<Server port=”8005″ shutdown=”SHUTDOWN”>
<Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />
<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />
Step 2: Creating configuration file of Tomcat instances for Apache Web Server
now open text editor and create a file called workers.properties and enter the information of all tomcat instances you have. and save it under the conf directory of Apache.
A sample worker.properties is given:
# lists the workers by name
worker.list=tomcat1, tomcat2, loadbalancer
# Tomcat1 configuration
worker.tomcat1.port=8009 //AJP1.3 Connector’s port
worker.tomcat1.host=localhost //IP of machine where tomcat1 is running
worker.tomcat1.type=ajp13 //AJP Connector type.
worker.tomcat1.lbfactor=100 //Load Balancing factor
# Tomcat2 configuration
worker.tomcat2.port=8119 //AJP1.3 Connector’s port
worker.tomcat2.host=localhost //IP of machine where tomcat1 is running
worker.tomcat2.type=ajp13 //AJP Connector type.
worker.tomcat2.lbfactor=100 //Load Balancing factor
#Load Balance worker configuration
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat1,tomcat2
Step 3: Configure JK module
Copy the downloaded JK module file (mod_jk.so) to the modules directory of Apache Web Server. now goto conf directory of Apache Server, and open the httpd.conf file in the text editor.
and add these lines to the end of httpd.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
now you have done all the configuration, now you need to configure the JK Module to redirect the requests to specific tomcats. It can be done by the following:
To redirect specific request to specific Tomcat add this line to httpd.conf:
JkMount /abc/* tomcat1
or
JkMount /*.do tomcat2
Or, if you want to configure requests to be served by any of the tomcat then add this:
JkMount /*.jsp loadbalancer
if any of the tomcat is stopped then all the request will be served by other tomcat and if both the tomcat are running then the requests are redirected to tomcat servers according to the lbfactor specified in the workers.properties.
Now start all the Tomcat Servers and Apache Server, and its done.
Posted by Tirun Reddy on June 21, 2017 at 12:30 pm
It is good job, could you please explain how to configure two tomcat instances in two different machines.
please reply me.
Posted by Girish Gaurav Agarwal on July 14, 2017 at 11:56 am
its simple, just change hostname from localhost to ip of the machine in worker.properties, and it will work for you.
for new update and post you can follow our blog @
blog.girishgaurav.com
Posted by karan on November 17, 2015 at 9:49 pm
nice article. But how can we specify more than one tomcat for specific request.
eg :- JkMount /abc/* tomcat1
JkMount /abc/* tomcat2
JkMount /efg/* tomcat3
JkMount /efg/* tomcat4
Please help and suggest
Posted by Girish Gaurav Agarwal on December 3, 2015 at 8:22 am
Hi Karan,
As I mentioned, you can use a balanced worker for defining multiple tomcat for a specific request:
JkMount /abc/* loadbalancer
I’ve moved my blog from wordpress to http://blog.girishgaurav.com, kindly follow it to get updated.
Posted by kumar Shorav on December 17, 2013 at 11:06 am
Really very neat and clean articles…it samed me a lot of time. I would like to know how to monitor jk connector in tomcat 7.
Thanks
Posted by Sujatha on April 9, 2013 at 3:43 pm
Hi,
I need to install mod_jk in windows server 2008. I already installed apache 2.2.22. Please help me.
Thanks,
Posted by krishan_111284@yahoo.com on February 16, 2013 at 12:12 am
Very nice tutorial. Thanks a lot….
Posted by Joe on November 22, 2012 at 1:29 am
Your explanation looks good. I set it up but I am having a problem. I have apache on the tomcat1 server and tomcat running on tomcat1 and tomcat2 servers where they are different servers (i.e. different IP addresses for tomcat1 and tomcat2. When I purposely take down tomcat1 to test my configuration I get a 503 error. So Apache isn’t connecting to 8009 of the remote tomcat server. Any suggestions on what to look for to fix my problem?
Posted by Joe on November 22, 2012 at 1:34 am
Two seconds after I asked the question I found my answer. 8009 wasn’t open on the remote server. I thought it was because it had Apache on there before “using” 8009 locally. I switched the AJP connector to a port I knew was open and it worked.
Thanks for the write up!
Posted by DInesh on May 23, 2012 at 12:24 pm
Worthfull post 🙂
Posted by David Long on October 27, 2010 at 2:13 am
Great post. I was wondering if there was a way with the cluster you setup to enable sticky sessions. I am finding that the load balancer is just switching ever other request to the different tomcat servers.
Posted by AlbertMF on July 23, 2010 at 9:00 pm
Great post! 😀
Harovia
Posted by Server Computers on July 12, 2010 at 6:13 am
The Apache Web server allows site managers to override the standard error page that is served for specific errors by number. Server Computers
Posted by Derrick on July 12, 2010 at 6:02 am
Very nice work.
It may be worth explaining how you can have more than two instances just as easily.
Posted by Hussain on July 11, 2010 at 9:39 am
Very nice, clean and simple approach …. will surely try it out. Thanks a lot.