Multiple Instances of Tomcat
November 25th, 2005 ahowesSo I finally set out to learn how to set up multiple instances of Tomcat. It is common that you could have multiple web sites being served up by the same tomcat installation so setting up muiltple instances will allow for each application to have its own JVM (keeping one from affecting the other) and allow you to bring down one site without bringing down all of tomcat.
So, in a nutshell, what you are doing is using one common binary distribution of tomcat and starting up multiple instances of of that common tomcat with different configuation files and code.
Set up Tomcat
Install and set up Tomcat as you normally would. Also, set up Apache and mod_jk if you have not already, you will need Apache to serve the requests to each tomcat instance.
Setting up multiple Tomcat instances
The next step is to set up each tomcat instance. Each instance gets its own conf, webapps, temps, logs and work directories while using a common binary distribution. This is made possible by the environment variable CATALINA_BASE. CATALINA_BASE points to your code/configuration base while CATALINA_HOME points to the binary distribution. Once you have each of your instances set up, you can create a shell script for each instance that will start tomcat, setting the CATALINA_BASE env variable for that particular instance.
Apache and mod_jk
The last piece is setting up mod_jk and apache to connect to each tomcat instance using the ports specifed in each instance’s server.xml file. You can configure different workers for mod_jk that connect to each instance’s specified port in server.xml. Then in apache’s httpd.conf you set define what url mask uses what worker.
For the details of this set up, here is an excellent article that lays out the steps needed to set up the multiple tomcat instances that I found. He even gives you example scripts that you can use to start up each individual instance and how to start up each instance upon startup.
Some notes
The one gotcha I fould while setting this up is that if you use the scripts given in the article, you need to set up the directory/file permissions so that the tomcat user can access those files since the script starts up tomcat using su tomcat. So all I did was set up the group tomcat as the group owner of the files within each instance’s directory. You can also change the script to use root to start up tomcat, but since it’s recommended to not run to many things as root, I set up each instance to run as tomcat.
