Wednesday, October 21, 2009

Apache Tomcat 6.0 Setup (w/daemon)

Objective: Install Tomcat and run as a daemon. As this is my second Tomcat post, I have issues with their documentation. Here's my guide to getting Tomcat working. 

OS: CentOS 5.3
Tomcat Version: 6.0.20

Document Conventions:
$CATALINA_HOME is the root directory of your Tomcat installation.  

Preparation/Prerequisites: GCC, Autoconf, Java SDK
# yum install gcc 
# yum install autoconf 
# yum install java 
# yum install java-devel 

NOTE: Creating a tomcat user is completely optional but may help with error checking and security
# groupadd tomcat
# useradd -g tomcat -s /sbin/nologin -d /home/tomcat tomcat
# passwd tomcat

# cd /usr/local/
# wget http://latest.version.of.tomcat
NOTE: Repositories may have an out of date version
# tar zxvf apache-tomcat-6.0.20.tar.gz
# chown -R tomcat.tomcat apache-tomcat-6.0.20
# rm apache-tomcat-6.0.20.tar.gz 

Daemon Configuration
NOTES FROM APACHE: Tomcat can be run as a daemon using the jsvc tool from the commons-daemon project. Source tarballs for jsvc are included with the Tomcat binaries, and need to be compiled. Building jsvc requires a C ANSI compiler (such as GCC), GNU Autoconf, and a JDK. Before running the script, the JAVA_HOME environment variable should be set to the base path of the JDK.
Alternately, *when calling the ./configure script, the path of the JDK may be specified using the --with-java parameter, such as ./configure --with-java=/.*
NOTE: With JDK 1.6 default is /usr/lib/jvm/java-1.6.0-openjdk.x86_64
     Using the following commands should result in a compiled jsvc binary, located in the $CATALINA_HOME/bin folder. This assumes that GNU TAR is used, and that CATALINA_HOME is an environment variable pointing to the base path of the Tomcat installation.
     Please note that you should use the GNU make (gmake) instead of the native BSD make on FreeBSD systems.

# tar xvfz jsvc.tar.gz
# cd jsvc-src
# autoconf
# ./configure
# make
# cp jsvc ..
# cd .. 

Starting Tomcat Daemon
NOTE: It is possible to start the daemon using this command
# ./bin/jsvc -cp ./bin/bootstrap.jar \ -outfile ./logs/catalina.out -errfile ./logs/catalina.err \ org.apache.catalina.startup.Bootstrap
However, using the script template has given much more success.
# cd $CATALINA_HOME/bin/jsvc-src/native
# vi
# Adapt the following lines to your configuration

case "$1" in
    # Start Tomcat
    $DAEMON_HOME/jsvc \
    -user $TOMCAT_USER \
    -home $JAVA_HOME \
    -Dcatalina.home=$CATALINA_HOME \$TMP_DIR \
    -outfile $CATALINA_HOME/logs/catalina.out \
    -errfile '&1' \
    -cp $CLASSPATH \
    # To get a verbose JVM
    #-verbose \
    # To get a debug of jsvc.
    #-debug \

# chmod 755
# cp /etc/rc.d/init.d/tomcat
# ./etc/rc.d/init.d/tomcat start 

To start/stop on boot/shutdown, create links in the different run level directories using this template:
# ln -s /etc/rc.d/init.d/tomcat /etc/rc.d/rc3.d/S86tomcat
# ln -s /etc/rc.d/init.d/tomcat /etc/rc.d/rc3.d/K14tomcat

No comments:

Post a Comment