DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Optimizing Database Connectivity: A Comparative Analysis of Tomcat JDBC vs. HikariCP
  • Deployment of Spring MVC App on a Local Tomcat Server
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC

Trending

  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • AI Paradigm Shift: Analytics Without SQL
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • What Is Plagiarism? How to Avoid It and Cite Sources
  1. DZone
  2. Coding
  3. Languages
  4. Run and Configure Multiple Instances in a Single Tomcat Server

Run and Configure Multiple Instances in a Single Tomcat Server

Learn how to set up several different instances of an application on your Tomcat server, and manipulate each of these instances independently.

By 
Md Nasir Uddin Bhuiyan user avatar
Md Nasir Uddin Bhuiyan
·
Updated Jan. 08, 18 · Tutorial
Likes (27)
Comment
Save
Tweet
Share
85.2K Views

Join the DZone community and get the full member experience.

Join For Free

First, we need to download Tomcat (https://tomcat.apache.org/download-70.cgi).

I downloaded the Core .zip file.

Now unzip the file and rename the folder “Tomcat.” You will get the below folder structure.

Image title

Now suppose we want to run two instances on this single Tomcat.

  • Instance 1: app-one

  • Instance 2: app-two

First I want to create the app-one instance.

So make a copy of your downloaded Tomcat, and rename the folder  “app-one,” and keep only “bin,” “conf,” “logs,” “temp,” “webapps,” and “work” files only.

Image title

Now remove all the files from app-one/bin. In the “bin” folder of “app-one,” you can add a setenv.sh file to configure the JVM heap setting for that tomcat instance.

setenv.sh

export CATALINA_OPTS="$CATALINA_OPTS -Xms256m"
export CATALINA_OPTS="$CATALINA_OPTS -Xmx1024m"
export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxPermSize=512m"

Now make another copy of the “app-one” folder and rename it “app-two.”

Now we have three folders in our directory

1) Tomcat

2) app-one

3) app-two

Now go to the “Tomcat” directory and delete the “conf,” “logs,” “temp,” “webapps,” and “work” folders. Create a new folder named “controller” inside Tomcat. In the “controller” folder, we will write a script to control all the Tomcat instances. Now, inside our Tomcat folder, we have:

Image title

Now we will configure all the Tomcat instances (“app-one”, “app-two”) to different ports.

Configuration for App One

Go to this location, app-one/conf/, and open server.xml in edit mode. Suppose we want to run app-one in the “7050” port. Here is our configuration:

<?xml version='1.0' encoding='utf-8'?>
<Server port="7005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <Listener className="org.apache.catalina.core.JasperListener" />

  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="adminApp">
    <Connector port="7050" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="7543" />
    <Connector port="7509" protocol="AJP/1.3" redirectPort="7543" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

Configuration of App Two

Go to this location, app-two/conf/, and open server.xml in edit mode. Suppose we want to run app-two in “7060” port. Here is our configuration:

<?xml version='1.0' encoding='utf-8'?>
<Server port="7006" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <Listener className="org.apache.catalina.core.JasperListener" />

  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="adminApp">
    <Connector port="7060" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="7643" />
    <Connector port="7609" protocol="AJP/1.3" redirectPort="7643" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

Note that any port number must not conflict with other port numbers.

Now we are going to write startup.sh and shutdown.sh inside the "tomcat/controller" location to start and stop each individual instance of tomcat.

startup.sh 

#!/usr/bin/env sh

app_instance=$1;

BASE_TOMCAT=/location-to-tomcat-parent-directory

export CATALINA_HOME=$BASE_TOMCAT/tomcat
export CATALINA_BASE=$BASE_TOMCAT/$app_instance

$CATALINA_HOME/bin/startup.sh

shutdown.sh

#!/usr/bin/env sh

app_instance=$1;

BASE_TOMCAT=/location-to-tomcat-parent-directory/

export CATALINA_HOME=$BASE_TOMCAT/tomcat
export CATALINA_BASE=$BASE_TOMCAT/$app_instance

$CATALINA_HOME/bin/shutdown.sh

Everything is ready now. Copy paste your app-one.war file into “app-one/webapps” location and app-two.war file into “app-two/webapps” location and rename both files as ROOT.war .

Now you can start your apps using the following commands:

  • ./startup.sh app-one 

  • ./startup.sh app-two 

Our app-one URL, http://localhost:7050, and app-two URL, http://localhost:7060,

stop apps by using following commands:

  • ./shutdown.sh app-one 

  • ./shutdown.sh app-two 

This way, you can run more than one instance in a single Tomcat with different configurations on each instance.

Apache Tomcat

Opinions expressed by DZone contributors are their own.

Related

  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Optimizing Database Connectivity: A Comparative Analysis of Tomcat JDBC vs. HikariCP
  • Deployment of Spring MVC App on a Local Tomcat Server
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook