We've been doing a fair bit of work with Tomcat for a while now. It was partly instigated by the knowledge that ColdFusion 10 was going to use Tomcat under the hood (in replace of JRun), but we've also been doing work deploying Railo as well as some Java apps (deploying as WAR files).
One of the things we're so used to is the ability to run multiple instances of ColdFusion on JRun, and we wanted to do the same with Tomcat. As such, this entry is a generic walkthrough to get up and running with multiple instances of Tomcat.
The first thing you need to do is get a hold of Tomcat. Go grab the most recent version from http://tomcat.apache.org (v7.0.27 as of writing).
We like to install Tomcat versions inside a master ApacheTomcat folder, e.g. /opt/ApacheTomcat/apache-tomcat-7.0.27
Once you've extracted Tomcat, take a look at the contents of the folder. You'll find some text files, and more importantly a bunch of folders:
- bin
- contains all the binaries and scripts for running Tomcat
- conf
- configuration files including server.xml and web.xml
- lib
- the libraries that make Tomcat work
- logs
- temp
- webapps
- this is where we put our apps
- work
- folder used by Tomcat to write out files needed during runtime, including generated code for JSPs, class files, serialised sessions
An important part of any Tomcat installation is environment variables. There are 5 different variables that interact with Tomcat, two of which are mandatory:
Mandatory
Optional
- CATALINA_BASE
- CATALINA_TMPDIR
- CLASSPATH
All three optional variables can be calculated using CATALINA_HOME
The usual way
Under a normal install, we really only care about CATALINA_HOME. By default, this variable points to the Tomcat root folder, so using our set up, it points to /opt/ApacheTomcat/apache-tomcat-7.0.27
This gives access to the /bin and /lib folders, and allows all other environment variables to be set when we run startup.sh (found in the /bin folder).
And this is the important part. Instead of allowing CATALINA_BASE to be set automatically, we want to explicitly set it. That's the key to multiple instances.
Configuring Multiple Instances
Create two new folders inside your CATALINA_HOME folder, e.g.
/opt/ApacheTomcat/apache-tomcat-7.0.27/instance1
/opt/ApacheTomcat/apache-tomcat-7.0.27/instance2
Copy the following folders (and their contents) from CATALINA_HOME, and paste them into the two directories you created above:
- /conf
- /logs
- /temp
- /webapps
- /work
(You might want to delete the contents of your copied log folders).
Next we need to configure the 3 different ports that each Tomcat instance will use. These are the connector port, the AJP port, and the shutdown port.
- Connector port
- What Tomcat uses to communicate with the outside world. Default: 8080
- AJP port
- If you're going to be connecting to Apache (or IIS), then you'll be looking to use AJP rather than the "basic" connector. AJP is a binary protocol and is therefore faster than the default connector. Default: 8009
- Shutdown port
- Tomcat uses a port as part of it's shutdown process. It communicates with this port to shutdown cleanly, and therefore it has to be unique per instance. Default: 8005
Let's start by opening and editing /instance1/conf/server.xml. Find the blocks similar to below for the three connector ports
<server port="8005" shutdown="SHUTDOWN">
.....
<connector connectiontimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectport="8443">
<connector port="8009" protocol="AJP/1.3" redirectport="8443">
</connector></connector></server>
Change the ports to something like:
<server port="8105" shutdown="SHUTDOWN">
.....
<connector connectiontimeout="20000" port="8181" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectport="8443">
<connector port="8109" protocol="AJP/1.3" redirectport="8443">
</connector></connector></server>
Do the same for the second instance, providing different port numbers again.
Let's now create some startup/shutdown scripts for our instances. Create a new file called instance1-startup.sh in CATALINA_HOME/bin
Add the following to the script:
export CATALINA_BASE=/opt/ApacheTomcat/apache-tomcat-7.0.27/instance1
cd $CATALINA_HOME/bin
./startup.sh
Create another new file called instance1-shutdown.sh in CATALINA_HOME/bin, and add the following:
export CATALINA_BASE=/opt/ApacheTomcat/apache-tomcat-7.0.27/instance1
cd $CATALINA_HOME/bin
./shutdown.sh
Do the same for instance2, being careful to change the paths.
You can now run ./instance1-startup.sh and ./instance1-shutdown.sh to start/stop your first instance, as well as your other scripts to control your other instances.
Congratulations, you're now running multiple instances of Tomcat, which can be accessed via (depending on the port numbers you chose above)
http://localhost:8181
http://localhost:8282
You can now deploy your ColdFusion and Railo WAR files directly into an instances webapps folders, e.g.
/opt/ApacheTomcat/apache-tomcat-7.0.27/instance1/webapps