Multiple Tomcat Instances

posted by Roberto Cortez on

Multiple Tomcat InstancesWhen building a real application you often find yourself having to deal with different stages of the software. The most common stages are development, testing and production, but you can have many more. This means that you need a different environment to deploy the application on each of the current stages. You use different environments to be able to perform versioning, different configurations, test bug fixes and so on. This also poses challenges on upgrading environments, changing shared configuration or keeping track of the servers. I will show you how to do it with Tomcat.

The easiest way to set up multiple Tomcat instances is to duplicate the entire Tomcat folder and change a few configurations. I don’t advise doing it this way, since it’s harder to maintain, harder to spin up new instances and harder to upgrade. Instead, we will set up the instances in a much more flexible way, by duplicating only a few things and keeping a shared base folder for all instances.

Installation

You need Tomcat of course. Download it here.

I’ve used version 7.0, but this should also work with other versions. I’m also doing the setup in a Unix like environment. This can also be accomplished in a Windows box, but the commands need to be adjusted.

Unzip the installation folder to a directory of your choice. I just recommend to do it in a parent folder and you can use a name like tomcat or server.

Now, instead of using the unzipped folder, we are going to create a link to it, like this:
ln -s apache-tomcat-7.0.64/ current

Here is a sample:

Setup

To keep this simple, we are going to create two instances: development and production. But keep in mind that you can create as many as you want by making the necessary adjustments to the scripts.

Folders

Create a folder now named instances or environments. Inside, create a folder named development:

Now copy the folders conf, logs, temp, webapps and work from the Tomcat install folder into development and production:

If you wish, you can now remove these folders from the Tomcat install folder, but is not mandatory.

Home and Base

The idea here is to share the main Tomcat folders and each instance has a copy of their personal folders to not clash with each other. Tomcat defines two environment variables called CATALINA_HOME and CATALINA_BASE that allow us to do that.

Create a bin folder in the instances development. Add the following exec.sh script:

Note: If you are using MacOSX, you might need to install core-utils using brew and replace readlink by greadlink to achieve the proper behaviour.

This script is going to set up the proper configuration variables to point to our shared Tomcat and the specific instance folders. Note the properties http.port, https.port, ajp.port and shutdown.port are included in the CATALINA_OPTS environment variable. With these we can pass specific configuration to the server.xml file. Tomcat is smart enough to perform property replace substitution as long as you have the proper placeholders in place.

Files

All these operations are performed in the development folder instance.

conf/server.xml

Edit the file conf/server.xml do the following changes;

ReplaceBy
8080${http.port}
8443${https.port}
8009${ajp.port}

Note: Unfortunately the only place where property replacement doesn’t work is the shutdown port. I think this is a bug in Tomcat and should be fixed. So for now, we need to keep it hardcoded.

bin/exec.sh

On the bin folder, create links to exec.sh to the following files: catalina.sh, startup.sh, shutdown.sh.

This will allow you to call the original Tomcat, but by calling the exec.sh set up first. The magic is done by the line $CATALINA_HOME/bin/"$(basename "$0")" "$@" in the exec.sh script.

Run

The instance should be ready to be executed. Just run it as you would do it normally by executing sh catalina.sh run or sh startup.sh from the development instance folder.

Additional Instances

Just duplicate the development instance folder to a production one and edit the bin/exec.sh to update it with different ports. You can user 7080 for http, 7443 for https, 7009 for ajp and 7005 for the shutdown.

Since property replacement is not working properly for the shutdown port, we need to manually edit conf/server.xml from the production instance and replace 8005 by 7005. When this bug is fixed, and you actually use a property, you don’t have to worry about doing this.

Note: You might need to reestablish the proper links in the scripts catalina.sh, startup.sh and shutdown.sh stored in the bin folder.

After this, your second instance production is ready to run. If you need more, just repeat the last steps, making sure to pick ports that don’t conflict with the instances already set up.

Perks

With this set up you can now:

  • Create new instances easily with minimum changes. You can actually have one untouched unchanged instance, that you can use to copy from to create others.
  • Update the Tomcat version, just by installing a new distribution and updating the link to current.
  • If you place jars in the libs folder of the HOME installation, they become instantly available to all instances.
  • Instead of duplicating the conf folder, you can actually link to the one in HOME and also share the configuration between all environments. Or just link to the files you want to share.
  • Remove an instance, by just deleting its BASE folder.
  • Also works for TomEE!

Alternatives

If you don’t like this set up, you can try using Docker. Check the following post: Get Into Docker.

Let me know if this was useful to you or if you had any trouble following the blog instructions!

Share with others!
  • Twitter
  • Facebook
  • LinkedIn
  • Google Plus
  • Reddit
  • Add to favorites
  • Email
  • RSS

Comments ( 5 )

  1. Replybercut2000

    Thanks,
    saved my day today!

  2. Replyandres

    Thank you very much. Instructions very clear and it really works.

  3. Replyr0d

    Use HyperForm to spin up as many Tomcat docker instances needed.
    https://youtu.be/HjcER-r5InI

Leave a reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>