Setting-up Multiple Apache Tomcat Instances In Linux(RHEL/CentOS)


I have already explained you how we can install & configure Apache Tomcat on a Linux server. Sometimes, we need to to have more than one instance of Apache Tomcat running on the same server. This is useful to divide the application load. We can divide the WAR files to be deployed among multiple instances so as to improve performance. So, lets learn how we can add multiple Apache Tomcat Instance. 🙂 But before that, a little explanation of this setup 😉

Multiple Tomcat instances means multiple directories where Tomcat is installed (known as catalina_home & catalina_base) & different ports on which the services listen. So, we will be creating one more directory (or rename the one that we get after unpacking Tomcat installer tarball) & editing the main configuration file server.xml. I assume that you already have one running instance of Tomcat. Refer to this post for more information.

Download the installer. Download the desired Apache Tomcat installer version from official website.

Unpack the tarball. Unpack the contents.

[root@server shashank]# tar -xf apache-tomcat-8.0.15.tar.gz

Rename the unpacked directory or Create a new directory to hold unpacked files. You will find a directory called apache-tomcat-8.0.15 in your current directory. You can choose to rename this or create a new directory & move the contents there.

[root@server shashank]# mv apache-tomcat-8.0.15 apache-tomcat

Edit catalina.sh script. Now we will edit the catalina.sh script so that it knows what is catalina_home & catalina_base. Always a good idea to backup the file(s) before editing 😉

[root@server apache-tomcat]# cp -p bin/catalina.sh bin/catalina.sh.bkp
[root@server apache-tomcat]# vi bin/catalina.sh

Put below content above the text that says “OS specific support”. Make sure to use the path where you unpacked Tomcat.

#Exporting variables for this new instance.
export CATALINA_HOME=/opt/app/apache-tomcat
export CATALINA_BASE=/opt/app/apache-tomcat

Edit Tomcat configuration file. Edit the configuration file server.xml to use correct ports for Tomcat. Showing you the ports that need to be changed below. You can refer to the first instance’s server.xml to find the difference.

[root@server apache-tomcat]# cp -p /opt/app/apache-tomcat/conf/server.xml /opt/app/apache-tomcat/conf/server.xml.bkp
[root@server apache-tomcat]# vi /opt/app/apache-tomcat/conf/server.xml
Server port="8006" shutdown="SHUTDOWN"
Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444"
     A "Connector" using the shared thread pool
    
    Connector executor="tomcatThreadPool"
               port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444" 
   Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation 
    
    Connector port="8444" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
  
Define an AJP 1.3 Connector on port 8009
Connector port="8084" protocol="AJP/1.3" redirectPort="8443"

Start Tomcat. Issue below command to start second instance of Tomcat.

[root@server apache-tomcat]# /opt/app/apache-tomcat/bin/startup.sh
Using CATALINA_BASE: /opt/app/apache-tomcat
Using CATALINA_HOME: /opt/app/apache-tomcat
Using CATALINA_TMPDIR: /opt/app/apache-tomcat/temp
Using JRE_HOME: /opt/app/jdk1.8.0_25
Using CLASSPATH: /opt/app/apache-tomcat/bin/bootstrap.jar:/opt/app/apache-tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@server apache-tomcat]# netstat -tulnp | grep 80
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 12824/java
tcp 0 0 ::ffff:127.0.0.1:8006 :::* LISTEN 32287/java
tcp 0 0 :::8009 :::* LISTEN 12824/java
tcp 0 0 :::8080 :::* LISTEN 12824/java
tcp 0 0 :::8081 :::* LISTEN 32287/java
tcp 0 0 :::8084 :::* LISTEN 32287/java

So, you can see 2 Tomcat instances running on port 8005 & 8006 🙂 Hope, this was useful.

Advertisements

Automate Apache Tomcat Installation on Linux Server Using Shell Script


My work involves installing & managing a lot of Linux servers. At times I have to do same work on numerous servers. One such task is installing Tomcat. In these posts I showed you how to install Apache Tomcat & how to check its version on Linux servers.
In this post, I will show you how you can automate the installation process across any number of servers using a shell script 🙂

My approach is to : –

  • Setup password-less SSH on all servers from one jumpbox machine. Jumpbox is a server from which you will execute this script.
  • Create a list of servers in a text file. All servers are typed line by line.
  • Copy Java & Tomcat installers to all servers in one go from this jumpbox.
  • And finally execute the script.

Below are the commands & script through which I accomplished this. All of these are self-explanatory 😉

for hst in `cat /home/shashank/hosts.txt`; do scp ~shashank/jdk-8u25-linux-x64.tar.gz $hst:/home/shashank; done
for hst in `cat /home/shashank/hosts.txt`; do scp ~shashank/apache-tomcat-8.0.15.tar.gz $hst:/home/shashank; done

What it does above? For loop reads the contents of text file (server IPs) line by line & copies tarballs there.

for hst in `cat /home/shashank/hosts.txt`; do ssh $hst -t 'bash -s' < ~shashank/tomcat_installer.sh; done

The trick above is to use ‘bash -s’. It will accept the script as an argument & execute it. After execution, it will move to next server. Below is my installer script.

Do let me know if it was helpful or not 🙂 I will come up with some more posts soon 🙂

#Apache Tomcat Installer Script
#Author : Shashank Srivastava
#set -x
echo "Logged into `hostname`. Installing here."
#Checking if installer tarballs are present or not. If they are not found, script will print error message & quit.
if [ -f /home/shashank/jdk-8u25-linux-x64.tar.gz ] && [ -f /home/shashank/apache-tomcat-8.0.15.tar.gz ]
then
echo "Unpacking Java installer tarball.......";
sleep 2s
#waiting for 2 seconds to show you what is being done.
sudo tar -xzvf /home/shashank/jdk-8u25-linux-x64.tar.gz;
echo ""
echo "Java tarball unpacked.";
echo ""
echo "Unpacking Tomcat installer tarball.......";
sleep 2s;
sudo tar -xzvf /home/shashank/apache-tomcat-8.0.15.tar.gz;
echo ""
echo "Tomcat tarball unpacked.";
echo ""
echo "Installing Java & Tomcat to /opt/app directory....";
sudo cp -rp /home/shashank/jdk1.8.0_25 /opt/app;
sudo cp -rp /home/shashank/apache-tomcat-8.0.15 /opt/app;
echo ""
echo "Exporting necessary variables......";
export JAVA_HOME=/opt/app/jdk1.8.0_25;
export PATH=$PATH:/opt/app/jdk1.8.0_25/bin:$PATH;
export CATALINA_HOME=/opt/app/apache-tomcat-8.0.15;
echo "Variables exported.";
echo ""
echo $JAVA_HOME;
echo ""
echo $PATH;
echo ""
echo $CATALINA_HOME;
sleep 2s;
echo ""
echo "Checking Java & Tomcat versions.";
echo ""
echo "Java is installed at `which java` Directory";
echo ""
java -version;
echo ""
java -cp $CATALINA_HOME/lib/catalina.jar org.apache.catalina.util.ServerInfo;
echo ""
echo "Starting Tomcat server"
echo ""
cd $CATALINA_HOME/bin
./startup.sh
echo "'
echo "Removing unpacked tarballs from PWD.";
sudo rm -rf /home/shashank/jdk1.8.0_25;
sudo rm -rf /home/shashank/apache-tomcat-8.0.15;
else
echo "Installer tarballs not found in /home/shashank. Please make sure they exist there. Exiting installation process now."
exit
fi

How To Check Apache Tomcat Version on RHEL/CentOS?


In my last post, I explained how to install Tomcat on Linux. At times, you will need to know what version of Tomcat is installed. So below is how to find it out 🙂

Just go the directory where Apache Tomcat is installed & issue below command.

java -cp lib/catalina.jar org.apache.catalina.util.ServerInfo

It will return info like this : –

Server version: Apache Tomcat/8.0.23
Server built: May 19 2015 14:58:38 UTC
Server number: 8.0.23.0
OS Name: Linux
OS Version: 2.6.32-504.el6.x86_64
Architecture: amd64
JVM Version: 1.8.0_25-b17
JVM Vendor: Oracle Corporation

Installing Apache Tomcat on RHEL/CentOS


Apache Tomcat is an Open-Source web server meant to run Java code. Just like Apache serves websites written, e.g, PHP, Tomcat serves websites written in Java. Installing Tomcat on Linux is a whole new experience, compared to if you have done it on Windows 😉 Lets begin this tutorial 🙂

Install Java. Apache Tomcat requires Java to be installed first. So, first install it from its official website.

Download Tomcat. Download Apache Tomcat from its official website. I have installed version 8.0.23. You may choose any compatible version. P.S. This tarball contains all the binaries & software to run Tomcat.

Unpack the Tomcat tarball. Once the tarball has been downloaded, just unpack it to any location where you want it to be installed using command : –

tar -xf apache-tomcat-8.0.23.tar.gz 

Start Tomcat. Move to the Tomcat directory & run below script.

./bin/startup.sh

To stop Tomcat, just issue –

./bin/shutdown.sh

So, that’s it! Apache Tomcat has been installed & running fine. If you thought you will need to click next..next..continue..etc, then you’re wrong 😉 You just need to place the archive(tarball) in the right location & it is installed!! Installation on Linux is far different 😉

Hope you found this post useful 🙂 Do let me know how was it.