Installing Apache Tomcat 5.5 on Linux

I’ve previously done a guide on the right way of installing Tomcat 5.0 on the Mac but things are a little different on Linux so I figured I’d do another guide. This one is a little less advanced because it only covers running Tomcat as root and not as a non-root user. Depending on how busy I am in the next while I may or may not do a follow-up article on the additional steps needed to run Tomcat as a non-root user. I have tested this procedure on RHEL ES 4 with Tomcat 5.5.17 and the Sun JDK version 1.5.0_6 but it should be the same on all Linux distros and for all 5.X Tomcat versions, the only thing that is likely to change is the location of $JAVA_HOME. Correction, the startup script included is for Redhat based distros only (RHEL, Fedora, CentOS etc).

Before We Begin

Before starting you need to make sure you have the 1.5 version of Sun’s JDK installed. If you’re on an RPM based Linux install running on x86 the simplest thing to do is to grab the RPM form java.sun.com. How ever you choose to install the Sun JVM you will need to know where it installs to for creating the $JAVA_HOME environment variable later. If you get it from the Sun RPM it will install in /usr/java/ so at the time of writing that means the most current JDK will install to /usr/java/jdk1.5.0_06/. If you are using a Debian based distro (like Ubuntu) you might find my guide on installing the Sun JDK on Debian helpful.

Step 1 – Install Tomcat Files

The first step is to get the binary distribution of Tomcat from the Apache Site (you just need the Core package). When you get this extract it and copy the entire folder it contains to /usr/local. The sequence of commands below assumes that you got the .tar.gz version of the core package for Tomcat 5.5.17. You’ll need to adjust slightly for other versions. It is also helpful to create a simlink in /usr/local called tomcat that you will always have pointing at your current tomcat install, makes upgrades easier.

[bbusschots@honeysuckle ~]$ tar -xzf apache-tomcat-5.5.17.tar.gz
[bbusschots@honeysuckle ~]$ sudo mv apache-tomcat-5.5.17 /usr/local/
[bbusschots@honeysuckle ~]$ cd /usr/local/
[bbusschots@honeysuckle local]$ sudo ln -s apache-tomcat-5.5.17/ tomcat

Note: I use sudo for commands that need to be run as root, if your system is not set up to use sudo like this then do everything as root and leave off the sudo from the commands above.

At this point we are basically done. If you want to be able to start and stop tomcat from the command line you’ll need to set up environment variables or you can skip that step and set Tomcat up as a service instead. The choice is yours. On a production environment you really should set Tomcat up as a service though.

Step 2a – Setting Environment Variables (Optional):

If you choose to go this route you will need to set the following two environment variables in your shell (because each shell is different I won’t go in to how you do that in this article):

JAVA_HOME – needs to point to your Java install. (If you used the latest Sun RPM
that will be /usr/java/jdk1.5.0_6)

CATALINA_HOME – should be set to /usr/local/tomcat

You are now ready to start Tomcat with the command /usr/local/tomcat/bin/startup.sh and stop Tomcat with the command /usr/local/tomcat/bin/shutdown.sh. Tomcat will not start automatically at boot though.

Step 2b – Setting Tomcat up as a Service (Optional):

If you want Tomcat to start automatically on boot then you need to set it up as a service. To do this you need to copy the code below and save it as a file called tomcat in the folder /etc/init.d:

# This is the init script for starting up the
# Jakarta Tomcat server
#
# chkconfig: 345 91 10
# description: Starts and stops the Tomcat daemon.
#

# Source function library.
. /etc/rc.d/init.d/functions

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

tomcat=/usr/local/tomcat
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh
export JAVA_HOME=/usr/java/jdk1.5.0_6

start(){
echo -n $"Starting Tomcat service: "
#daemon -c
$startup
RETVAL=$?
echo
}

stop(){
action $"Stopping Tomcat service: " $shutdown
RETVAL=$?
echo
}

restart(){
stop
start
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
# This doesn't work ;)
status tomcat
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac

exit 0

You may have to change the value for JAVA_HOME if your JDK is in a different location to mine. You then need to make the file executable with the command:

sudo chmod +x /etc/init.d/tomcat

And finally you need to set it to start at boot with the command:

sudo chkconfig --add tomcat

You now have a working Tomcat install that will start it self at boot time. You can also interact with it using the service command to start, stop, restart and see the status of the service at any time. E.g.

sudo service tomcat start
sudo service tomcat satus
sudo service tomcat stop