Running Multiple Instances of Tomcat in a Server

Apache Tomcat is a web server and servlet container that is used to serve Java applications. We can install more than one tomcat instance on a server to run different profiles.

Prerequisite:
A proper version of Java should be installed on Server. (Java 8 is recommended)

Step 1: Create a new Group:

sudo groupadd tomcat_one

Step 2: Create a new user and add that user to the above-created group:

sudo useradd -M -s /bin/nologin -g tomcat_one -d /opt/tomcat_one tomcat_one

Step 3: Download the tar file for the required version of Tomcat:

wget http://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz
tar -xzvf apache-tomcat-8.5.20.tar.gz
mv apache-tomcat-8.5.20.tar.gz/* /opt/tomcat_one
cd /opt/tomcat_one

Step 4: Update all the necessary permissions:

sudo chgrp -R tomcat_one /opt/tomcat_one
sudo chmod -R g+r conf
sudo chmod g+x conf
sudo chown -R tomcat webapps/ work/ temp/ logs/

Step 5: Now for running downloaded tomcat as a service, we need to create a file in /etc/init.d :

nano tomcat_one

After opening this file, Paste the below code with required change:

#!/bin/bash
# chkconfig: — 80 20
# Provides: tomcat_uat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Tomcat_uat
# Short-Description: start and stop tomcat
#. /etc/rc.d/init.d/functions
export JAVA_HOME=/usr/java/default
export JAVA_OPTS=”-Dfile.encoding=UTF-8 \
-Dnet.sf.ehcache.skipUpdateCheck=true \
-XX:+UseConcMarkSweepGC \
-XX:+CMSClassUnloadingEnabled \
-XX:+UseParNewGC”
export PATH=$JAVA_HOME/bin:$PATH #Java Home Path
TOMCAT_HOME=/opt/tomcat_one #Tomcat Directory Path
TOMCAT_USER=tomcat_one #Tomcat User
SHUTDOWN_WAIT=20
tomcat_pid() {
echo `lsof -n -i :8080 | grep LISTEN | awk ‘{ print $2 }’`
}
start() {
pid=$(tomcat_pid)
if [ -n “$pid” ]
then
echo “Tomcat is already running (pid: $pid)”
else
# Start tomcat
echo “Starting tomcat”
ulimit -n 100000
umask 007
/bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/startup.sh
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n “$pid” ]
then
echo “Stoping Tomcat”
/bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/shutdown.sh
let kwait=$SHUTDOWN_WAIT
count=0;
until [ `ps -p $pid | grep -c $pid` = ‘0’ ] || [ $count -gt $kwait ]
do
echo -n -e “\nwaiting for processes to exit”;
sleep 1
let count=$count+1;
done
if [ $count -gt $kwait ]; then
echo -n -e “\nkilling processes which didn’t stop after $SHUTDOWN_WAIT seconds”
kill -9 $pid
fi
else
echo “Tomcat_one is not running”
fi
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
pid=$(tomcat_pid)
if [ -n “$pid” ]
then
echo “Tomcat_one is running with pid: $pid”
else
echo “Tomcat_one is not running”
fi
;;
esac
exit 0

Step 6: Now change the permissions and service at startup:

chmod 755 tomcat_one 
chkconfig — add tomcat_one
chkconfig — level 234 tomcat_one on
chkconfig — list tomcat_one

This is how we can run multiple tomcat server on the same server and configure them to run as a service.

service tomcat_one start/stop/restart

References:

  1. https://www.openprogrammer.info/2015/06/14/how-to-install-java-8-and-tomcat-8-on-centos-6-as-service/
  2. https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-centos-7

Thanks