Run and Configure Multiple Instances in a Single Tomcat Server

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.

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.

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:

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 "%r" %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 "%r" %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.warfile 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.