Originally Published: Tuesday, 2 October 2001 Author: Henry Chen
Published to: enhance_articles_sysadmin/Sysadmin Page: 3/6 - [Printable]

Serving Java from Linux

Ever want a server on your box that can serve JSP and Java Servlets but don't want to pay big money for a commercial solution? Then Linux.com has the article for you. Follow author Henry Chen into the land of the sun.

  << Page 3 of 6  >>

The Tomcat package comes with scripts that will start and stop Tomcat.  We are going to enhance this a little bit.  Create this file by vi /etc/init.d/tomcat:

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

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

prog="tomcat"
TOMCAT_USER=tomcat

start() {
    echo -n $"Starting $prog: "
    su -l $TOMCAT_USER -c '/usr/tomcat/bin/startup.sh'
    echo
}

stop() {
    echo -n "Stopping $prog: "
    su -l $TOMCAT_USER -c '/usr/tomcat/bin/shutdown.sh'
    echo
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0

This will give us control over Tomcat like other daemons on the server.  Now we have to edit the the startup and shutdown scripts which came with the Tomcat package by vi /usr/tomcat/bin/startup.sh (and shutdown.sh).  Insert these lines before BASEDIR:

TOMCAT_HOME=/usr/tomcat
JAVA_HOME=/usr/java/jdk
CLASSPATH=.:$JAVA_HOME/lib/tools.jar
export TOMCAT_HOME JAVA_HOME CLASSPATH

This will setup the proper environment variables for when we use these scripts.  Also remember to edit the first line so that it reads: #!/bin/sh  The Tomcat package ships with a space between the #! and the /bin/sh. Now, we need to do some miscellaneous things so that the scripts will work:

/usr/sbin/useradd tomcat
chmod 755 /etc/init.d/tomcat
ln -s ../init.d/tomcat /etc/rc3.d/S12tomcat
chmod 755 /usr/tomcat/bin/startup.sh
chmod 755 /usr/tomcat/bin/shutdown.sh
chmod 755 /usr/tomcat/bin/tomcat.sh

I also like to keep the logs and the working files in the /var directory:

mkdir /var/tomcat
mkdir /var/tomcat/logs
mkdir /var/tomcat/work
chown -R tomcat:tomcat /var/tomcat
ln -s /var/tomcat/logs /usr/tomcat/logs
ln -s /var/tomcat/work /usr/tomcat/work

Finally, when Tomcat runs, it automatically creates some files so we need to make sure that the tomcat username can create these files:

chown -R tomcat:tomcat /usr/tomcat/conf
chown -R tomcat:tomcat /usr/tomcat/webapps

Before you start up Tomcat, you need to install the backward compatibility RPM:

cd ~$username
rpm -ivh compat-libstdc++-6.2-2.9.0.14.i386.rpm

Now we are ready to start Tomcat.

/etc/init.d/tomcat start

You should see some messages on the console.  If something is wrong, you will see error messages.  The error messages are usually voluminous; you will not miss them. 

Another way to check to make sure Tomcat is running properly is to do a ps -ef. You will see a lot of Tomcat related processes being run by the tomcat username.

Assuming that you want to use Java to develop applications in the future, it is also more convenient to setup environment variables automatically every time you (and other users) login.  For bash, do this vi /etc/profile and add this section:

# Java/Tomcat Environment
TOMCAT_HOME=/usr/tomcat
JAVA_HOME=/usr/java/jdk
export TOMCAT_HOME JAVA_HOME
PATH="$PATH:/usr/java/jdk/bin"




  << Page 3 of 6  >>