Question

I want to deploy two applications foo.war and bar.war to the same Tomcat instance. Is it possible for them to listen for connections on different ports, e.g. foo listens on port 81 and bar listens on port 82? If so, how can I configure this? I realise that it is not necessary for the applications to listen on different ports, but that is what I want to achieve.

Also, am I right in saying that if I rename foo.war to ROOT.war such that it runs in the root context, then all requests to this Tomcat instance will be handled by the foo app and therefore bar would have to be deployed to a separate Tomcat instance?

Was it helpful?

Solution

If you want Tomcat to listen to multiple ports, you need to setup a connector for each port. To get each port mapped to a different application, you need need to wrap each connector in a service and create a host with it's own appBase.

Example of service definition in server.xml:

<Service name="foo">
    <Connector port="80" protocol="org.apache.coyote.http11.Http11NioProtocol" />
    <Engine name="Catalina80" defaultHost="localhost">
        <Host name="localhost" appBase="foo" unpackWARs="true" autoDeploy="true" />
    </Engine>
</Service>

<Service name="bar">
    <Connector port="81" protocol="org.apache.coyote.http11.Http11NioProtocol" />
    <Engine name="Catalina81" defaultHost="localhost">
        <Host name="localhost" appBase="bar" unpackWARs="true" autoDeploy="true" />
    </Engine>
</Service>

Instead of dropping the war files in the webapps directory, you need to create the directory foo for port 80 and bar for port 81. Name both war files ROOT.war and drop them in their own base directory. You can of course have multiple apps in each directory if you need.

The directory defined in appBase is relative to the tomcat directory. By using an absolute path, it could be anywhere on your system. From the documentation:

appBase

The Application Base directory for this virtual host. This is the pathname of a directory that may contain web applications to be deployed on this virtual host. You may specify an absolute pathname, or a pathname that is relative to the $CATALINA_BASE directory. [...] If not specified, the default of webapps will be used.

Another option is to keep the default tomcat configuration and use another http server (apache, nginx, lighttpd,...) to map a port to the internal path of a tomcat application.

The root application won't receive requests that match other applications, e.g. /foo/example will go to foo.war, /example/example will go to ROOT.war.

OTHER TIPS

No need to change ports

Juggling multiple incoming requests and outgoing responses across many users using any of multiple running web apps is the purpose of Java Servlet technology. All that traffic can be handled on a single port.

Simply drop both war files into Tomcat's webapps folder. That is all you need to do.

By default, Tomcat expands ("explodes" some say) each war (technically a zip file) into a folder and automatically deploys the app for you. This happens on the fly if Tomcat is already running, or on startup when you launch Tomcat. Some people turn off the auto-deploy feature for production to save Tomcat the work of scanning for new war files.

No need for multiple ports. A Servlet container's job is to examine the URL and determine which Servlet should be invoked.

By default, the name of the war file determines the URL. Given your example:

All the web apps can be served on the same port. Your only concern with ports is if you use Unix-style operating system that protects access to low-numbered ports. This includes Mac OS X, BSD, Linux, and Solaris. Either use a high-numbered port in your URL (Tomcat defaults to 8080), or use port-forwarding to send incoming requests on port 80 (web browsers’ default) to Tomcat’s port (such as 8080).

If you want the war files served by using different domains, learn about "virtual host" settings in Tomcat.

I have successfully configured Tomcat to run apps on multiple ports. I don't know if this is the best way to do this, but I simply duplicated contents of

<Service>...</Service>

in conf/server.xml and changed the ports for the Connector tags and changed the appBase attribute of the Host tag. You control which port your app runs on based upon the appBase that it is deployed to.

I was already doing what Basil mentions above.

As it turned out I needed to wildcard my sites-enabled/blah.conf file, to not reference the first (and only) .war I deployed months before adding another app.

Here's what I wildcarded:

  • DocumentRoot
  • JkMount
  • Directory
  • DirectoryIndex

So fyi, ya'll. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top