Next Previous Contents

4. How to Setup Tomcat

This document will only discuss Tomcat 5. Old documentation for Tomcat 4 can be found here.

4.1 Getting Tomcat

Download the Tomcat binary from jakarta.apache.org/tomcat/. Place in a directory of your choice. The latest version as of this writing is 5.0.19.

4.2 Installing Tomcat

Copy the Tomcat binary (jakarta-tomcat-5.0.19.tar.gz) to the top-level directory where you want to install it. A good spot is /usr/local.
$ su -
# mv $HOME/src/jakarta-tomcat-5.0.19.tar.gz  /usr/local
# cd /usr/local
# gzip -d jakarta-tomcat-5.0.19.tar.gz
# tar -xvf jarkarta-tomcat-5.0.19.tar

4.3 Tomcat Configuration

Add the $CATALINA_HOME envirnoment variable to your PATH.
# vi /etc/profile
CATALINA_HOME=/usr/local/jakarta-tomcat-5.0.19
export CATALINA_HOME

NOTE: You may want to set up a symbolic link to /usr/local/jakarta-tomcat

4.4 Running Tomcat

Starting Tomcat

 
# cd /usr/local/jakarta-tomcat-5.0.19/bin (cd $CATALINA_HOME/bin)
# ./startup.sh

or simply

# $CATALINA_HOME/bin/startup.sh

Stoping Tomcat

# cd /usr/local/jakarta-tomcat-5.0.19/bin (cd $CATALINA_HOME/bin)
# ./shutdown.sh

or simply

# $CATALINA_HOME/bin/shutdown.sh

Testing Tomcat

Point your browser to

http://localhost:8080

and you should see the default Tomcat page. This page includes JSP and Servlet examples for you to try.

################TODO######################################

Create a New Application

The Easy Way

First we need to edit the server.xml file located at $TOMCAT_HOME/conf/server.xml. We need to add a new Context configuration. I'm going to add a Context named "sparling":

<Context path="/sparling"
         docBase="webapps/sparling"
         crossContext="false"
         reloadable="true">
</Context>
Refer to the User's Guide for an explantation of these lines.

Now we need to create the sparling directory to $TOMCAT_HOME/webapps.


# cd $CATALINA_HOME
# cd webapps
# mkdir sparling
# cd sparling
# mkdir WEB-INF
# cd WEB-INF
# mkdir classes
Now restart Tomcat. This will rebuild the tomcat-apache.conf file.

Now move your servlet class file to the $CATALINA_HOME/webapps/sparling/WEB-INF/classes directory. For example, if we add the file HelloWorld.class to this directory, then we can access the servlet via the browser at: http://localhost:8080/sparling/servlet/HelloWorld.

Additional Info

Running Tomcat as non-root user

Create tomcat user
# chown -R tomcat /usr/local/jakarta-tomcat-4.1.18
Create/edit /etc/rc.d/init.d/tomcat


#!/bin/sh
#
# Startup script for Tomcat

JAVA_HOME=/usr/java/j2sdk1.4.1_03
export JAVA_HOME
start_tomcat=/usr/local/jakarta-tomcat-4.1.18/bin/startup.sh
stop_tomcat=/usr/local/jakarta-tomcat-4.1.18/bin/shutdown.sh

start() {
        echo -n "Starting tomcat: "
        su -c ${start_tomcat} - tomcat
        echo "done."
}
stop() {
        #echo -n "Shutting down http: "
        echo -n "Shutting down tomcat: "
        ${stop_tomcat}
        echo "done."
}

# See how we were called
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 10
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
esac

exit 0

Compiling Servlets

export CLASSPATH=$CLASSPATH:$CATALINA_HOME/common/lib/servlet.jar
<29-Mar-2004>
Next Previous Contents