We first see the tomcat directory structure, where each folder has the following purpose:
A usual way to run Tomcat is to only set the CATALINA_HOME environment variable and run the startup.sh script file. The startup.sh file automatically calculates and assigns the values of other variables which we have not set.
The startup.sh file sets the environment variable and then calls catalina.sh. This file reads CATALINA_BASE value, attaches conf i.e $CATALINA_BASE/conf folder and gets server.xml. This file is the heart of Tomcat’s configuration. It contains all configuration information, like shutdown port, connector post, host name, application folder, etc. For example, Tomcat usually uses 8080 as a connector port, so we can access it at http://localhost:8080/.
Create one folder named “tomcat-instance1” and copy conf, logs, temp, webapps, work folder from CATALINA_HOME folder and change conf/server.xml file in tomcat-instance1. We need to change these ports: shutdown port, connector port, ajp port and redirect port.
Let’s see the sample server.xml file:
<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" /> </server>
So, we change these ports to different numbers, because once a port is binded, then an other process can’t bind it again. In tomcat-instance1/conf/server.xml file I configured server port =8105, connector port = 8181, ajp port = 8109.
<server port="8105" shutdown="SHUTDOWN"> ..... <connector connectiontimeout="20000" port="8181" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectport="81443" /> <connector port="8109" protocol="AJP/1.3" redirectport="81443" /> </server>
Now we can create two script files for starting up and shutting down the tomcat-instance1.
export CATALINA_BASE= /home/ramki/tomcat-instance1 cd $CATALINA_HOME/bin ./startup.sh
export CATALINA_BASE= /home/ramki/tomcat-instance1 cd $CATALINA_HOME/bin ./shutdown.sh
Here we explicitly set the CATALINA_BASE variable and point it to the new tomcat-instance1. Then we go to the CATALINA_HOME/bin folder because all binary files for running tomcat are still present there. Then we use the startup/shutdown cripts.
Based on the above technique, we can create many instance folders and change the conf/server.xml file port values and run that instance with their own newly created script files.
Reference: Running Multiple Tomcat Instances on Single Machine from our JCG partner Rama at the Ramkitech blog (original text was modified for readability reasons).