Sunday, November 16, 2014

CentOS 7 - How to run Multiple Tomcat Instances on One Server?

Before you run multiple Tomcat instances on one server, you need to understand Tomcat’s directory a little bit. You can config your Tomcat instance to “shared mode” or “separate copies”:
  • 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: Tomcat log files
  • src: Source code used by Tomcat server
  • webapps: All web applications are deployed here
  • work: This is the directory in which Tomcat will place all servlets that are generated from JSPs.

Important Tomcat ports:

  • Connector port: Port for listening to HTTP requests
  • Shutdown port: This port is used when we try to shutdown Tomcat server
  • AJP Connector Port: The Apache JServ Protocol 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

Download Tomcat:
Download tomcat from Apache “http://tomcat.apache.org/download-70.cgi”.

Extract into /opt/tomcat:
# tar -xzvf apache-tomcat-7.0.57.tar.gz
# cp -r apache-tomcat-7.0.57 /opt/tomcat
# cp -r apache-tomcat-7.0.57 /opt/tomcat2

“/opt/tomcat” will be our default Tomcat instance, we will modify the ports in “/opt/tomcat2”.

Modify ports in /opt/tomcat2:
# cd /opt/tomcat2/conf
# vi server.xml
shutdown port:
change
<Server port="8005" shutdown="SHUTDOWN">
to
<Server port="8006” shutdown="SHUTDOWN">

Connector port:
change
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
to
<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444" />

AJP port:
change
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
to
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />

Modify catalina.sh in “/opt/tomcat2/bin”:
# cd /opt/tomcat2/bin
# vi catalina.sh
Add the following two lines:
export CATALINA_HOME=/opt/tomcat2
export CATALINA_BASE=/opt/tomcat2

Create two scripts in /etc/profile.d directory to set env variables for both instance:
# vi /etc/profile.d/script.sh
Add:
#!/bin/bash
CATALINA_HOME=/opt/tomcat
PATH=$CATALINA_HOME/bin:$PATH
export PATH CATALINA_HOME
export CLASSPATH=.

# vi /etc/profile.d/script2.sh
Add:
#!/bin/bash
CATALINA_HOME2=/opt/tomcat2
PATH=$CATALINA_HOME2/bin:$PATH
export PATH CATALINA_HOME2
export CLASSPATH=.

Start two instances:
# $CATALINA_HOME/bin/catalina.sh start
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started.

# $CATALINA_HOME2/bin/catalina.sh start
Using CATALINA_BASE:   /opt/tomcat2
Using CATALINA_HOME:   /opt/tomcat2
Using CATALINA_TMPDIR: /opt/tomcat2/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat2/bin/bootstrap.jar:/opt/tomcat2/bin/tomcat-juli.jar
Tomcat started.

You can verify by checking 8080 and 8081 ports:
# netstat -atnp | grep 808
tcp6       0      0 :::8080                 :::*                    LISTEN      12883/java
tcp6       0      0 :::8081                 :::*                    LISTEN      12912/java

To Stop:
# $CATALINA_HOME/bin/catalina.sh stop
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar

# $CATALINA_HOME2/bin/catalina.sh stop
Using CATALINA_BASE:   /opt/tomcat2
Using CATALINA_HOME:   /opt/tomcat2
Using CATALINA_TMPDIR: /opt/tomcat2/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat2/bin/bootstrap.jar:/opt/tomcat2/bin/tomcat-juli.jar

No comments: