Monday 23 December 2013

Running Two Tomcat Instances On Same Server (*nix)

Sometimes you want to host applications that cannot run on the same instance of Tomcat because of some incompatibility between the required classes of the two applications, or you might want to deploy the same application multiple times, perhaps for different customers, and you cannot do that on a single instance of Tomcat.


Here’s a brief step by step guide to running more than one instance of Tomcat on a single machine.

Lets first understand Tomcat Directory 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 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.


JDK(Java Development Kit)

For proper execution, Tomcat requires JDK(Java Development Kit). JDK that contains API classes, Java compiler(javac), Java Virtual Machine(JVM) provides the basic tools to compile Java applications and applets.

Install JDK

You can download the latest version of JDK(JDK 7) from Sun's Java web URL's at:
http://java.sun.com/products/archive/index.html

http://java.sun.com/products/archive/j2se/1.4.2_08/index.html

http://java.sun.com/j2se/1.4.2/download.html
Unpack and install JDK in the directory /usr/local/. Now, we need to define the environment variable JAVA_HOME, such that it points to the java installation directory "/usr/local/jdk1.X.X".
Add the following lines to the "/etc/bashrc" or "/etc/profile" file, so that the environment variable can be used by all aplications/users in the server.
JAVA_HOME=/usr/local/jdk1.X.X
export JAVA_HOME

Install Tomcat

The Tomcat complete package is available for download at the following URL :

http://tomcat.apache.org/download-55.cgi
Download the latest version of Tomcat and Install it in the /usr/local directory.
mv apache-tomcat-5.5.26.tar.gz /usr/local
tar -xvzf apache-tomcat-5.5.25.tar.gz
Now, create the following symbolic link.
ln -s /usr/local/apache-tomcat-5.5.25 /usr/local/tomcat
We can then run the Tomcat web server using a seperate user "tomcat". Add the user and group "tomcat".
groupadd tomcat
useradd -g tomcat -c "Tomcat User" -d /usr/local/tomcat tomcat
passwd tomcat <password for user>
Change the ownership of Tomcat installation directories.
chown tomcat:nobody /usr/local/tomcat
chown -R tomcat:nobody /usr/local/apache-tomcat-5.5.25
The installation of Tomcat is now complete. We can start the Tomcat instance using a startup script "/usr/local/tomcat/bin/startup.sh". Also, make sure to define the variable CATALINA_HOME in the script "/usr/local/tomcat/bin/catalina.sh".
CATALINA_HOME=/usr/local/tomcat
The default Tomcat configuration will service HTTP requests on port 8080. You can access this Tomcat instance using the URL http://server_IP:8080/, where server_IP is the address of the machine where we just now installed the Tomcat server.
In order to shut down the Tomcat service, use the script :
/usr/local/tomcat/bun/shutdown.sh
You can access the Tomcat Manager Interface corresponding to this Tomcat installation using the URL:
http://server_IP:8080/manager/
You can also configure the Manager Application Access using a username and password combination that has the role "manager" associated to them. All you have to do to enable this access is edit the Tomcat users configuration file.
/usr/local/tomcat/conf/tomcat-users.xml
By default, the Manager application is completely disabled. To enable access to the Manager web application, add the following entry in /usr/local/tomcat/conf/tomcat-users.xml.
<tomcat-users>
<role rolename="manager"/>
<user username="admin" password="tomcat" roles="manager"/>
</tomcat-users>
Once you restart the Tomcat service after this, you will be able to access the Manager interface using the login details admin/tomcat.


Additional Tomcat Instances

We have already installed one instance of Tomcat in the server.
We can still install multiple instances of Tomcat using the environment variable CATALINA_HOME. Each instance will use its own startup/shutdown scripts, configuration files, log files, etc.

Configure Second Instance

The first instance of Tomcat is installed inside the directory "/usr/local/tomcat". In order to configure an additional instance, create a new directory "/usr/local/tomcat1", assign correct permissions and copy all contents from the old directory to the new one.
mkdir /usr/local/tomcat1
chown tomcat:tomcat /usr/local/tomcat1
cp -pr /usr/local/tomcat/* /usr/local/tomcat1/
The Tomcat configuration is defined by the contents of the server.xml file that is found in the Tomcat's configuration($CATALINA_HOME/conf) directory. In order to configure an additional instance, modify the tomcat configuration file corresponding to the second instance "/usr/local/tomcat1/conf/server.xml"
Change AJP connector port from 8080 to 8081, because the first instance is already using 8080. The AJP connector port is used by Apache to forward requests. Also change the SHUTDOWN port from 8005 to 8115, because the the first instance already is using port 8005. After modifications, it should look something like this:
<Server port="8115" shutdown="SHUTDOWN">
.....
<Connector port="8081" protocol="HTTP/1.1"

connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />



We are now done with the configuration of the second instance of Tomcat. We can start this instance using the startup script /usr/local/tomcat1/bin/startup.sh. Before that, it is required that you define the variable CATALINA_HOME as /usr/local/tomcat1 in the script /usr/local/tomcat1/bin/catalina.sh.

JAVA_HOME=/usr/local/jdk
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/local/jakarta/tomcat2/apache-tomcat-7.0.47
CATALINA_BASE=/usr/local/jakarta/tomcat
CONNECTOR_PORT="8081"

Note: Paths can vary in your case
You can access this instance of Tomcat using the URL http://server_IP:8081 and the manager interface at http://server_IP:8081/manager/.

Similar to how we did initially, here too, you will need to manually add the "manager" user in the user configuration file /usr/local/tomcat1/conf/tomcat-users.xml.

Create following two scripts to run Tomcat as a service
  1. Create /etc/init.d/tomcat 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/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/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



No comments:

Post a Comment