Showing posts with label Tomcat. Show all posts
Showing posts with label Tomcat. Show all posts

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
>tomcat 

Installation
# 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.

# cd $CATALINA_HOME/bin
# 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 Tomcat.sh
# Adapt the following lines to your configuration
JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk.x86_64
CATALINA_HOME=/usr/local/apache-tomcat-6.0.20
DAEMON_HOME=/usr/local/apache-tomcat-6.0.20/bin
TOMCAT_USER=tomcat
TMP_DIR=/var/tmp
CATALINA_OPTS=
CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
$DAEMON_HOME/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar 

case "$1" in
  start)
    #
    # Start Tomcat
    #
    $DAEMON_HOME/jsvc \
    -user $TOMCAT_USER \
    -home $JAVA_HOME \
    -Dcatalina.home=$CATALINA_HOME \
    -Djava.io.tmpdir=$TMP_DIR \
    -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 \

# chmod 755 Tomcat.sh
# cp Tomcat.sh /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


Integrating Tomcat into Apache over Port 80

Objective: By default, Tomcat runs over port 8080. What if your organization, such as the US Government, likes to block port 8080 on the firewall? You can integrate Tomcat into Apache and run them both on port 80. This task is performed by the mod_jk.so module. When a request for a java page comes in over port 80, Apache will use this module to automatically forward the request to Tomcat. The biggest caveat to this task is the broken functionality of the auto-configurator in Tomcat 6.0  

OS: CentOS 5.3
Apache Version: 2.2
Tomcat Version: 6.0.20 

Prerequisites: Both Apache and Tomcat have been installed and are running properly. You can access the test page for both services over 80 and 8080 respectfully.
Mod_Jk.so: This is the module that will ultimately be the connection point between Apache and Tomcat. This can be obtained from: http://tomcat.apache.org/download-connectors.cgi It is suggested to use the precompiled binary, however the source is worth looking at as it contains a sample workers.properties file which will be created later in this document.
Document Conventions:
${tomcat_home} is the root directory of tomcat. I suggest using at least one symbolic link for your configuration files; this is from experience doing things the hard way.
#ln –s /usr/local/apache-tomcat.6.0.20     tomcat
${apache_home} is the root directory of apache. On CentOS 5.3 the default directory is /etc/httpd which contains symbolic links to (logs) /var/log/httpd, (modules) /usr/lib64/httpd/modules, and (run) /var/run. If your links or directories are different, please keep this in mind as this document cannot will reference these from time to time when necessary.

Instructions:
Place your obtained mod_jk.so module and place it in the preferred httpd/modules folder.
Create a workers.properties file in {$tomcat_home}/conf
It should, at minimum contain these lines:
worker.list=ajp13
worker.ajp13.type=ajp13
worker.ajp13.host=hostname.of.webserver (alias is acceptable, fqdn is recommended)
worker.ajp13.port=8009


Edit {$tomcat_home}/conf/server.xml
Change
To hostname.of.webserver">
Below the above statement add
Change   appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
To   appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">

Create a mod_jk.conf file.
NOTE: Apache Tomcat documentation will reference a directory called {$tomcat_home}/conf/jk. This directory may not exist by default and the auto configuration file that is described does not get placed in this folder.
NOTE: There are two methods to create/modify your mod_jk.conf file – from a template or from scratch. It has been documented that the auto-configuration template option will be phased out in 7.0 and is currently pretty broken.

Method 1 – Auto-Configuration:
Make sure Tomcat is not running.
#cd {$tomcat_home}/bin/shutdown
#./shutdown.sh
Start Tomcat with the jkconf switch.
#./startup.sh –jkconf
Your newly created mod_jk.conf file will appear in {$tomcat_home}/conf/auto/
NOTE: Even after shutting down and restarting Tomcat without the jkconf switch, Tomcat may overwrite the mod_jk.conf file in the {$tomcat_home}/conf/auto/ directory. It is recommended that you move this file to {$tomcat_home}/conf/jk/
NOTE: It is possible that your mod_jk.conf file will work out of the box, however if you are not getting expected results, refer to method 2 and make any changes applicable.

Method 2 – Manual Configuration:
NOTE: The code below is based on the auto-configuration method. The comment lines are there to show what was changed to make this installation work properly. The virtualhost tags are only necessary if you have more than webhost/webserver on the same machine. If you receive this error in your logs when starting httpd, “JkWorkersFile cannot occur within section” either comment out the tags or move the JkWorkersFile line, both shown below.
Create a file called mod_jk.conf in /{$tomcat_home}/conf/jk/ with these lines:
LoadModule jk_module "/etc/httpd/modules/mod_jk.so"
JkWorkersFile /usr/local/tomcat/conf/workers.properties
JkLogLevel info
#JkOptions +ForwardDirectories
#hostname.of.webserver>
#ServerName hostname.of.webserver
JkMount /manager ajp13
JkMount /manager/* ajp13
JkMount /docs ajp13
JkMount /docs/* ajp13
JkMount /examples ajp13
JkMount /examples/* ajp13
JkMount /host-manager ajp13
JkMount /host-manager/* ajp13
#

NOTE: JkMount tells Apache which directories/files to redirect to Tomcat if those pages are requested. Add accordingly.

Editing httpd.conf
Open up your httpd.conf file (located in: /etc/httpd/conf)
Add Include {$tomcat_home}/conf/auto/mod_jk.conf
Because mod_jk.conf calls mod_jk.so make sure this line is not in effect:
LoadModule jk_module "/etc/httpd/modules/mod_jk.so"

Start/Restart Tomcat
Start/Restart Apache
If you are able to navigate to http://hostname.of.webserver/manager/html (the Tomcat manager webpage) then you have successfully integrated Tomcat into Apache.