Running multiple instances of Tomcat 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 applications and we want each application to have it’s own defined configuration options, then it need 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.

Steps to configure multiple instances of 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 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 very obvious step to be in 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, temp data.

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

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

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

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

These files will be needed to start and shutdown 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 properties (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 are 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 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 which will be used when your application will be actually running. These folders are like 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.

e.g. 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 above changes you should be able to launch multiple instances of same tomcat server in your machine. Please let me know if you are struck somewhere.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

15 thoughts on “Running multiple instances of Tomcat with single server installation”

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.