Running Multiple Instance of Tomcat in Ubuntu
Monday, 25 August 2008 00:00

As we hinted at in our last post, it is possible to run multiple instances of Apache Tomcat in Ubuntu, without having to install it twice. Multiple Tomcat servers can be helpful for several reasons. Although we do all our QA testing on a local staging network, we still like to host a beta server next to our live server to help us verify proper functionality in our production environment.

Before diving into how we run two Tomcat servers, let's first look at the directory structure of the Tomcat installation. Assuming that you already have a single Tomcat server up and running within /usr/local/tomcat/, you might see the following sub-directories:

bin/
conf/
lib/
logs/
temp/
webapps/
work/

To run two Tomcat instances, we only need to copy the sub-directories that contain information specific to each instance (such as configurations), but the binaries can stay in this home location and be shared.

Step 1 - Create the Directories
To begin, create two directories that will hold the information for each server:

mkdir /path/to/server1 /path/to/server2

Next, make a copy of the necessary sub-directories into these locations:

cp -rd /usr/local/tomcat/conf /usr/local/tomcat/webapps /path/to/server1
cp -rd /usr/local/tomcat/conf /usr/local/tomcat/webapps /path/to/server2

Let's also create empty directories for things like log files and temp space:

mkdir /path/to/server1/logs /path/to/server1/temp /path/to/server1/work
mkdir /path/to/server2/logs /path/to/server2/temp /path/to/server2/work

Step 2 - Configure Each Instance
Each instance will need to use its own set of ports, to ensure there are no conflicts. In Tomcat, the ports are controlled within the server.xml file. We'll leave server1 alone, but we'll change server2's to use different ports.

nano /path/to/server2/conf/server.xml

First, look for the line that says something like:

<Server port="8005" shutdown="...">

Change the port to 8006, or any other of your choosing. Next, look for any other lines that contain ports. There should be at least one other, but may be more:

<Connector port="8080" protocol="HTTP/1.1" ... />

Change this port to listen on 8081, or any other of your choosing. Note that this is the port that web browsers will connect to. Or, in our case, our reverse proxy will connect to so that our users don't need to type in crazy ports in their URL.

Of course, you'll need to deploy your J2EE application to each server. Deploying is beyond the scope of this article. However, if you already had your app up and running and you've followed the steps so far, you should be able to boot up the same app twice for the purposes of learning.

Step 3 - Configure Your Bootup Scripts
Now the only part left is to boot up each instance. This is done by specifying an environment variable called CATALINA_BASE, which points to the new directories we've created. A sample bootup script might look like this (read our previous tutorial for installing this as a bootup script):

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export CATALINA_BASE=/path/to/server1
export CATALINA_OPTS="-server -Xmx512m"
case $1 in
start)
sh /usr/local/tomcat/bin/startup.sh
;;
stop)
sh /usr/local/tomcat/bin/shutdown.sh
;;
restart)
sh /usr/local/tomcat/bin/shutdown.sh
sh /usr/local/tomcat/bin/startup.sh
;;
esac
exit 0

For the other server, you'll need to use the same script again, but this time use /path/to/server2 as your CATALINA_BASE.

Conclusion
That should be all you need to boot two Tomcats. You can use the same technique to run even more instances if necessary. As new fixes or upgrades are made to Tomcat, you only need to update one set of binaries in the original location, and it will take effect for all instances you have running.