How to Run Multiple Tomcat Instances on One Server?

I’m sure you must have faced one of below question while developing Dynamic Web Project in past.

If yes, then you are at right place..

Tomcat Multiple Instances

Let’s understand basic first

To run multiple Tomcat instances on one server, you can use the following steps:

  1. Download and extract multiple copies of the Tomcat binary distribution to different directories on your server.
  2. Create a unique configuration file for each instance by copying the server.xml file from the conf directory of each Tomcat instance and modifying the ports and other settings as needed.
  3. Create unique sets of environment variables for each instance by copying the setenv.sh (or setenv.bat) file from the bin directory of each Tomcat instance and modifying the CATALINA_HOME and CATALINA_BASE variables to point to the appropriate directories for each instance.
  4. Create unique startup and shutdown scripts for each instance by copying the startup.sh and shutdown.sh (or startup.bat and shutdown.bat) files from the bin directory of each Tomcat instance and modifying them to point to the correct environment variables and configuration files for each instance.
  5. Start each instance by running the appropriate startup script for each instance.
  6. To access the different instances, you can use different IP addresses or hostnames, or different ports on the same IP address or hostname.

Note: The above steps are for Linux and Unix-like systems, for Windows you need to adjust the script names accordingly.

Let’s 1st understand Tomcat Directory Structure..

When creating multiple instances of tomcat server, we need to play with the folders inside the server. These folders contain the actual scripts and code for the server. We have the option to either use the code base for tomcat in shared mode where all tomcat instances refer to the same physical code or we can create separate copies of these folder for each tomcat instance.

Tomcat server ports

Having a good understanding of tomcat ports is essential to manage the multiple instances of the same server installation. These ports are used by tomcat for start-up, deployment and shut-down operations.

The detail of each port is as:

Other must read:

Lets start creating multiple Tomcat Instances in Mac environment..

Step-1

Download and Copy Tomcat Instances under ~/Documents/Crunchify/ folder.

Multiple Tomcat Instances

Step-2

Change below HTTP, HTTPS, AJP, Shutdown Ports for Tomcat2 instance.

File location: Tomcat2/conf/server.xml file.

Apache Tomcat Server Shutdown Port Apache Tomcat Server Connector Port Apache Tomcat Server AJP Port

Step-3

The startup.sh and shutdown.sh script files make use of catalina.sh for performing the startup and shutdown operations. We shall edit catalina.sh file as described below:

a)

For tomcat1 , specify following two variables in catalina.sh script file present in the bin folder of the tomcat_home

export CATALINA_HOME= /Users//Documents/Crunchify/Tomcat1 export CATALINA_BASE= /Users//Documents/Crunchify/Tomcat1

b)

For tomcat2 , specify following two variables in catalina.sh script file present in the bin folder of the tomcat_home

export CATALINA_HOME= /Users//Documents/Crunchify/Tomcat2 export CATALINA_BASE= /Users//Documents/Crunchify/Tomcat2

Step-4

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

Here’s a typical startup routine. Just create crunchify.sh script and run it using ./crunchify.sh

JAVA_HOME=/usr/java JAVA_OPTS="-Xmx1024m -Xms300m" export JAVA_HOME JAVA_OPTS CATALINA_HOME CATALINA_BASE $CATALINA_HOME/bin/catalina.sh start

If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion.

Suggested Articles.

  1. Step by Step guide to Enable HTTPS or SSL correct way on Apache Tomcat Server – Port 8443
  2. How to Start Stop Apache Tomcat via Command Line? Check if Tomcat is already running and Kill command
  3. Ansible: How to terminate all AWS EC2 instances using Ansible script?
  4. How to install & setup Apache Tomcat server on Linux Ubuntu host [on Linode]
  5. Step by Step Guide to Setup and Install Apache Tomcat Server in Eclipse Development Environment (IDE)
  6. Build RESTful Service in Java using JAX-RS and Jersey (Celsius to Fahrenheit & Fahrenheit to Celsius)