RSS RSS feed | Atom Atom feed
Popular Articles: Tom Riddle's Magical Diary | AJAX Lego Robot | AJAX CAPTCHA | SQL Multisets

Installing Tomcat with commons-daemon (jsvc)

Run Tomcat as a daemon

Most installation that I've seen of Tomcat is made with tomcat running as root. This could potentially be a disasters security hole. Most Linux systems only allow the root to listen to port 80...which is why many users of tomcat under Linux run tomcat as root. With jscv, the process will start off as root but later on will change owner to a user of your choice.

Installation

  • Create the user to run tomcat under with
    useradd tomcat
    This will create a directory under
    /home/tomcat
  • Download and install tomcat under /home/tomcat. This is how my tomcat directory looks like
    ls -l /home/tomcat drwxr-xr-x 3 tomcat tomcat 4096 Dec 13 02:51 bin drwxr-xr-x 6 tomcat tomcat 56 Sep 23 09:42 common drwxr-xr-x 3 tomcat tomcat 4096 Dec 13 05:18 conf -rw-r--r-- 1 tomcat tomcat 11357 Sep 23 09:44 LICENSE drwxr-xr-x 2 tomcat tomcat 25 Dec 13 02:51 logs -rw-r--r-- 1 tomcat tomcat 688 Sep 23 09:44 NOTICE -rw-r--r-- 1 tomcat tomcat 6403 Sep 23 09:42 RELEASE-NOTES -rw-r--r-- 1 tomcat tomcat 7006 Sep 23 09:44 RUNNING.txt drwxr-xr-x 5 tomcat tomcat 44 Sep 23 09:42 server drwxr-xr-x 4 tomcat tomcat 30 Sep 23 09:42 shared drwxr-xr-x 2 tomcat tomcat 6 Sep 23 09:42 temp drwxr-xr-x 3 tomcat tomcat 35 Dec 13 05:17 webapps drwxr-xr-x 3 tomcat tomcat 21 Dec 13 02:52 work
  • Compile the jscv code by following the instructions on http://tomcat.apache.org/tomcat-5.0-doc/setup.html
  • http://tomcat.apache.org/tomcat-6.0-doc/setup.html

Run As Service

Tomcat 5.x ships with a tomcat service file which you can use and modify. However, it's written to be used with Java 1.4. To use it with Java 1.5 you need to tweak it some more or use the following file. Please note the items in red. Tomcat user and the JDK path which you must update to fit your system. Also make sure the DAEMON_HOME executable is in the right place.
#!/bin/sh # # Startup script for Tomcat, the Apache Servlet Engine # # chkconfig: 345 80 20 # description: Tomcat is the Apache Servlet Engine # processname: tomcat # pidfile: /var/run/tomcat.pid # # Mike Millson <*******@meritonlinesystems.com> # # version 1.02 - Clear work directory on shutdown per John Turner suggestion. # version 1.01 - Cross between Red Hat Tomcat RPM and Chris Bush scripts TOMCAT_PROG=tomcat JAVA_HOME='/usr/java/jdk1.5.0_06' CATALINA_HOME='/home/tomcat/' DAEMON_HOME=$CATALINA_HOME/bin/jsvc TMP_DIR=/var/tmp CATALINA_OPTS= CLASSPATH=\ $JAVA_HOME/lib/tools.jar:\ $CATALINA_HOME/bin/commons-daemon.jar:\ $CATALINA_HOME/bin/bootstrap.jar # if TOMCAT_USER is not set, use tomcat like Apache HTTP server if [ -z "$TOMCAT_USER" ]; then TOMCAT_USER="tomcat" fi RETVAL=0 # start and stop functions start() { echo -n "Starting tomcat: " chown -R $TOMCAT_USER:$TOMCAT_USER /home/tomcat/* $DAEMON_HOME \ -user $TOMCAT_USER \ -home $JAVA_HOME \ -Dcatalina.home=$CATALINA_HOME \ -Djava.io.tmpdir=$TMP_DIR \ -Djava.awt.headless=true \ -outfile $CATALINA_HOME/logs/catalina.out \ -errfile '&1' \ $CATALINA_OPTS \ -cp $CLASSPATH \ org.apache.catalina.startup.Bootstrap # To get a verbose JVM #-verbose \ # To get a debug of jsvc. #-debug \ RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat return $RETVAL } stop() { echo -n "Stopping tomcat: " PID=`cat /var/run/jsvc.pid` kill $PID RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/tomcat /var/run/tomcat.pid } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop # Ugly hack # We should really make sure tomcat # is stopped before leaving stop sleep 5 start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 esac exit $RETVAL

Start and Stop

To start tomcat, use (on redhat)
service tomcat start
To stop
service tomcat stop
slashdot digg del.icio.us technorati [more]



Re: Installing Tomcat with commons-daemon (jsvc)

Nice post!

This issue is why I've usually run Apache httpd using AJP to link to Tomcat. Httpd runs as root and Tomcat runs as user tomcat. But historically AJP had been a pain to set up (you need to compile mod_jk).

The new httpd 2.2 has mod_ajp which should be easier. I haven't used it but it looks terrific.

Re: Installing Tomcat with commons-daemon (jsvc)

thanks, I used this very handy script...

Re: Installing Tomcat with commons-daemon (jsvc)

Hi, Thought you'd like to know that you can stop tomcat using the jsvc program. Heres a snip from my start/stop script:
    restart)
        # Takes maximum dumbass approach....
        echo "Restarting tomcat service...."
        pushd .
        cd $CATALINA_HOME
        $CATALINA_HOME/bin/jsvc -Djava.endorsed.dirs=$CATALINA_HOME/common/endorsed \
            -cp $CATALINA_HOME/bin/bootstrap.jar:$CATALINA_HOME/bin/commons-daemon.jar \
            -outfile $CATALINA_HOME/logs/catalina.out \
            -errfile $CATALINA_HOME/logs/catalina.err \
            -user tomcat -stop org.apache.catalina.startup.Bootstrap
        echo "Shutdown complete, pausing before restart...."
        sleep 2
        $CATALINA_HOME/bin/jsvc -Djava.endorsed.dirs=$CATALINA_HOME/common/endorsed \
            -cp $CATALINA_HOME/bin/bootstrap.jar:$CATALINA_HOME/bin/commons-daemon.jar \
            -outfile $CATALINA_HOME/logs/catalina.out \
            -errfile $CATALINA_HOME/logs/catalina.err \
            -user tomcat org.apache.catalina.startup.Bootstrap
        popd
        echo "Done"
note the -stop switch. hth --Robin

Re: Installing Tomcat with commons-daemon (jsvc)

Very useful script. It would be better if there was a third option 'status' which tells if the tomcat is currently running or not.

Re: Installing Tomcat with commons-daemon (jsvc)

There is a -check option for jsvc... see jsvc -help for more details. It never told me anything though :S

Re: Installing Tomcat with commons-daemon (jsvc)

use the option `-pidfile /var/run/my_tomcat_name` in the start function. Then your status just looks like this
status)
    if [[ -f $PID_FILE ]]
    then
      echo "found a pidfile so we are probably up and running."
    else
      echo "no pidfile so we are probably down."
    fi
    exit 1
    ;;

Add a comment Send a TrackBack