Configure multiple Tomcat instances on single server

Learn how to set up several different instances of an application on your Tomcat server, and manipulate each of these instances independently. This article will explain to you how to configure multiple Tomcat instances on single server.

Many times we come to a situation where we need to modify the server configuration such that it is specific to an application. And if we got more than one such applications and we want each application to have it’s own defined configuration options, then it needs some sort of configuration. In this tutorial, I am going to discuss the changes you should make to have different instances of tomcat for each application.

multiple-apache-tomcat-instances

Install JAVA

[[email protected] ~]# sudo yum install java-1.8.0-openjdk.x86_64
[[email protected] ~]# sudo java -version
openjdk version “1.8.0_141”
OpenJDK Runtime Environment (build 1.8.0_141-b16)
OpenJDK 64-Bit Server VM (build 25.141-b16, mixed mode)

[[email protected] ~]# sudo cd /opt/; wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.52/bin/apache-tomcat-8.0.52.tar.gz
[[email protected] ~]# sudo tar -zxvf apache-tomcat-8.0.52.tar.gz
[[email protected] ~]# sudo cd apache-tomcat-8.0.52

Create the subdirectories of each instance directory.

[[email protected] ~]# sudo mkdir /var/lib/apache-tomcat-8.0.52

[[email protected] ~]# sudo mkdir -p /var/lib/apache-tomcat-8.0.52/{custserv,prodserv}

[[email protected] ~]# sudo mkdir -p /var/lib/apache-tomcat-8.0.52/custserv/{logs,conf,temp,webapps,work}
[[email protected] ~]# sudo mkdir -p /var/lib/apache-tomcat-8.0.52/prodserv/{logs,conf,temp,webapps,work}

Copy the conf directory from the default Tomcat installation into each instance.

[[email protected] ~]# sudo cp -r /opt/apache-tomcat-8.0.52/conf/* /var/lib/apache-tomcat-8.0.52/custserv/conf
[[email protected] ~]# sudo cp -r /opt/apache-tomcat-8.0.52/conf/* /var/lib/apache-tomcat-8.0.52/prodserv/conf

Create symbolic links to the default bin and lib locations.

[[email protected] ~]# sudo ln -s /opt/apache-tomcat-8.0.52/bin /var/lib/apache-tomcat-8.0.52/custserv/bin
[[email protected] ~]# sudo ln -s /opt/apache-tomcat-8.0.52/bin /var/lib/apache-tomcat-8.0.52/prodserv/bin

[[email protected] ~]# sudo ln -s /opt/apache-tomcat-8.0.52/lib /var/lib/apache-tomcat-8.0.52/custserv/lib
[[email protected] ~]# sudo ln -s /opt/apache-tomcat-8.0.52/lib /var/lib/apache-tomcat-8.0.52/prodserv/lib

 

Change default ports in prodserv/conf/server.xml and make sure ports 8080,8005,8009 are not conflicting with custserv/conf/server.xml.  We changed default ports in custserv/conf/server.xml.

Now, we are going to create the user and group of Apache, for that we are going to execute the following:

[[email protected] ~]# sudo groupadd tomcat 

[[email protected] ~]# sudo useradd -s /bin/false -g tomcat -d /opt/apache-tomcat-8.0.52/ tomcat

 

Now, let’s change the owner of the tomcat directory to the user and tomcat group:

[[email protected] ~]# sudo chown -R tomcat.tomcat  /opt/apache-tomcat-8.0.52/  /var/lib/apache-tomcat-8.0.52/custserv/ /var/lib/apache-tomcat-8.0.52/prodserv

Start Tomcat Services

we will execute a test to make sure that everything is working the way we want, for this we will execute the following:

[[email protected] ~]# sudo cd /var/lib/apache-tomcat-8.0.52/prodserv/bin

[[email protected] ~]# sudo ./startup.sh

We can see that everything is correct in the “Tomcat started” line. Tomcat runs by default on port 8080.

[[email protected] ~]# sudo cd /var/lib/apache-tomcat-8.0.52/custserv/bin

[[email protected] ~]# sudo ./startup.sh

We have changed the default tomcat port to 8081.

Enable ports 8080 and 8081 on your Firewall.

Create a sample page on your webapps directory /var/lib/apache-tomcat-8.0.52/custserv/webapps/sample and access Apache Tomcat service to validate its installation.

http://<host>:8080/sample and http://<host>:8081/sample