Next Previous Contents

4. How to Setup Tomcat

4.1 Tomcat 5

Tomcat 5

4.2 Tomcat 4

Get the Tomcat binary from http://jakarta.apache.org/downloads/binindex.html. For this install, I used jakarta-tomcat-4.1.31.tar.gz.

Copy file to /usr/local.


# cd /usr/local/
# gzip -d jakarta-tomcat-4.1.31.tar.gz
# tar xvf jakarta-tomcat-4.1.31.tar.gz
Set the CATALINA_HOME environment variable. I set this in /etc/profile:
CATALINA_HOME="/usr/local/jakarta-tomcat-4.1.31"
export CATALINA_HOME
Set the JAVA_HOME environment variable. I set this in /etc/profile:

JAVA_HOME="/usr/java/j2sdk1.4.1_03"
export JAVA_HOME

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

ln -s jakarta-tomcat-4.1.31/ jakarta-tomcat
If you do, then you can refer to /usr/local/jakarta-tomcat instead. You can then update your /etc/profile CATALINA_HOME variable to use the symlink instead. This will also make it easier to deal with Tomcat upgrades.

Starting Tomcat

 
# cd /usr/local/jakarta-tomcat-4.1.31/bin
# ./startup.sh

Stoping Tomcat

# cd /usr/local/jakarta-tomcat-4.1.31/bin
# ./shutdown.sh

Testing Tomcat

Point your browser to

localhost:8080

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

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

# groupadd tomcat
# useradd -g tomcat tomcat

# chown -R tomcat:tomcat /usr/local/jakarta-tomcat-4.1.31
# chown tomcat:tomcat /usr/local/jakarta-tomcat

Create /etc/rc.d/init.d/tomcat

# vi /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.31/bin/startup.sh
stop_tomcat=/usr/local/jakarta-tomcat-4.1.31/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

Next Previous Contents