Tomcat is a pure JAVA HTTP server to host java applications. While testing these applications, we need multiple instances of Tomcat. We can achieve this as a scale out of Virtual Machines or deploy multiple instances of Tomcat on the same server.
In this article, I want to go through the steps of creating multiple Tomcat instances on the same box. The instances can be the same version or multiple versions, for example, tomcat 6 and 7 running in harmony.
Next, we need to install the JDK on the system,
Note: As of this writing the most up-to-date version of the JDK is version 7u7, and that is what im using for this article. Mind you it is the same process for previous versions.
Lets make the directory /usr/java and go to it:
mkdir -p /usr/java/
Lets make the rpm file that we downloaded into an executable file. I placed my downloaded files in a folder called /temporary:
chmod 700 /temporary/jdk-7u7-linux-x64.rpm
Install it with rpm -i :
rpm -i /temporary/jdk-7u7-linux-x64.rpm
The JDK is now installed, lets install tomcat!
Tomcat is actually very simple install. It is a matter of extracting the files in a location, modifying a few scripts, and running the startup scripts. For those who are not 100% familiar with how tomcat works, it sets up using the environment variables of the user that is executing the startup script.
First things first, what we would like to do, is create a new user called tomcat and make it impossible for it to logon.
useradd -g tomcat -s /usr/sbin/nologin -m -d /home/tomcat tomcat
Now lets extract the files. Remember, our files are in the folder /temporary
tar -xf apache-tomcat-7.0.32.tar.gz
Copy them to the /opt folder
cp /temporary/apache-tomcat-7.0.32 /opt/tomcat7
For every instance, we need to copy the folder over. For this exercise, we will create 2 instances:
cp -rf /opt/tomcat7 /opt/tomcat7-1
cp -rf /opt/tomcat7 /opt/tomcat7-2
Change the owner of the folders to the group tomcat and the user tomcat:
chown -R tomcat:tomcat tomcat7-1
chown -R tomcat:tomcat tomcat7-2
Now we need to modify the startup and shutdown scripts. In order to do so, lets go into the bin folder of each instance.
In this folder, is the startup.sh and shutdown.sh script. We need to modify these scripts to include the right environmental parameters. Add these commands to the startup.sh script:
It should look like this:
Once done, do the same for the shutdown script (shutdown.sh).
Next, in the conf folder, we need to modify the server.xml file to modify the server ports used for tomcat. This is how we do it:
There are two ports needed: (1) the actual http port and (2) the shutdown port. For every instance we will create, these ports need to be different (Later we will see how to use the same ports with another method).
For our first instance, we can leave the defaults, 8080 and 8005. For the next instance, we will change the ports. (I like to add a 100 to the default which means 8180 and 8105 for the second port)
Now to run the instance of Tomcat, lets execute the script as the user tomcat:
su -p -s /bin/sh tomcat startup.sh
Lets check out our separate tomcat instances:
Success, now we are up and operational!
In order to get into the manager, we need to configure the user accounts for it. We need to modify the file tomcat-users.xml in the conf folder:
<user username="admin" password="tomcat" roles="manager-gui"/>
Here we added the role "manager-gui" and applied it to the user "admin" with the password "tomcat".
I mentioned earlier that there is a way to have all your instances of Tomcat to respond on the same http port. We can achieve this using a server called HAProxy. HAProxy is an open source load balancer that you can use to redirect http requests to web servers that are using different ports. For a full breakdown of the configuration, please read my post on HAProxy here.