Table of Contents




Usually we come across situations where we need to run multiple instances of tomcat server in order to run different applications independent of each other. The situation where this is required is:

1) The tomcat server has been modified according to one particular application and it is not easy to segregate the server and application so that any other legacy/third-party application can be deployed on this tomcat server.

2) Two different user categories will be using the two different URL’s to access the server and this is a company/security requirement or one application has to run on a different port on the server for firewall issues.

Understanding 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.

/bin : This directory contains the startup and shutdown scripts for both Windows and Linux.
/conf : This directory contains the main configuration files for Tomcat. The two most important are the server.xml and the global web.xml .
/server : This directory contains the Tomcat Java Archive files.
/lib : This directory contains Java Archive files that Tomcat is dependent upon.
/logs : This directory contains Tomcat’s log files.
/src : This directory contains the source code used by the Tomcat server. Once Tomcat is released, it will probably contain interfaces and abstract classes only.
/webapps : All web applications are deployed in this directory; it contains the WAR file.
/work : This is the directory in which Tomcat will place all servlets that are generated from JSPs. If you want to see exactly how a particular JSP is interpreted, look in this directory.

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:

  • Connector Port : This is the port where Apache Tomcat listen for the HTTP requests.
  • Shutdown Port : This port is used when we try to shutdown the Apache Tomcat Server.
  • AJP (Apache JServ Protocol) Connector Port : The Apache JServ Protocol (AJP) is a binary protocol that can conduct inbound requests from a web server through to an application server that sits behind the web server.
  • Redirect Port : Any redirection happening inside Apache Tomcat will happen through this port. In Apache TOMCAT there are two instance where redirectPort is mentioned. First one is for the Apache TOMCAT server and other one is for the AJP port.

Steps to configure multiple instances of tomcat on Linux

1) Install Tomcat : For running two different tomcat servers, install/unzip tomcat into two different directories. I am assuming them to be /usr/tmp/tomcat1 and /usr/tmp/tomcat2. If you want to have more than two copies of tomcat running simultaneously, then create as many folders for the same. Deploy the corresponding web applications in to the webapps folder of the tomcat servers.

Other than the above scenario it is also possible to use the same tomcat base installation but using only the minimal directories. These minimal directories can be copied for tomcat1 to tomcat2 instead of installing fresh tomcat into /usr/tmp/tomcat2 folder. The minimal directories are bin, conf, webapps, temp and webapps.

2) Edit configuration:

I have also written about Tomcat terms and components used in steps given below.

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 three variables in catalina.sh script file present in the bin folder of the tomcat_home
export CATALINA_HOME=/usr/tmp/tomcat1

b) For tomcat2, specify following three variables in catalina.sh script file present in the bin folder of the tomcat_home
export CATALINA_HOME=/usr/tmp/tomcat2

c) In the /usr/tmp/tomcat2/conf directory, modify the server.xml file so that all ports which are currently in the format 8xxx are now in 6xxx format. We need to edit a minimum of three ports viz. start-up, shutdown and AJP. You can search for them in server.xml by using shutdown=”SHUTDOWN”, <Connector port= and protocol=”AJP/1.3″ strings. If you are using SSL with tomcat then you also need to provide a SSL port which is unique for all tomcat instances.

d) JAVA_HOME environment variable should be set and pointing to valid JDK or JRE location.

The above steps can also be applied on a tomcat installation on Windows operating system. The only changes required are the path and the CATALINA_HOME statement should be “set CATALINA_HOME=[PATH]“.

Common issues

a) Modify the catalina.sh and server.xml using the vi editor and not any windows file editor.

b) Do not set CATALINA_HOME environment variable. If it is set, delete it. The script file catalina.sh will automatically handle this mandatory environment variable.

c) Do remember the port number updated in server.xml as the same will be required to access the new instance. For example, if you replaced 8080 with 6060 then the tomcat server url will be http://localhost:6060.

Utility to create Tomcat Multiple Instances

Recently, I created a tool for creating multiple instances of tomcat server. This tool works on Windows as well as Linux. The link to download the same is:

There is a readme.txt and help_document.doc included in the package to guide you in running the tool.

Note: This utility the other approach for creating multiple instances i.e. copying the minimal folders from base installation to new installations. It doesn’t require internet connection to create the multiple instances.

References

1. How to Run Multiple Instances of Tomcat on a Single Server
2. Best Practices for Running Multiple Instances of Tomcat
3. Running Multiple Tomcat Instances on One Server

Related Posts

How to Run Multiple Tomcat Instances admin Tomcat
Table of Contents 1. Understanding Tomcat directory structure 2. Tomcat server ports 3. Steps to configure multiple instances of tomcat on Linux 4. Common issues 5. Utility to create Tomcat Multiple Instances 6. References Usually we come across situations where we need to run multiple instances of tomcat server in order to run different applications independent...
Table of Contents <div id="toc"> 1. <a href="#tomcatdirectory">Understanding Tomcat directory structure</a> 2. <a href="#tomcatports">Tomcat server ports</a> 3. <a href="#linuxsteps">Steps to configure multiple instances of tomcat on Linux</a> 4. <a href="#commonissues">Common issues</a> 5. <a href="#utility">Utility to create Tomcat Multiple Instances</a> 6. <a href="#references">References</a> </div> <br/><br/> Usually we come across situations where we need to run multiple instances of <a href="http://www.javaexperience.com/tomcat-server-components-and-terms/" title="Tomcat server components and terms">tomcat server</a> in order to run different applications independent of each other. The situation where this is required is: 1) The tomcat server has been modified according to one particular application and it is not easy to segregate the server and application so that any other legacy/third-party application can be deployed on this tomcat server. 2) Two different user categories will be using the two different URL's to access the server and this is a company/security requirement or one application has to run on a different port on the server for firewall issues. <a name="tomcatdirectory"></a> <h2>Understanding Tomcat directory structure</h2> 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. <strong>/bin :</strong> This directory contains the startup and shutdown scripts for both Windows and Linux. <strong>/conf :</strong> This directory contains the main configuration files for Tomcat. The two most important are the server.xml and the global web.xml . <strong>/server :</strong> This directory contains the Tomcat Java Archive files. <strong>/lib :</strong> This directory contains Java Archive files that Tomcat is dependent upon. <strong>/logs :</strong> This directory contains Tomcat's log files. <strong>/src :</strong> This directory contains the source code used by the Tomcat server. Once Tomcat is released, it will probably contain interfaces and abstract classes only. <strong>/webapps :</strong> All web applications are deployed in this directory; it contains the WAR file. <strong>/work :</strong> This is the directory in which Tomcat will place all servlets that are generated from JSPs. If you want to see exactly how a particular JSP is interpreted, look in this directory. <a name="tomcatports"></a> <h2>Tomcat server ports</h2> 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: <ul> <li><strong>Connector Port :</strong> This is the port where Apache Tomcat listen for the <a href="http://www.javaexperience.com/how-to-send-http-post-request-in-java/" title="How to Send HTTP POST Request in Java">HTTP requests</a>.</li> <li><strong>Shutdown Port :</strong> This port is used when we try to shutdown the Apache Tomcat Server.</li> <li><strong>AJP (Apache JServ Protocol) Connector Port :</strong> The Apache JServ Protocol (AJP) is a binary protocol that can conduct inbound requests from a web server through to an application server that sits behind the web server.</li> <li><strong>Redirect Port :</strong> Any redirection happening inside Apache Tomcat will happen through this port. In Apache TOMCAT there are two instance where redirectPort is mentioned. First one is for the Apache TOMCAT server and other one is for the AJP port.</li> </ul> <a name="linuxsteps"></a> <h2>Steps to configure multiple instances of tomcat on Linux</h2> <strong>1) Install Tomcat :</strong> For running two different tomcat servers, install/unzip tomcat into two different directories. I am assuming them to be <strong>/usr/tmp/tomcat1</strong> and <strong>/usr/tmp/tomcat2</strong>. If you want to have more than two copies of tomcat running simultaneously, then create as many folders for the same. <a href="http://www.javaexperience.com/deploy-servlet-on-tomcat/">Deploy the corresponding web applications</a> in to the webapps folder of the tomcat servers. Other than the above scenario it is also possible to use the same tomcat base installation but using only the minimal directories. These minimal directories can be copied for tomcat1 to tomcat2 instead of installing fresh tomcat into /usr/tmp/tomcat2 folder. The minimal directories are bin, conf, webapps, temp and webapps. <strong>2) Edit configuration:</strong> I have also written about <a href="http://www.javaexperience.com/tomcat-server-components-and-terms/">Tomcat terms and components</a> used in steps given below. 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 three variables in catalina.sh script file present in the bin folder of the tomcat_home <strong>export CATALINA_HOME=/usr/tmp/tomcat1</strong> b) For tomcat2, specify following three variables in catalina.sh script file present in the bin folder of the tomcat_home <strong>export CATALINA_HOME=/usr/tmp/tomcat2</strong> c) In the /usr/tmp/tomcat2/conf directory, modify the server.xml file so that all ports which are currently in the format 8xxx are now in 6xxx format. We need to edit a minimum of three ports viz. start-up, shutdown and AJP. You can search for them in server.xml by using <strong>shutdown="SHUTDOWN", <Connector port= and protocol="AJP/1.3"</strong> strings. If you are using SSL with tomcat then you also need to provide a SSL port which is unique for all tomcat instances. d) JAVA_HOME environment variable should be set and pointing to valid <a href="http://www.javaexperience.com/java-difference-between-jvm-jre-jdk-jit/">JDK or JRE</a> location. The above steps can also be applied on a tomcat installation on Windows operating system. The only changes required are the path and the CATALINA_HOME statement should be "set CATALINA_HOME=[PATH]". <a name="commonissues"></a> <h2>Common issues</h2> a) Modify the catalina.sh and server.xml using the vi editor and not any windows file editor. b) Do not set CATALINA_HOME environment variable. If it is set, delete it. The script file catalina.sh will automatically handle this mandatory environment variable. c) Do remember the port number updated in server.xml as the same will be required to access the new instance. For example, if you replaced 8080 with 6060 then the tomcat server url will be http://localhost:6060. <a name="utility"></a> <h2>Utility to create Tomcat Multiple Instances</h2> Recently, I created a tool for creating multiple instances of tomcat server. This tool works on Windows as well as Linux. The link to download the same is: <div class="download-code"><img src="http://www.javaexperience.com/wp-content/uploads/2012/10/downloadcode-e1349278942164.jpg" alt="Tool to create tomcat binaries"><a href="http://www.javaexperience.com/wp-content/uploads/2012/08/TomcatInstances.zip"><br><br><strong>Tomcat Multiple Instances Utility</strong></a></div> There is a readme.txt and help_document.doc included in the package to guide you in running the tool. Note: This utility the other approach for creating multiple instances i.e. copying the minimal folders from base installation to new installations. It doesn't require internet connection to create the multiple instances. <a name="references"></a> <h2>References</h2> 1. <a href="http://www.openlogic.com/wazi/bid/188102/How-to-Run-Multiple-Instances-of-Tomcat-on-a-Single-Server" target="_blank">How to Run Multiple Instances of Tomcat on a Single Server</a> 2. <a href="http://blogs.vmware.com/vfabric/2012/10/5-scenarios-and-best-practices-for-running-multiple-instances-of-tomcat-or-tc-server.html" target="_blank">Best Practices for Running Multiple Instances of Tomcat</a> 3. <a href="http://java.dzone.com/articles/running-multiple-tomcat" target="_blank">Running Multiple Tomcat Instances on One Server</a>
How to get Installed Version of Tomcat server
Tomcat Interview Questions and Answers
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments