Results for tag "tomcat"

When 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

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:

Tomcat Install

Shell

1

2

3

4

5

6

7

8

9

10

radcortez:tomcat radcortez$pwd

/usr/local/share/java/tomcat

radcortez:tomcat radcortez$ln-sapache-tomcat-7.0.64/current

radcortez:tomcat radcortez$ls

total8

drwxr-xr-x4radcortez admin136BSep2401:49.

drwxr-xr-x@26radcortez admin884BSep2401:38..

drwxr-xr-x13radcortez admin442BSep2401:45apache-tomcat-7.0.64

lrwxr-xr-x1radcortez admin21BSep2401:49current->apache-tomcat-7.0.64/

radcortez:tomcat radcortez$

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:

Tomcat Instances

Shell

1

2

3

4

5

6

7

8

9

10

11

radcortez:tomcat radcortez$mkdirinstances

radcortez:tomcat radcortez$cdinstances/

radcortez:instances radcortez$mkdirdevelopment

radcortez:instances radcortez$ls

total0

drwxr-xr-x4radcortez admin136BSep2401:56.

drwxr-xr-x5radcortez admin170BSep2401:56..

drwxr-xr-x2radcortez admin68BSep2401:56development

radcortez:instances radcortez$pwd

/usr/local/share/java/tomcat/instances

radcortez:instances radcortez$

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:

exec.sh

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#!/bin/bash

TOMCAT_HOME="$(dirname $0)/.."

cd$TOMCAT_HOME&&TOMCAT_HOME=$PWD&&cd-&>/dev/null

export TOMCAT_HOME

export CATALINA_HOME="$(readlink -f "$TOMCAT_HOME/../../current")"

export CATALINA_BASE="$(readlink -f "$TOMCAT_HOME")"

export CATALINA_OPTS="-Dhttp.port=8080 $CATALINA_OPTS"

export CATALINA_OPTS="-Dhttps.port=8443 $CATALINA_OPTS"

export CATALINA_OPTS="-Dajp.port=8009 $CATALINA_OPTS"

export CATALINA_OPTS="-Dshutdown.port=8005 $CATALINA_OPTS"

echo"JAVA_HOME set to $JAVA_HOME"

echo"CATALINA_BASE set to $CATALINA_BASE"

echo"CATALINA_HOME set to $CATALINA_HOME"

$CATALINA_HOME/bin/"$(basename "$0")""$@"

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;

Replace

By

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.

Tomcat Bin Scripts

Shell

1

2

3

4

5

6

7

8

9

10

11

12

radcortez:bin radcortez$ln-sexec.shcatalina.sh

radcortez:bin radcortez$ln-sexec.shstartup.sh

radcortez:bin radcortez$ln-sexec.shshutdown.sh

radcortez:bin radcortez$ls

total32

drwxr-xr-x6radcortez admin204BSep2410:14.

drwxr-xr-x8radcortez admin272BSep2408:07..

lrwxr-xr-x1radcortez admin7BSep2410:13catalina.sh->exec.sh

-rw-r--r--1radcortez admin651BSep2409:32exec.sh

lrwxr-xr-x1radcortez admin7BSep2410:14shutdown.sh->exec.sh

lrwxr-xr-x1radcortez admin7BSep2410:13startup.sh->exec.sh

radcortez:bin radcortez$

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.