64

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
DeeStackOverflow
  • 2,741
  • 4
  • 23
  • 23
  • I dont think it is possible unless you have another instance, I have upvoted, let us see if any have other thoughts. – kosa Jan 11 '12 at 17:01
  • I really want to use only one tomcat instance. Our team is all trying to use a single instance going forward. There is something in the server.xml that needs to change I heard but not sure what to do there. – DeeStackOverflow Jan 11 '12 at 17:03

5 Answers5

62

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>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • The above seems to give better results with the log but still has this error: Jan 11, 2012 12:58:13 PM org.apache.catalina.core.StandardContext resourcesStart SEVERE: Error starting static Resources java.lang.IllegalArgumentException: Document base C:\Program Files\Tomcat6\app1\host-manager does not exist or is not a readable directory at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:142) – DeeStackOverflow Jan 11 '12 at 18:02
  • I am going to mark this as an accepted answer; although I could not get the error messages to go away, I could at least get Tomcat to start up. If anybody has time to figure this out please let us know of your results. The other option of course, is to have 2 instances of Tomcat running as pointed out by others in this thread. – DeeStackOverflow Jan 12 '12 at 14:30
  • 2
    Docs are saying that each engine inside specific server should have unique name (http://tomcat.apache.org/tomcat-7.0-doc/config/engine.html) so I vote for @speeves answer – Kangur Nov 25 '14 at 17:02
  • The redirect port is the same for both services in this example, is that intentional? – Bryji Jan 18 '16 at 09:08
  • where we have to keep the directories app1 and app2 ? Inside webapps or outside webapps – Shivkumar Mallesappa May 04 '16 at 07:52
10

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>

(...Repeated 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.

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
speeves
  • 1,358
  • 9
  • 10
  • This helped me. Very nice :) – rocksteady May 30 '16 at 10:47
  • 1
    This answer worked best for me since I wanted a separate set of logs, separate Catalina/localhost/*.xml contexts, separate appBase. I dded a to get separate logs – djb Aug 09 '16 at 01:32
2

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 for 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>
StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Aleja_Vigo
  • 970
  • 7
  • 12
  • The problem is, I don't have apache installed and no plans in our team to do so. – DeeStackOverflow Jan 11 '12 at 17:17
  • In a former position, we ran the the Big-Ip F5s directly into tomcat to remove a point of failure. I _do_ love Apache for many things though :) – speeves Jan 11 '12 at 17:30
  • 1
    This is theoretically possible, but the mapping between "/" and "/app1" makes you crazy. Usually the "mod_proxy_html" is needed as well for such a configuration, and it is quite sophisticated both for installation and configuration. Even if you are using Apache, using two services in the tomcat configuration is still but more convenient way. – 30thh Sep 18 '12 at 07:31
0

Use two different Tomcat instances.

EDIT:

Or configure Tomcat as explained in the answer of this question: Tomcat configuration help: multiple ports not responding

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I tried the suggestion in the link (basically add a new Service and a connector within). Did not work. My appBase is not C:\somepath - the application is deployed in webapps. So what would the appBase be ? – DeeStackOverflow Jan 11 '12 at 17:14
  • How do you want to have two apps with the same name (ROOT) in the same folder? You need to separate the folders used by the two tomcat services. See the tomcat documentation for details. – JB Nizet Jan 11 '12 at 17:23
  • I have folders for each app deployed like this: [tomcathome]/webapps/app1 and [tomcathome]/webapps/app2. The default server.xml allows me to call them as localhost:8080/app1 and localhost:8080/app2. I have copied the appconfig.xml as app1.xml into [tomcathome]/conf/Catalina/localhost. – DeeStackOverflow Jan 11 '12 at 17:36
0

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>
StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
speeves
  • 1,358
  • 9
  • 10
  • Can I just modify server.xml to add 2 new ports and point them to each of the apps under webapps ? – DeeStackOverflow Jan 11 '12 at 17:05
  • We don't want to create 2 instances of tomcat. People here are running two different apps within the same instance of tomcat and sharing libraries. I just want to extend it further by assigning port numbers. – DeeStackOverflow Jan 11 '12 at 17:19