How to Run Multiple Web Applications in One Apache Tomcat Server

Apache Tomcat is an open source web container which allows you to deploy Java Servlets, JSP and Web Sockets to run a web server powered by Java code. It also can be identified as a cross-platform servlet container or a web container.

Simply, Tomcat is a very popular among many industrial stakeholders due to many advantages over other web containers on the market. You can create web archives from your Java project and simply deploy it within a tomcat container to host an HTTP web server coded by Java. Industries choose apache tomcat over other container due to following advantages.

  1. Light weight.
  2. Widely Used.
  3. Much faster than other containers.
  4. Easy to configure.
  5. Very flexible.

Usually, apache tomcat is a user-friendly product which gives the engineers space to deploy their WAR artifacts (Web ARchives) with a minimal configuration changes.

This post is targeting an audience which is already using tomcat and know how to start and use apache tomcat engine.

In apache tomcat, WARs should be put in to the webapps directory which the container deploys them by default. Simply, webapps directory acts as the main container of the Java code for tomcat to deploy it as a web server.

In a scenario which we need to host more than one web servers from a single tomcat container, you can use this post as a guide to accomplish it. I’m going to show you how to deploy multiple web applications or two web servers within one tomcat from this article.

Prerequisites: Java should be installed in the server. Preferably 1.7.x or above. In this tutorial I have Java 1.7 installed since I use the tomcat version 8.0.37.

You can install Java using your package manager such yum or apt as shown:

# yum install java              [On CentOS based Systems]
# apt-get install default-jre   [On Debian based Systems]

Step 1: Install Apache Tomcat Server

1. First create a separate tomcat user using root account.

# useradd tomcat
# passwd tomcat

Now login as tomcat user and download the latest apache tomcat bundle from the official site here: https://tomcat.apache.org/ or alternatively, you can use following wget command to download directly in the terminal.

In this case, I download Apache Tomcat, 8.5.5, which is one of the latest stable versions released by now.

$ wget http://redrockdigimark.com/apachemirror/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz

2. Once file has been downloaded, uncompress the content using tar command and view the directory structure as shown:

$ tar -xvf apache-tomcat-8.5.5.tar.gz
$ cd apache-tomcat-8.5.5/
$ ls -l
Apache Tomcat – Directory Listing
total 112
drwxr-x---. 2 tomcat tomcat  4096 Sep 29 11:26 bin
drwx------. 2 tomcat tomcat  4096 Sep  1 01:23 conf
drwxr-x---. 2 tomcat tomcat  4096 Sep 29 11:26 lib
-rw-r-----. 1 tomcat tomcat 57092 Sep  1 01:23 LICENSE
drwxr-x---. 2 tomcat tomcat  4096 Sep  1 01:21 logs
-rw-r-----. 1 tomcat tomcat  1723 Sep  1 01:23 NOTICE
-rw-r-----. 1 tomcat tomcat  7063 Sep  1 01:23 RELEASE-NOTES
-rw-r-----. 1 tomcat tomcat 15946 Sep  1 01:23 RUNNING.txt
drwxr-x---. 2 tomcat tomcat  4096 Sep 29 11:26 temp
drwxr-x---. 7 tomcat tomcat  4096 Sep  1 01:22 webapps
drwxr-x---. 2 tomcat tomcat  4096 Sep  1 01:21 work

Step 2: Configure Apache Tomcat Server

3. The configuration change that we are looking for is lying within the conf directory, is being used to place all the configuration files that helps tomcat to start up.

The content of the conf directory looks like below.

$ cd conf/
$ ls -l
conf – Directory Listing
total 224
-rw-------. 1 tomcat tomcat  12502 Sep  1 01:23 catalina.policy
-rw-------. 1 tomcat tomcat   7203 Sep  1 01:23 catalina.properties
-rw-------. 1 tomcat tomcat   1338 Sep  1 01:23 context.xml
-rw-------. 1 tomcat tomcat   1149 Sep  1 01:23 jaspic-providers.xml
-rw-------. 1 tomcat tomcat   2358 Sep  1 01:23 jaspic-providers.xsd
-rw-------. 1 tomcat tomcat   3622 Sep  1 01:23 logging.properties
-rw-------. 1 tomcat tomcat   7283 Sep  1 01:23 server.xml
-rw-------. 1 tomcat tomcat   2164 Sep  1 01:23 tomcat-users.xml
-rw-------. 1 tomcat tomcat   2633 Sep  1 01:23 tomcat-users.xsd
-rw-------. 1 tomcat tomcat 168133 Sep  1 01:23 web.xml

4. In this case, what’s important to me is the server.xml file. So I’m not going to do an in-depth explanation about other files or directories.

The server.xml is the configuration file which tells tomcat that which port to start it up, which directory content to deploy and many more main and basic configurations.

It basically looks like below after you open the file.

$ vim server.xml
Apache Tomcat Server.xml Configuration
Apache Tomcat Server.xml Configuration

Step 3: Deploying Web Apps in Apache Tomcat

5. Now we will deploy a new web application in Apache tomcat, first find the place where the service tag is closed </service> and insert below lines after the first closed service tag.

Server.xml – New Web App Entry
<Service name="webapps2">
    <Connector port="7070" maxHttpHeaderSize="7192"
        maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
        enableLookups="false" redirectPort="7443" acceptCount="100"
        connectionTimeout="20000" disableUploadTimeout="true" />
        <Connector port="7072" 
        enableLookups="false" redirectPort="7043" protocol="AJP/1.3" />

    <Engine name="webapps2" defaultHost="localhost">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
            resourceName="UserDatabase"/>
            <Host name="localhost" appBase="webapps2"
                unpackWARs="true" autoDeploy="true"
                 xmlValidation="false" xmlNamespaceAware="false">
            </Host>
    </Engine>
</Service>
Deploy Web Applications in Tomcat
Deploy Web Applications in Tomcat

As you see, I’ve changed the connector port to 7070 in the newly inserted entry since the default tomcat starts with port 8080. After setting this up completely there will be two web servers running under the ports 8080 and 7070.

6. After saving the change made to server.xml, create a directory in the apache called webapps2 within the apache main.

$ cd /home/tomcat/apache-tomcat-8.5.5/
$ mkdir webapps2

If you observe the server.xml new entry I have provided, you should see that the service name, app base and the engine is named as webapps2. That’s the reason I created the directory named webapps2. You can create one as you desire, but make sure you make changes to the entry as it is needed.

7. To make sure the second web server is up and running, I copied the content of the webapps directory to webapps2 directory.

$ cp -r webapps/* webapps2/

8. Now the exciting part. We are going to start the server and see whether if it is working. Go to bin directory and execute the startup.sh script. You can view the logs in the catalina.out file resides in the logs directory.

$ cd bin/
$ ./startup.sh
startup.sh – Output
Using CATALINA_BASE:   /home/tomcat/apache-tomcat-8.5.5
Using CATALINA_HOME:   /home/tomcat/apache-tomcat-8.5.5
Using CATALINA_TMPDIR: /home/tomcat/apache-tomcat-8.5.5/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /home/tomcat/apache-tomcat-8.5.5/bin/bootstrap.jar:/home/tomcat/apache-tomcat-8.5.5/bin/tomcat-juli.jar
Tomcat started.

9. If you refer the logs you will be able to see that both webapps and webapps2 are deployed and the app is started up without any issue.

$ cd logs/
$ tail -25f catalina.out 
catalina.out – Verifying Apache Tomcat Logs
29-Sep-2016 12:13:51.210 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/tomcat/apache-tomcat-8.5.5/webapps/examples
29-Sep-2016 12:13:51.661 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /home/tomcat/apache-tomcat-8.5.5/webapps/examples has finished in 452 ms
29-Sep-2016 12:13:51.664 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/tomcat/apache-tomcat-8.5.5/webapps/docs
29-Sep-2016 12:13:51.703 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /home/tomcat/apache-tomcat-8.5.5/webapps/docs has finished in 39 ms
29-Sep-2016 12:13:51.704 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/tomcat/apache-tomcat-8.5.5/webapps/host-manager
29-Sep-2016 12:13:51.744 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /home/tomcat/apache-tomcat-8.5.5/webapps/host-manager has finished in 39 ms
29-Sep-2016 12:13:51.748 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080]
29-Sep-2016 12:13:51.767 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009]
29-Sep-2016 12:13:51.768 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service webapps2
29-Sep-2016 12:13:51.768 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.5
29-Sep-2016 12:13:51.777 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/manager
29-Sep-2016 12:13:51.879 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/manager has finished in 102 ms
29-Sep-2016 12:13:51.879 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/ROOT
29-Sep-2016 12:13:51.915 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/ROOT has finished in 35 ms
29-Sep-2016 12:13:51.927 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/examples
29-Sep-2016 12:13:52.323 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
29-Sep-2016 12:13:52.337 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
29-Sep-2016 12:13:52.341 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/examples has finished in 414 ms
29-Sep-2016 12:13:52.341 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/docs
29-Sep-2016 12:13:52.371 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/docs has finished in 29 ms
29-Sep-2016 12:13:52.371 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/host-manager
29-Sep-2016 12:13:52.417 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /home/tomcat/apache-tomcat-8.5.5/webapps2/host-manager has finished in 46 ms
...

10. In this scenario, the IP of the server I used is 172.16.1.39 and you can see I could start two web servers up within a single tomcat container.

http://172.16.1.39:8080   [1st Web App]
http://172.16.1.39:7070   [2nd Web App]

Hope you all find this article useful and enjoyable. Keep in touch with TecMint and feel free to reach me out for any queries regarding this article.

Thilina Uvindasiri
I'm a BSc Special (Hons) graduate in Information Technology and works as an Engineer-Systems. Love to work, explore and research Linux and play rugby as a hobby.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Leave a Reply to James Lim Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.