Total Pageviews

Tuesday, 20 May 2014

How to run multiple Tomcat instances on single Linux server?

If you are a developer then you will find yourself in a situation where you have to run multiple instances of Tomcat server on a single machine. The information available on the internet may not be precise and hence I have listed down the step-by-step guide to achieve it.
The configuration used for the illustration is Linux CentOS-6.X server.

  • /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 server 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 start-up, deployment and shut-down 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.
  • Step 1:
    Download Tomcat from http://tomcat.apache.org/download-60.cgi
  • Step 2:
    Extract it into two different folders, let’s say /opt/tomcat1 and /opt/tomcat2
  • Step 3:
    Keep tomcat1 instance as is and change following things in tomcat2 instance
    Edit /opt/tomcat2/conf/server.xml and change port number
    <server port="8105" shutdown="SHUTDOWN">
    .....
    <Connector port="8181" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />
    .....
    <Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
  • Step 4:
    Create following two scripts to run Tomcat as a service
    1. Create /etc/init.d/tomcat1 with following instructions

      #!/bin/bash
      # description: Tomcat Start Stop Restart
      # processname: tomcat
      # chkconfig: 234 20 80
      JAVA_HOME=/usr/lib/jvm/jre-openjdk
      export JAVA_HOME
      PATH=$JAVA_HOME/bin:$PATH
      export PATH
      CATALINA_HOME=/opt/tomcat1
      case $1 in
      start)
      sh $CATALINA_HOME/bin/startup.sh
      ;;
      stop)
      sh $CATALINA_HOME/bin/shutdown.sh
      ;;
      restart)
      sh $CATALINA_HOME/bin/shutdown.sh
      sh $CATALINA_HOME/bin/startup.sh
      ;;
      esac
      exit 0
    2. Create /etc/init.d/tomcat2 with following instructions

      #!/bin/bash
      # description: Tomcat Start Stop Restart
      # processname: tomcat
      # chkconfig: 234 20 80
      JAVA_HOME=/usr/lib/jvm/jre-openjdk
      export JAVA_HOME
      PATH=$JAVA_HOME/bin:$PATH
      export PATH
      CATALINA_HOME=/opt/tomcat2
      case $1 in
      start)
      sh $CATALINA_HOME/bin/startup.sh
      ;;
      stop)
      sh $CATALINA_HOME/bin/shutdown.sh
      ;;
      restart)
      sh $CATALINA_HOME/bin/shutdown.sh
      sh $CATALINA_HOME/bin/startup.sh
      ;;
      esac
      exit 0
  • Step 5:
    Start/Stop Tomcat service
    Service tomcat1 start/stop/restart
    Service tomcat2 start/stop/restart
  • Step 6:
    Add Tomcat service in startup
    chkconfig tomcat1 on
    chkconfig tomcat2 on
This will enable you to use two instances of Tomcat on a single machine.

No comments:

Post a Comment