How to Install multiple tomcat in Linux


This article discusses how to install Apache Tomcat 6.0 (6.0.18) on  Linux . Additionally it shows how to setup multiple Tomcat JVM instances on a single Linux server.

Installing Java Runtime Environment

To run Tomcat, you need Java Standard Edition (Java SE), also known as the JDK.

You can download SUN’s latest Java JDKs at: http://java.sun.com/javase/downloads/index.jsp.
Download the binary file to /tmp and install it as follows as root:

[root@server~]# mkdir -p /usr/java

[root@server~]# cd /usr/java

[root@server~]# chmod 700 /tmp/jdk-6u10-linux-x64.bin

[root@server~]# /tmp/jdk-6u10-linux-x64.bin

[root@server~]# export JAVA_HOME=/usr/java/jdk1.6.0_10

[root@server~]# export PATH=$JAVA_HOME/bin:$PATH

[root@server~]# which java

/usr/java/jdk1.6.0_10/bin/java

[root@server~]# java -version

java version “1.6.0_10”

Java(TM) SE Runtime Environment (build 1.6.0_10-b33)

Installing Tomcat Software

Download the latest Tomcat version from http://tomcat.apache.org/

Installing Tomcat from a binary release (tar file) requires manual creation of the Tomcat user account. This is not necessary if you install the Tomcat RPM package on a Linux system that supports RPMs.

Create a user account with no login shell for running the Tomcat server:

[root@server~]# groupadd tomcat

[root@server~]# useradd -g tomcat -s /sbin/nologin -m -d /home/tomcat tomcat

Next extract the tar file to /var/lib and changed the ownership of all files and directories to tomcat:

[root@server~]# cd /var/lib

[root@server~]# tar zxvf /tmp/apache-tomcat-6.0.18.tar.gz

[root@server~]# chown -R tomcat.tomcat /var/lib/apache-tomcat-6.0.18

To get the Tomcat version of the newly installed Tomcat, run:

[root@server~]# /var/lib/apache-tomcat-6.0.18/bin/version.sh

Using CATALINA_BASE:   /var/lib/apache-tomcat-6.0.18

Using CATALINA_HOME:   /var/lib/apache-tomcat-6.0.18

Using CATALINA_TMPDIR: /var/lib/apache-tomcat-6.0.18/temp

Using JRE_HOME:       /usr

Server version: Apache Tomcat/6.0.18

Server built:   Jul 22 2008 02:00:36

Server number:  6.0.18.0

Starting/Stopping Tomcat

now try to startup the Tomcat server to see whether the default Tomcat home page is being displayed.

For security reasons don’t run the Tomcat server as user root but as tomcat which was created with no login shell. Therefore, to run Tomcat use the su command with the -p option to preserves all the environment variables when switching to tomcat and since the tomcat account has no login shell, it needs to be specified with the -s option.

[root@server~]# export JAVA_HOME=/usr/java/jdk1.6.0_10

[root@server~]# export PATH=$JAVA_HOME/bin:$PATH

[root@server~]# export CATALINA_HOME=/var/lib/apache-tomcat-6.0.18

[root@server~]# export CATALINA_BASE=/var/lib/apache-tomcat-6.0.18

[root@server~]# su -p -s /bin/sh tomcat $CATALINA_HOME/bin/startup.sh

Using CATALINA_BASE:                /var/lib/apache-tomcat-6.0.18

Using CATALINA_HOME:               /var/lib/apache-tomcat-6.0.18

Using CATALINA_TMPDIR:           /var/lib/apache-tomcat-6.0.18/temp

Using JRE_HOME:                             /usr/java/jdk1.6.0_10

Now verify that Tomcat was started successfully by opening the URL http://localhost:8080

Note that you should also be able to use the name of your server instead of localhost. Once you opened the URL in your browser you should see Tomcat’s Congratulation page. If you don’t see the page, check the log files under $CATALINA_HOME/logs.

Before you continue with the next steps, make sure to shut down Tomcat since we want to run the Tomcat server out of a separate application directory.

[root@server~]# su -p -s /bin/sh tomcat $CATALINA_HOME/bin/shutdown.sh

 Switching to Tomcat User Account
Since for security reasons the tomcat user has no login shell, it needs to be specified with the -s option when switching from root to tomcat:

[root@server~]# su – -s /bin/sh tomcat

$ id

uid=1001(tomcat) gid=1001(tomcat) groups=1001(tomcat)

$

Setting Up First Tomcat JVM Instance

It is recommended not to store the web applications’ files in Tomcat’s distribution directory tree. For example, having a separate directory makes Tomcat upgrades easier since it won’t overwrite configuration files like server.xml.

If you want to run two Tomcat instances concurrently on a single Linux server, two separate directories are needed anyway. It should be noted here that it’s also possible to run multiple web applications per Tomcat JVM instance.

This HOWTO shows the creation and configuration of one web application for each Tomcat instance.

Setting up Directories and Files
In the following example, setup the first Tomcat JVM instance under the base directory /opt/tomcat-instance/sales.example.com. It’s a good practice to name the base directory after the site name, in this example sales.example.com.

Creating a new base directory for a new instance requires the creation and copying of various directories and configuration files. Execute the following commands as root:

[root@server~]# mkdir -p /opt/tomcat-instance/sales.example.com

[root@server~]# cd /opt/tomcat-instance/sales.example.com

[root@server~]# cp -a /var/lib/apache-tomcat-6.0.18/conf .

[root@server~]# mkdir common logs temp server shared webapps work

[root@server~]# chown -R tomcat:tomcat /opt/tomcat-instance

Most of the remaining steps are executed as the tomcat user. So make sure you switch from root to tomcat:

[root@server~]# su – -s /bin/sh tomcat

Next creat an environment file for the new Tomcat instance. This will be useful for easily setting the environment variables when starting/stopping the new Tomcat instance:

$ cat > /opt/tomcat-instance/sales.env << EOF

export JAVA_HOME=/usr/java/jdk1.6.0_10

export PATH=\$JAVA_HOME/bin:\$PATH

export CATALINA_HOME=/var/lib/apache-tomcat-6.0.18

export CATALINA_BASE=/opt/tomcat-instance/sales.example.com

EOF

$

CATALINA_HOME is the base directory of Tomcat that contains all the libraries, scripts etc. for Tomcat. This is the parent directory of the extracted Tomcat tar file.
CATALINA_BASE is the base directory of the new Tomcat instance, which in this example points to /opt/tomcat-instance/sales.example.com.

Configuring Tomcat Network Ports
Since this is the first Tomcat instance that’s being created here, the default port numbers can be left unchanged in $CATALINA_BASE/conf/server.xml (/opt/tomcat-instance/sales.example.com/conf/server.xml):

<Server port=”8005″ shutdown=”SHUTDOWN”>

<Connector port=”8080″ protocol=”HTTP/1.1″

connectionTimeout=”20000″

redirectPort=”8443″ />

<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />

However, these port numbers will have to be changed for the second Tomcat instance though.

Starting First Tomcat Instance
To start the newly created Tomcat JVM instance, ensure that the environment variables are set for the new instance and execute the startup script:

$ source /opt/tomcat-instance/sales.env

$ $CATALINA_HOME/bin/startup.sh

Using CATALINA_BASE:   /opt/tomcat-instance/sales.example.com

Using CATALINA_HOME:   /var/lib/apache-tomcat-6.0.18

Using CATALINA_TMPDIR: /opt/tomcat-instance/sales.example.com/temp

Using JRE_HOME:       /usr/java/jdk1.6.0_10

If everything has been configured correctly, you should now see an empty white page when opening the URL http://localhost:8080. If you get an error in the browser instead of an empty page, check the log files under $CATALINA_BASE/logs.

Note that since CATALINA_BASE has been changed for the new Tomcat instance, the logs are no longer written to /var/lib/apache-tomcat-6.0.18/logs.

Relaying HTTP Port 80 Connections to Tomcat Port 8080
By default, Tomcat listens on port 8080. To have the Tomcat server itself listen on HTTP port 80, Tomcat would have to run as root since only root can listen on ports below 1024 on Linux. But for security reasons this is not recommended.

A preferred solution to this is to relay port 80 TCP connections to port 8080 using the Netfilter package that comes with Linux.

The Netfilter package that comes already with Linux is transparent to Tomcat. The following steps show how to relay port 80 TCP connections to Tomcat’s port 8080 using the iptables command from the Netfilter package.

[root@server~]# iptables -t nat  -I PREROUTING -p tcp –dport 80 -j REDIRECT –to-ports 8080

[root@server~]# iptables -t  nat -I OUTPUT -p tcp –dport 80 -j REDIRECT –to-ports 8080

The first rule redirects incoming requests on port 80 generated from other computer nodes, and the second rule redirects incoming requests on port 80 generated from the local node where Tomcat is running.
To see the newly configured rules, run:

[root@server~]# iptables -t nat -L

Chain PREROUTING (policy ACCEPT)

target                     prot   opt    source               destination

REDIRECT           tcp     —      anywhere          anywhere            tcp dpt:www redir ports 8080

Chain POSTROUTING (policy ACCEPT)

target                     prot  opt     source               destination

Chain OUTPUT (policy ACCEPT)

target                     prot  opt    source               destination

REDIRECT           tcp     —     anywhere          anywhere            tcp dpt:www redir ports 8080

To remove the NAT rules we just created, you can run the iptables -t nat -F command which flushes and deletes the rules.

Note that this will also flush any other rules that may have been configured on your system!
To make the rules permanent for reboots, you can use the following option:

First save the newly created rules in a file:

[root@server~]# service iptables save

Note that for security reasons it’s important that firewall rules are established before the network interfaces come up.

It should be noted here that there is one Tomcat configuration parameter that you may or may not want to change, the proxyPort parameter in the server.xml file. Since Tomcat still receives requests on port 8080 as they are relayed by the Linux Netfilter system from port 80, Tomcat may display port 8080 in the URL depending on the application’s content. So if you want to change it to port 80, the proxyPort parameter would need to be added in the $CATALINA_BASE/conf/server.xml  file for port 8080:

<Connector port=”8080″ protocol=”HTTP/1.1″ proxyPort=”80″

connectionTimeout=”20000″

redirectPort=”8443″ />

After that you need to restart Tomcat to make this change effective.

Connecting to First Tomcat Instance Using Default HTTP Port
If iptables have been configured correctly, you should now be able to open the URL http://localhost and see an empty white page. If you get an error in the browser instead of an empty page, check the iptables configuration and check the log files under $CATALINA_BASE/logs.

Setting Up a Web Application for First Tomcat JVM Instance

You can setup multiple web applications for each Tomcat JVM instance. In this guide we are setting up one web application for each Tomcat JVM instance.
First make sure to switch to the tomcat user account and source in the environment variables for the remaining steps:

# su – -s /bin/sh tomcat

$ source /opt/tomcat-instance/sales.env

 Setting up Web Application Layout

In the following example I create a new directory called “sales” under $CATALINA_BASE/webapps which will become the root directory for the first web application, that is $CATALINA_BASE/webapps/sales. In Tomcat web application root directories are created under $CATALINA_BASE/webapps by default.

$ mkdir $CATALINA_BASE/webapps/sales

 Configuring Web Application

To configure Tomcat to recognize the new web application under $CATALINA_BASE/webapps/sales, the $CATALINA_BASE/conf/server.xml file needs to be edited. This is done by adding a new Context element with the path and docBase attributes. Note that Tomcat refers to webapps as “context”. So Context here represents the configuration of a web application. The path attribute is the application name used within the URL, and the docBase attribute is the absolute path name of the new web application root under $CATALINA_BASE/webapps:

<Host name=”localhost”  appBase=”webapps”

unpackWARs=”true” autoDeploy=”true”

xmlValidation=”false” xmlNamespaceAware=”false”>

<Context docBase=”sales” path=”/mysales”/>

In this example you can see that appBase already points to webapps by default, that is $CATALINA_BASE/webapps. The newly added path attribute points to the sales directory under $CATALINA_BASE/webapps which is the location for the new application. And the docBase attribute is set to mysales which stands for the application name within the URL, i.e. “http://localhost/mysales&#8221; or “http://localhost:8080/mysales&#8221;. Make sure to add this new Context element inside the Host container element for ‘localhost’ which is the default host name.

Home Page for Web Application
To have a starting page for the new web application, you can simply create and add a index.html file under the web application’s root directory $CATALINA_BASE/webapps/sales. You could also create your own JSP page here. For testing purposes here is a simple index.html example for the new application:

$ cat > $CATALINA_BASE/webapps/sales/index.html << EOF

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

http://www.w3.org/TR/html4/loose.dtd”&gt;

<HTML>

<HEAD><META http-equiv=Content-Type content=”text/html”></HEAD>

<BODY>

<H3>Apache Tomcat Sales Home Page</H3>

</BODY>

</HTML>

EOF

$

Restarting First Tomcat Instance
now check whether the new web application has been configured correctly. To do that, run the following commands to restart the new Tomcat JVM instance:

$ source /opt/tomcat-instance/sales.env

$ $CATALINA_HOME/bin/shutdown.sh

$ $CATALINA_HOME/bin/startup.sh

If everything was configured correctly, you should now see the default home page for the new web application when opening the URL http://localhost/mysales orhttp://localhost/mysales:8080.

If you get the error ‘java.net.ConnectException: Connection refused’ when you shutdown Tomcat, then Tomcat was probably not running.

Setting Up Second Tomcat JVM Instance

Login as root and execute the following steps to setup the second Tomcat JVM instance:

# mkdir -p /opt/tomcat-instance/order.example.com

# cd /opt/tomcat-instance/order.example.com

# cp -a /var/lib/apache-tomcat-6.0.18/conf .

# mkdir common logs temp server shared webapps work

# chown -R tomcat.tomcat /opt/tomcat-instance/order.example.com

# su – -s /bin/sh tomcat

$ cat > /opt/tomcat-instance/order.env << EOF

export JAVA_HOME=/usr/java/jdk1.6.0_10

export PATH=\$JAVA_HOME/bin:\$PATH

export CATALINA_HOME=/var/lib/apache-tomcat-6.0.18

export CATALINA_BASE=/opt/tomcat-instance/order.example.com

EOF

$

$ source /opt/tomcat-instance/order.env

For the second Tomcat JVM instance the default port numbers need to be changed in $CATALINA_BASE/conf/server.xml (/opt/tomcat-instance/order.example.com/conf/server.xml). In the following example I increased the port numbers by one:

<Server port=”8006″ shutdown=”SHUTDOWN”>

<Connector port=”8081″ protocol=”HTTP/1.1″

connectionTimeout=”20000″

redirectPort=”8444″ />

<Connector port=”8010″ protocol=”AJP/1.3″ redirectPort=”8444″ />

Create a new application root directory:

$ mkdir $CATALINA_BASE/webapps/order

To configure the new web application, edit $CATALINA_BASE/conf/server.xml (/opt/tomcat-instance/order.example.com/conf/server.xml) and add the following entry in blue:

<Host name=”localhost”  appBase=”webapps”

unpackWARs=”true” autoDeploy=”true”

xmlValidation=”false” xmlNamespaceAware=”false”>

<Context docBase=”order” path=”/myorder”/>

Create a new home page for the new “Order” application and include a link to the Java servlet that will be setup next:

$ cat > $CATALINA_BASE/webapps/order/index.html << EOF

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

http://www.w3.org/TR/html4/loose.dtd”&gt;

<HTML>

<HEAD><META http-equiv=Content-Type content=”text/html”></HEAD>

<BODY>

<H3>Apache Tomcat Order Home Page</H3>

<a href=”/myorder/execute”>Execute Order</a>

</BODY>

</HTML>

EOF

$

Now make sure to relogin as tomcat and start the second Tomcat JVM instance:

# su – -s /bin/sh tomcat

$ source /opt/tomcat-instance/order.env

$ $CATALINA_HOME/bin/startup.sh

After the second Tomcat JVM restarted, open the URL http://localhost:8081/myorder and you should see the “Execute Order” link. Clicking on this link should invoke the Java servlet and display “Executing Order” in your browser.

If you are presented with an empty page instead, review the above steps and make sure you didn’t miss a step. Check also the log files under $CATALINA_BASE/logs.

About Alex Hunt

Hi All I am Manish Kumar Jha aka Alex Hunt. I am currently working in VMware Software India Pvt Ltd as Operations System Engineer (vCloud Air Operations). I have around 5 Years of IT experience and have exposure on VMware vSphere, vCloud Director, RHEL and modern data center technologies like Cisco UCS and Cisco Nexus 1000v and NSX. If you find any post informational to you please press like and share it across social media and leave your comments if you want to discuss further on any post. Disclaimer: All the information on this website is published in good faith and for general information purpose only. I don’t make any warranties about the completeness, reliability and accuracy of this information. Any action you take upon the information you find on this blog is strictly at your own risk. The Views and opinions published on this blog are my own and not the opinions of my employer or any of the vendors of the product discussed.
This entry was posted in Linux/CentOS. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s