Setting Tomcat to run Mutiple instances of it

Tomcat

Introduction

This document will teach us how to setup Tomcat to run Multiple instances of it.

Intended Audience: System admins.

Instruction to installing Tomcat so that we can run multiple instances.

1. Install the Tomcat files

Download Tomcat and unzip it into an appropriate directory. I usually put it in /opt, so it ends up in a directory called /opt/apache-Tomcat-6.0.29 (6.0.29 being the current version as of this writing), and make a symlink named /opt/tomcat to that directory. When later versions come out, we can unzip them and relink, leaving the older version in case things don’t work out (which rarely if ever happens, but as system admins we are paranoid).

2. Add user for each instance

For each instance of Tomcat we’re going to run, we’ll need to add a user and create Tomcat directory in it that will be CATALINA_BASE. For example, we will add users Tomcat1 and Tomcat2 and create Tomcat directory the CATALINA_BASE will be /home/Tomcat1/Tomcat and /home/Tomcat2/Tomcat2 respectively.

In each users home directory we need the following sub directories: conf, logs, temp, webapps, and work.

We will copy these sub directories from /opt/tomcat in the Tomcat directory of the user. Of course you should tighten up your server.xml a bit.

The webapps directory is where you’ll put the web applications you want to run on the particular instance of Tomcat.

We like to have the Tomcat manager webapp installed on each instance, so we can play with the webapps, and see how many active sessions there are.

3. Configure the ports and/or addresses for each instance

Tomcat listens to at least three network ports, one for the shutdown command, one for accepting requests on HTTP and one for accepting connection on AJP port. Two instances of Tomcat can’t listen to the same port number on the same IP address, so we will edit our server.xml files to change the ports they listen to.

The first port to look at is the shutdown port. This is used by the command line shutdown script (actually, but the Java code it runs) to tell the Tomcat instance to shut itself down. This port is defined at the top of the server.xml file for the instance.

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

Make sure each instance uses a different port value. The port value will normally need to be higher than 1024, and shouldn’t conflict with any other network service running on the same system. The shutdown string is the value that is sent to shut the server down. Note that Tomcat won’t accept shutdown commands that come from other machines.

Unlike the other ports Tomcat listens to, the shutdown port can’t be configured to listen to its port on a different IP address. It always listens on 127.0.0.1.

The other ports Tomcat listens to are configured with the <Connector> elements, for instance the HTTP or JK listeners. The port attribute configures which port to listen to. Setting this to a different value on the different Tomcat instances on a machine will avoid conflict.

Of course, we’ll need to configure whatever connects to that Connector to use the different port. If a web server is used as the front end using mod_jk, mod_proxy, or the like, then this is simple enough – change our web server’s configuration.

In some cases we may not want to do this, for instance you may not want to use a port other than 8080 for HTTP connectors. If you want all of your Tomcat intances to use the same port number, you’ll need to use different IP addresses. The server system must be configured with multiple IP addresses, and the address attribute of the <Connector> element for each Tomcat instance will be set to the appropriate IP address.

Ports to be changed listed below:

8005
8080
8009
8443

4. Startup

Startup scripts are a whole other topic, but here’s the brief rundown. The main different from running a single Tomcat instance is we need to set CATALINA_BASE to the directory you set up for the particular instance you want to start (or stop).

Put the following in users .bash_profile or .bashrc:

JAVA_HOME=/path/to/jdk
JAVA_OPTS="-Xmx800m -Xms800m"
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=~/tomcat
export JAVA_HOME JAVA_OPTS CATALINA_HOME CATALINA_BASE

Save and close the file. Start the Tomcat with following command

$CATALINA_HOME/bin/catalina.sh start

That’s it now we can run multiple instances of Tomcat without making the redundant copies of Tomcat binaries and libraries.

You may also want the script to start and stop multiple tomcat as service you can find it here.

1 Comment

1 Trackback / Pingback

  1. Running Tomcat as Service | Mohan Cheema's Online Diary

Leave a Reply

Your email address will not be published.


*


CommentLuv badge