Multiple Grails applications on a single Tomcat instance

Let us consider this scenario, you are now running a grails application on a Tomcat server. Now you want to host an another application in a Tomcat server, for some reasons such as costs or for easy management, you do not want to deploy another server. We can achieve this by configuring Tomcat to host multiple applications in the same server.

This article assumes that you have successfully installed Tomcat and have two or more applications ready to be hosted under a single instance of Tomcat. Installing Tomcat is done the usual way either by installing via the repository or downloading the binary from the official site. Installing via the binary is recommended as we can get a latest/stable version of Tomcat.

Tomcat applications are configured to run on port 8080 by default. This can be altered by tweaking the Tomcat’s configuration file (server.xml) which is crucial to run multiple applications under a single Tomcat instance.

For every application that you need to run under a single instance, you have to edit the server.xml file and add entries for the service. To create an entry for the application in the Tomcat’s configuration file specify the port number, webapps and work directories. Then start Tomcat in the server using the default startup script (startup.sh) located under “bin” directory of your Tomcat installation. Please refer the code below:

<Service name=”example”>
    <Connector port=8090> protocol=”HTTP/1.1″ connectionTimeout=20000″ redirectPort=8443/>
        <Engine name=”myapp” defaultHost=”localhost”>
            <Realm className=”org.apache.catalina.realm.LockOutRealm>
                <Realm className=”org.apache.catalina.realm.UserDatabaseRealm” resourceName=”UserDatabase”/>
            </Realm>
            <Host name=”localhost” appBase=”webapps/app”>
                unpackWARs=true” autoDeploy=true>
                <Valve className=”org.apache.catalina.valves.RemoteIpValve/>
                <Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs” prefix=”localhost_access_log” suffix=”.txt” pattern=%{X-Forwarded-For}i %l %u %t %r %s %b %{User-Agent}i %{Referer}i” resolveHosts=false/>
            </Host>
        </Engine>
</Service>

The above code contains directives for the application named “example” which is configured to run on port 8090. The work directory “myapp” is where the application’s cache is located. The webapps directory “webapps/app” is where the war file has to be placed as ROOT.war.

You can just keep on adding multiple applications just by changing the above mentioned values and appending them to the configuration file as long as the server’s resources (RAM and CPU) support your applications. Make sure the entries like port number, service name and webapps directory does not overlap with the already configured application. To replace a running application, just remove the work directory and all the contents under its webapps directory and place the ROOT.war file. Tomcat will automatically deploy the application by unpacking the war file.

You have now saved yourself from doing a lot of management work along with considerable costs with this type of Tomcat server setup.