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

Create /usr/lib/systemd/system/tomcat-custserv.service and /usr/lib/systemd/system/tomcat-prodserv.service to configure the environment for each instance.

[[email protected] ~]# sudo vi /usr/lib/systemd/system/tomcat-custserv.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/var/lib/apache-tomcat-8.0.52/custserv/temp/tomcat.pid
Environment=CATALINA_HOME=/var/lib/apache-tomcat-8.0.52/custserv
Environment=CATALINA_BASE=/var/lib/apache-tomcat-8.0.52/custserv
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/var/lib/apache-tomcat-8.0.52/custserv/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

Update same configuration for /usr/lib/systemd/system/tomcat-prodserv.service and change path.

Make sure user has created and updated for tomcat folders.

[[email protected] ~]# sudo useradd -s /bin/nologin -g tomcat -d /opt/apache-tomcat-8.0.52 tomcat
[[email protected] ~]# sudo cd /opt/apache-tomcat-8.0.52
[[email protected] ~]# sudo chgrp -R tomcat conf
[[email protected] ~]# sudo chown -R tomcat logs/ temp/ webapps/ work/ bin/ /lib

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

[[email protected] ~]# sudo systemctl start tomcat-prodserv.service
[[email protected] ~]# sudo systemctl start tomcat-custserv.service