HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions

Running multiple instances of Tomcat with single server installation

By Lokesh Gupta | Filed Under: Tomcat

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 !!

About Lokesh Gupta

Founded HowToDoInJava.com in late 2012. I love computers, programming and solving problems everyday. A family guy with fun loving nature. You can find me on Facebook, Twitter and Google Plus.

Feedback, Discussion and Comments

  1. Aditya

    August 29, 2017

    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
  2. JItender

    August 4, 2014

    How Load balancer work in Java applications

    Reply
  3. Narayana reddy

    July 27, 2014

    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
    • Lokesh

      July 27, 2014

      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%\Java\jre6”

      Reply
      • Narayana reddy

        July 27, 2014

        thanks lokesh….

        Reply
  4. bedona

    July 25, 2014

    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
    • Lokesh

      July 25, 2014

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

    July 25, 2014

    HI Lokesh nice work you are doing… I like your posts.. keep it up men..best luck for your future…

    Reply
  6. kaushal

    July 24, 2014

    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
    • Lokesh

      July 24, 2014

      Thanks for your valuable feedback. I really appreciate it. I will take it into consideration in all my future posts. Thanks again.

      Reply
  7. sudheer

    July 24, 2014

    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
    • Lokesh

      July 24, 2014

      I would say them 3 different installations of server; not 3 instances of same server.

      Reply

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Help me fight spammers. Solve this simple math. *

Search Tutorials

  • Email
  • Facebook
  • RSS
  • Twitter

Tomcat Tutorials

  • Tomcat – Architecture
  • Tomat – Default Port
  • Tomcat – Disable Directory Listing
  • Tomcat – SSL Configuration
  • Tomcat – Run Multiple Instances
  • Tomcat – Maven Plugin
  • Get Real IP Behind Load Balancer
  • How web servers work?

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Java Collections
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap