Run Multiple Tomcat Instances on One Server
Sometimes we may want to have multiple tomcat instances running on one server, because they may be using different Java version, or better performance, or load balance, etc… To achieve this we need to understand Tomcat structure first and then set up the configuration for different tomcat instances or services. Below instructions are based on Tomcat 7 running on CentOS 6.5.
Tomcat Structure
When creating multiple instances of tomcat server, we need to play with the folders inside the server. These folders contain the actual scripts and code for the server. We have the option to either use the code base for tomcat in shared mode where all tomcat instances refer to the same physical code or we can create separate copies of these folder for each tomcat instance.
- /bin : This directory contains the startup and shutdown scripts for both Windows and Linux.
- /conf : This directory contains the main configuration files for Tomcat. The two most important are the server.xml and the global web.xml .
- /server : This directory contains the Tomcat Java Archive files.
- /lib : This directory contains Java Archive files that Tomcat is dependent upon.
- /logs : This directory contains Tomcat’s log files.
- /src : This directory contains the source code used by the Tomcat server. Once Tomcat is released, it will probably contain interfaces and abstract classes only.
- /webapps : All web applications are deployed in this directory; it contains the WAR file.
- /work : This is the directory in which Tomcat will place all servlets that are generated from JSPs. If you want to see exactly how a particular JSP is interpreted, look in this directory.
Tomcat Running Ports
Having a good understanding of tomcat ports is essential to manage the multiple instances of the same server installation. These ports are used by tomcat for startup, deployment and shutdown operations. The detail of each port is as:
- Connector Port : This is the port where Apache Tomcat listen for the HTTP requests.
- Shutdown Port : This port is used when we try to shutdown the Apache Tomcat Server.
- AJP (Apache JServ Protocol) Connector Port : The Apache JServ Protocol (AJP) is a binary protocol that can conduct inbound requests from a web server through to an application server that sits behind the web server.
- Redirect Port : Any redirection happening inside Apache Tomcat will happen through this port. In Apache TOMCAT there are two instance where redirectPort is mentioned. First one is for the Apache TOMCAT server and other one is for the AJP port.
We need to make sure all tomcat instances use different ports above of HTTP, HTTPS, AJP, Redirect Port and SHUTDOWN. The configuration is in the file of $CATALINA_HOME/conf/server.xml.
Turn Tomcat into a Service
Essentially we want to have all tomcat instances (if only one as well) running as a service after we have the tomcat configured correctly. By doing this we can easily make tomcat run with different java version and start automatically with system bootup.
Service Script
We need to create the individual service script in /etc/init.d/ for each of the services we want tomcat running with. In the scripting file we need to specify the JAVA_HOME and CATALINA_HOME if the instance needs to run differently with the system wide settings. Here is a sample service script file named as my-service (I store my tomcat under /opt/my-service/):
#!/bin/bash
# description: my-service Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
name=my-service
JAVA_HOME=/etc/alternatives/java_sdk_1.7.0
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/opt/${name}/apache-tomcat-7.0.57
# have tomcat process running under user tomcat
# we need to make this script is runnable for user tomcat
# and tomcat location is writable for user tomcat
case $1 in
start)
/bin/su tomcat $CATALINA_HOME/bin/startup.sh
;;
stop)
/bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
;;
restart)
/bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
/bin/su tomcat $CATALINA_HOME/bin/startup.sh
;;
esac
exit 0
Also the script and tomcat installation need to be runnable and writable by tomcat user:
chmod 755 /etc/init.d/my-service
chown tomcat <tomcat_installation>
By now the service can easily be manipulated using service my-service start|stop|restart.
Service Configuration
We may also want to the service start automatically with system bootup. We need to add my-service to the chkconfig and make it automatically start:
chkconfig –add my-service
chkconfig –level 234 on
chkconfig –list my-service will show the config for my-service:
my-service 0:off 1:off 2:on 3:on 4:on 5:off 6:off
System V Runlevels
A little words about runlevels of SysV init, there 6 six runlevels of CentOS:
- 0 – Halt
- 1 – Single-user text mode
- 2 – Not used (user-definable)
- 3 – Full multi-user text mode
- 4 – Not used (user-definable)
- 5 – Full multi-user graphical mode (with an X-based login screen)
- 6 – Reboot
In general, users operate CentOS at runlevel 3 or 5 – both full multi-user modes.