How to run different apps on single Tomcat instance behind different ports?

TomcatTomcat6Port

Tomcat Problem Overview


Currently I have 2 web applications app1 and app2 running on Tomcat 6:

I want to configure Tomcat so that they run in root context behind separate ports:

What needs to be done?

Tomcat Solutions


Solution 1 - Tomcat

I think you can configure that in you server.xml file and put 2 services :

<Service name="app1">
   <Connector port="8081" protocol="org.apache.coyote.http11.Http11NioProtocol" 
           connectionTimeout="20000" 
           redirectPort="8443" />
   <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost"  appBase="app1"
        unpackWARs="true" autoDeploy="true">
      </Host>
   </Engine>
</Service>
<Service name="app2">
   <Connector port="8082" protocol="org.apache.coyote.http11.Http11NioProtocol" 
           connectionTimeout="20000" 
           redirectPort="8443" />
   <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost"  appBase="app2"
        unpackWARs="true" autoDeploy="true">
      </Host>
   </Engine>
</Service>

Solution 2 - Tomcat

Another example of adding connectors:

<Service name="reciver">
    <Connector port="8080" maxHttpHeaderSize="8192" maxThreads="10" 
               enableLookups="false" acceptCount="100"
               connectionTimeout="10000" disableUploadTimeout="true" 
               useBodyEncodingForURI="true"/>
    <Engine name="reciver" defaultHost="localhost" jvmRoute="host1">
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                   resourceName="UserDatabase" />
            <Host name="localhost" appBase="webapps" unpackWARs="true"
                  autoDeploy="false" xmlValidation="false"
                  xmlNamespaceAware="false">
                    <Context docBase="browser" path="/browser" reloadable="false"/>
            </Host>
    </Engine>
</Service>
<Service name="reciver2">
    <Connector port="8081" maxHttpHeaderSize="8192" maxThreads="10" 
               enableLookups="false" acceptCount="1"
               connectionTimeout="10000" disableUploadTimeout="true" 
               useBodyEncodingForURI="true" proxyName="example.pt" proxyPort="80"/>
    <Engine name="reciver2" defaultHost="example_app" jvmRoute="host2">
            <Host name="example_app" appBase="test_app/example_app" unpackWARs="true"
                  autoDeploy="false" xmlValidation="false"
                  xmlNamespaceAware="false">
                    <Context docBase="example_app" path="/example_app" reloadable="false"/>
            </Host>
    </Engine>
</Service>
(...Repeted 2 more times.)

Taken from: http://www.coderanch.com/t/84172/Tomcat/listen-multiple-ports

I recommend reading the whole thread, as it talks about performance hits with this configuration, and also possible race conditions.

Solution 3 - Tomcat

Besides running two Tomcat instances and using ROOT application (that has already been said and is a bit poor and ineffective solution) you can achieve it by using Apache + Tomcat. Configuring apache to listen to both ports and forward by IP:Port to different Tomcat applications. But you need a different port por tomcat!

Apache configuration

listen 8080,8081
...
<VirtualHost *:8080>
    ServerName localhost
    ProxyPass / http://localhost:8888/app1
    ProxyPassReverse / http://localhost:8080/app1
</VirtualHost>

<VirtualHost *:8081>
    ServerName localhost
    ProxyPass / http://localhost:8888/app2
    ProxyPassReverse / http://localhost:8080/app2
</VirtualHost>

or

listen 80,81
...
<VirtualHost *:80>
    ServerName localhost
    ProxyPass / http://localhost:8080/app1
    ProxyPassReverse / http://localhost:8080/app1
</VirtualHost>

<VirtualHost *:81>
    ServerName localhost
    ProxyPass / http://localhost:8080/app2
    ProxyPassReverse / http://localhost:8080/app2
</VirtualHost>

Solution 4 - Tomcat

Use two different Tomcat instances.

EDIT:

Or configure Tomcat as explained in the answer of this question: https://stackoverflow.com/questions/4719616/tomcat-configuration-help-multiple-ports-not-responding

Solution 5 - Tomcat

Tomcat runs on the ports specified in:

$CATALINA_HOME/conf/server.xml

As JB Nizet wrote, setup two different instances of tomcat, and configure the port value server.xml appropriately.

$CATALINA_HOME/tomcat-8081/conf/server.xml:

<?xml version='1.0' encoding='utf-8'?>
 <Server port="8081" ... >
  ...
 </Server>

$CATALINA_HOME/tomcat-8082/conf/server.xml:

<?xml version='1.0' encoding='utf-8'?>
 <Server port="8082" ... >
  ...
 </Server>

Solution 6 - Tomcat

I am using IntelliJ and I added a line in the application.properties file and it worked: for web app 1's project add: server.port = 8081 for web app 2's project add: server.port = 8082

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeeStackOverflowView Question on Stackoverflow
Solution 1 - TomcatBenoit MarilleauView Answer on Stackoverflow
Solution 2 - TomcatspeevesView Answer on Stackoverflow
Solution 3 - TomcatAleja_VigoView Answer on Stackoverflow
Solution 4 - TomcatJB NizetView Answer on Stackoverflow
Solution 5 - TomcatspeevesView Answer on Stackoverflow
Solution 6 - TomcatLittle AppleView Answer on Stackoverflow