Tuesday, February 8, 2011

Installing Tomcat on Linux

Introduction

This article discusses how to install Apache Tomcat 6.x (6.0.29) on Linux CentOS 5.5. Additionally it shows how to setup multiple Tomcat JVM instances on a single Linux server. For each Tomcat JVM instance a web application and Java servlet example is configured. The Tomcat installation steps outlined in this article are also applicable to most other Linux distributions.

Note that this document comes without warranty of any kind. But every effort has been made to provide the information as accurate as possible. I welcome emails from any readers with comments, suggestions, and corrections at ltphong112_at_gmail.com.

Installing Java Runtime Environment

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

For the Tomcat installation I used SUN's latest Java SE JDK that was available at the time of this writing: Java SE Development Kit (JDK) 6 Update 23 (6u23). Regarding Java SE 6, Platform Name and Version Numbers, see http://java.sun.com/javase/6/webnotes/version-6.html. And for the whole Java version history I recommend the Wiki article http://en.wikipedia.org/wiki/Java_version_history.

You can download Sun's latest Java JDKs at: http://java.sun.com/javase/downloads/index.jsp.
Choose your Linux type.
After that, make sure you choose download the “jdk-6u23-linux-i586-rpm.bin” one (as of this writing the update 23 was available).

To install JDK on CentOS 5.5, first you need to install "jpackage-utils". Run the following command with root:

#yum install jpackage-utils

After downloading JDK, make sure your "jdk-6u23-linux-i586-rpm.bin" file executable or you 'chmod +x" this file and then execute it:

#chmod +x jdk-6u23-linux-i586-rpm.bin
#./jdk-6u23-linux-i586-rpm.bin

It will create about 6 rpm files in the current directory and install it automatically. You should check if it's installed with:

#java -version

You should get:

java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) 32-Bit Server VM (build 19.0-b09, mixed mode)

If this fails, you can also try the alternative method to install Java. The CentOS Wiki at http://wiki.centos.org/HowTos/JavaOnCentOS is a great resource for that.

Installing Tomcat

Download the latest Tomcat 6.x version from http://tomcat.apache.org/download-60.cgi. For CentOS, I downloaded the Binary Core Distribution file apache-tomcat-6.0.29.tar.gz which was the latest version at the time of this writing.

Once you downloaded the tar file make sure the MD5 checksum matches the value posted on Tomcat's web site.
#md5sum /tmp/apache-tomcat-6.0.29.tar.gz
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.

For security reasons I created a user account with no login shell for running the Tomcat server:

#groupadd tomcat
#useradd -g tomcat -s /usr/sbin/nologin -m -d /home/tomcat tomcat

(It should be noted that other Linux systems have nologin under /sbin not /usr/sbin)

Next I extracted the tar file to /opt and changed the ownership of all files and directories to tomcat:
#cd /opt#tar zxvf /tmp/apache-tomcat-6.0.29.tar.gz #chown -R tomcat:tomcat /opt/apache-tomcat-6.0.29
The get the Tomcat version of the newly installed Tomcat, run:

#/opt/apache-tomcat-6.0.23/bin/version.sh
Using CATALINA_BASE:   /opt/apache-tomcat-6.0.23
Using CATALINA_HOME:   /opt/apache-tomcat-6.0.23
Using CATALINA_TMPDIR: /opt/apache-tomcat-6.0.23/temp
Using JRE_HOME:        /usr
Server version: Apache Tomcat/6.0.23
Server built:   Jul 22 2010 02:00:36
Server number:  6.0.23.0
OS Name:        Linux
OS Version:     2.6.18-6-i386

Architecture:   x86
JVM Version:    1.4.2 JVM Vendor: Free Software Foundation, Inc.

For better handling I always create a symbolic link for “tomcat” with:

#ln -s /opt/apache-tomcat-6.x.x tomcat

Starting/Stopping Tomcat

To start Tomcat, use this command

#/opt/tomcat/bin/startup.sh

To stop Tomcat, use this command

#/opt/tomcat/bin/shutdown.sh

For security reasons I 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 (more on the Tomcat environment variables later). And since the tomcat account has no login shell, it needs to be specified with the -s option. (You may want to use this su command if you plan on writing and implementing a system startup and shutdown script for system reboots.)

#export JAVA_HOME=/usr/java/jdk1.6.0_23
#export PATH=$JAVA_HOME/bin:$PATH
#export CATALINA_HOME=/opt/tomcat
#export CATALINA_BASE=/opt/tomcat

#su - tomcat -c $CATALINA_HOME/bin/startup.sh
Using CATALINA_BASE: /opt/apache-tomcat-6.0.29
Using CATALINA_HOME: /opt/apache-tomcat-6.0.29
Using CATALINA_TMPDIR: /opt/apache-tomcat-6.0.29/temp
Using JRE_HOME: /usr/java/jdk1.6.0_23

Now verify that Tomcat was started successfully by opening the URL http://localhost:8080 (Port number 8080 is the default port used by Tomcat). 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 (/opt/apache-tomcat-6.0.29/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 which is covered in the next chapter.

#su -tomcat -c $CATALINA_HOME/bin/shutdown.sh
Using CATALINA_BASE:   /var/lib/apache-tomcat-6.0.29
Using CATALINA_HOME:   /var/lib/apache-tomcat-6.0.29
Using CATALINA_TMPDIR: /var/lib/apache-tomcat-6.0.29/temp
Using JRE_HOME:        /usr/java/jdk1.6.0_23

Switching to Tomcat User Account

Most of the next steps in this article assume that you switched to the tomcat user account. If you see a '$' prompt, then the steps in this article are executed as the tomcat user. If you see a '#' prompt, then the steps are executed as root.

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:

#su - -s /bin/sh tomcat
$id
uid=1001(tomcat) gid=1001(tomcat) groups=1001(tomcat)

Note that non-root users cannot switch to the tomcat account.

Configure Tomcat to run on startup

You might want to have Tomcat automatically started when you reboot your system. For that we can use the following script and “chkconfig” command line.
Create a “tomcat” file in “/etc/init.d”

#vi /etc/init.d/tomcat

Copy the below startup script:
#!/bin/bash
#
# Init file for SixSigns Tomcat server
#
# chkconfig: 2345 55 25
# description: SixSigns Tomcat server
#
# Source function library.
. /etc/init.d/functions
RUN_AS_USER=tomcat # Adjust run user here
CATALINA_HOME=/opt/tomcat
start() {
echo "Starting Razuna Tomcat: "
if [ "x$USER" != "x$RUN_AS_USER" ]; then
su - $RUN_AS_USER -c "$CATALINA_HOME/bin/startup.sh"
else
$CATALINA_HOME/bin/startup.sh
fi
echo "done."
}
stop() {
echo "Shutting down Razuna Tomcat: "
if [ "x$USER" != "x$RUN_AS_USER" ]; then
su - $RUN_AS_USER -c "$CATALINA_HOME/bin/shutdown.sh"
else
$CATALINA_HOME/bin/shutdown.sh
fi
echo "done."
}
case "$1" in
start)
start
 ;;
stop)
stop
 ;;
restart)
stop
sleep 10
#echo "Hard killing any remaining threads.."
#kill -9 `cat $CATALINA_HOME/work/catalina.pid`
start
 ;;
*)
echo "Usage: $0 {start|stop|restart}"
esac
exit 0

Save the file (case you forgot “esc + x")

Add the tomcat startup script to the chkconfig with:

#chkconfig --add tomcat

Then activate it for your run level. Since I am not running any X Server on my server I only want to run it on level 345. Do it with:

#chkconfig --level 345 tomcat on

You can check if Tomcat will startup on reboot with listing the startup scripts with:

#chkconfig --list

If you see:

tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Then your script will be able to start at startup.

Note: In order for Tomcat to startup and shutdown, you will also need to add an environment variable pointing to the Tomcat directory (CATALINA_HOME), and one variable pointing to your Java SDK directory (JAVA_HOME). I will make the following assumptions:

JAVA_HOME
    /usr/java/jdk1.6.0_23/

CATALINA_HOME
    Tomcat is installed into /opt/apache-tomcat-6.0.29 and symlinked to /opt/tomcat.

Insert the following lines inside /etc/profile or /root/.bashrc.

export JAVA_HOME=/usr/java/jdk1.6.0_23
export CATALINA_HOME=/opt/tomcat

Notice that we specified the home directory of Tomcat to be /opt/tomcat. Some people believe that this is good practice because it eliminates an additional home directory that needs to be administered.

Now, we will put everything in /opt/tomcat under tomcat user and group. As root,

#chown -R tomcat:tomcat /opt/tomcat

If /opt/tomcat is a symlink to your Tomcat install directory, you'll need to do this:

#chown -R tomcat:tomcat /opt/apache-tomcat-6.0.29

Verify that JAVA_HOME and CATALINA_HOME environment variables are setup for tomcat user, and you should be good to go. Once the Tomcat binaries are under Tomcat user, the way you invoke it will be different.

Also, be aware that your web applications will need to be deployed (i.e. copied to the web application directories) as user tomcat, instead of root. A little more hassle, but possibly a little safer too.

No comments:

Post a Comment