Running Multiple Tomcats with Single Server Installation

Many times we come to a situation where we need to modify the server configuration such that it is specific to an application. And if we got more than one such application and we want each application to have its own defined configuration options, then it needs some …

Many times we come to a situation where we need to modify the server configuration such that it is specific to an application. And if we got more than one such application and we want each application to have its own defined configuration options, then it needs some sort of configuration. In this tutorial, I am going to discuss the changes you should make to have different instances of tomcat for each application.

1. Steps to configure multiple instances of the Tomcat server

I am using a Windows machine you I will be using the paths in Windows format. But the procedure to create multiple instances is the same in all other operating systems. Also, I am assuming that you want to create 2 new different instances of tomcat. Now start reading the complete steps to achieve the goal.

Step 1. Install Tomcat Server

This is a very obvious step to be in the first place. Let’s say our tomcat installation directory is “C:/tomcatServer“.

Step 2. Create 2 new different folders in different locations

These folders are for storing the instance-specific configuration and other data such as logs, and temp data.

Let’s say the new folders are “C:/tomcatInstanceOne” and “C:/tomcatInstanceTwo“.

Step 3. Copy the ‘conf’ folder into the instances folder from the server folder

This step is necessary to have different configurations for each instance. Any instance-specific configuration-related changes should be done in the related instance folder only.

Step 4. Create instance-specific startup.bat and shutdown.bat

These files will be needed to start and shut down a particular instance. The content of the files will be as below:

startup.bat

set CATALINA_HOME=C:\tomcatServer
set CATALINA_BASE=C:\tomcatInstanceOne
C:\tomcatServer\bin\startup.bat

shutdown.bat

set CATALINA_HOME=C:\tomcatServer
set CATALINA_BASE=C:\tomcatInstanceOne
C:\tomcatServer\bin\shutdown.bat

Place both files in ‘bin‘ folder inside both instance-specific folders. [e.g. Create folder C:/tomcatInstanceOne/bin and copy both files].

The first property (CATALINA_HOME) points to the location of the common information shareable between all running instances, while the other property (CATALINA_BASE) points to the directory where all the instance-specific information is stored.

Step 5. Create setenv.bat for setting instance-specific environment configuration

Create a file called setenv.bat in the “C:\tomcatInstanceOne\bin” (and in the second instance folder as well) directory to set any environment variables mentioned in C:\tomcatServer\bin\catalina.bat. This is the place to set system properties, JPDA addresses, etc.

Step 6. Create more folders for logs, temp etc.

Now it’s time to create more folders that will be used when your application will be actually running. These folders are logs, temp, webapps and work. Create them in both instance folders separately.

Just be sure to edit your conf\server.xml file so that the shutdown ports and HTTP connector ports don’t interfere with other tomcat instances that may be running.

For example, change the ports where Tomcat will be running/debugging.

First server.xml file

<connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<server port="8005" shutdown="SHUTDOWN"/>
<connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<connector port="8100" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

Second server.xml file

<connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<server port="8006" shutdown="SHUTDOWN"/>
<connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
<connector port="8101" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

After doing the above changes you should be able to launch multiple instances of the same tomcat server on your machine. Please let me know if you are stuck somewhere.

Happy Learning !!

Leave a Comment

  1. Hello, is it possible to run two different applications under one instance of tomcat with two different versions of java?

    Reply
  2. I just start in a server administration role and I’m facing this scenário, where we have four physical servers. For each one, we have two Tomcat Instances, getting 8 Tomcat over four servers.
    Applications: Only one ! The same over 8 Tomcat Instances.

    Please, does anybody knows a reason to do that ???

    Many thanks and sorry for my English

    Reply
  3. Hi Lokesh,

    In my windows system I have already configured Tomcat 8.0.45 version, I want to run multiple tomcat on same system using different port number (running one is 8080).
    I tried all the above steps but still when I tired to access the another tomcat using 8181 port its not connecting, could you assist me how to proceed further.

    Thanks in advance

    Reply
  4. HI Lokesh,
    in step 5: setenv.bat file which code need to be placed ,means same code of catalina.bat fiel or any other code
    after that where i need to deploy two different projects

    can u explain please….

    Reply
    • Apart from CATALINA_HOME and CATALINA_BASE, all environment variables can be specified in the “setenv” script. The script is placed either into CATALINA_BASE/bin or into CATALINA_HOME/bin directory and is named setenv.bat (on Windows) or setenv.sh (on *nix). The file has to be readable.

      By default the setenv script file is absent. If the script file is present both in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE is preferred. The CATALINA_HOME and CATALINA_BASE variables cannot be configured in the setenv script, because they are used to locate that file.

      Example Config:

      set “JRE_HOME=%ProgramFiles%Javajre6”

      Reply
  5. What is the benefit of running two different instances of the same server vs running two different servers all together in the same machine with different ports configured.
    You have changed the port numbers, copied startup bat files, so anyways we have to run the instances with separate startup files and have to deploy different applications separately.
    I am just curious how different instances of same server is going to help as it is all just code which will execute when we start a server and here we are just not replicating the files where codes are written.

    Reply
    • A very good question indeed. Thanks for raising it.

      The one major benefir I see, in having multiple instances approach, is ability to upgrade and downgrade main server version with easy steps. More to it, sometimes you will need to run 5 applications in minor version of tomcat and rest n-5 application in major tomcat version. In this case, you will install servers at ONLY 2 locations and point CATALINA_HOME property of first 5 instances to minor version install home, and rest instances will point to major version install location.

      In other case, you will have to install server in multiple locations and then AGAIN reconfigure all application specific settings.

      Another good reason might be permissions related. You can grant permisions to users in their instance specific folders; and restrict access to main server install. It just become easy when you have implemented multiple instances approach.

      Reply
  6. Hi………….I following your valuable expertise in Java .It really helpful and Thank you for this.But some where I get difficulties bcz of incomplete code or steps(mean you talking about some some previous steps may be).So I just want to say that please give us complete steps with program so that the It will be very helpful for person like me or beginners. Thank If dont find I didnt get any Idea after step 5,How to do and what to do.

    Reply
  7. What if i duplicate the entire tomcat folders into the number of instances i require.Is that not equivalent to what you have mentioned in your post

    Reply

Leave a Comment

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.