<< Prev  |  TOC  |  Front Page  |  Talkback  |  FAQ  |  Next >>
...making Linux just a little more fun!
Integrating Tomcat and Apache on RedHat 9
By Mike Millson

Integrating Tomcat and Apache on RedHat 9.0

Integrating Tomcat and Apache on RedHat 9.0

Mike Millson
Web Systems Engineer
August 26, 2003
Merit Online Systems, Inc.


Java servlets are a powerful tool for building websites and web based applications. One skill that every Java web developer should have is the ability to install and configure the Tomcat servlet engine. Many thanks to the Apache Software Foundation for providing this mature, stable, open source software. It was recently voted the Best Application Server of 2003 by InfoWorld readers.

This article discusses how to integrate Tomcat with the Apache web server on RedHat 9.0. The goal is to provide a simple, stable configuration that will allow users to gain confidence using Tomcat.

Please note all commands are issued as root unless otherwise noted.

Installing Apache

I chose to install Apache using the RedHat RPM. Using the RPM instead of compiling Apache from source simplifies system administration in the following ways:

I recommend using the RedHat up2date command line utility to install RedHat RPMs. Although up2date can be used without purchasing a RedHat Network subscription, a basic subscription is a great value. It eliminates a multitude of headaches by ensuring the software you install is the correct version and you have the right dependencies installed on your system.

RedHat RPMs that must be installed:

To install these packages using up2date, make sure you are connected to the Internet, and enter the following:

up2date -i httpd
up2date -i httpd-devel

You should now be able to start/stop/restart Apache as follows:

service httpd start
service httpd stop
service httpd restart

Verify that Apache is working by starting Apache and typing http://localhost/ into your browser. You should see the default Apache install page with links to documentation.

Installing Tomcat

The only requirements to run Tomcat are that a Java Development Kit (JDK), also called a Java Software Developement Kit (SDK), be installed and the JAVA_HOME environment variable be set.

Java SDK

I chose to install Sun's Java 2 Platform, Standard Edition, which can be downloaded from http://java.sun.com/j2se/). I chose the J2SE v1.4.2 SDK Linux self-extracting binary file.

Change to the directory where you downloaded the SDK and make the self-extracting binary executable:

chmod +x j2sdk-1_4_2-linux-i586.bin

Run the self-extracting binary:


There should now be a directory called j2sdk1.4.2 in the download directory. Move the SDK directory to where you want it to be installed. I chose to install it in /usr/java. Create /usr/java if it doesn't exist. Here is the command I used from inside the download directory:

mv j2sdk1.4.2 /usr/java

Set the JAVA_HOME environment variable, by modifying /etc/profile so it includes the following:

export JAVA_HOME

There will be other environment variables being set in /etc/profile, so you will probably be adding JAVA_HOME to an existing export command. /etc/profile is run at startup and when a user logs into a system.

Tomcat Account

You will install and configure Tomcat as root; however, you should create a group and user account for Tomcat to run under as follows:

groupadd tomcat
useradd -g tomcat tomcat

This will create the /home/tomcat directory, where I will install my Tomcat applications.

Download Tomcat

Download the latest release build from http://www.apache.org/dist/jakarta/tomcat-4/binaries/. Since Tomcat runs directly on top of a standard JDK, I cannot think of any reason to building it from source.

The Tomcat binary is available in two different flavors:

  1. non-LE
  2. LE

There are a number of different download formats. I chose the LE version gnu zipped tar file (jakarta-tomcat-4.1.27-LE-jdk14.tar.gz).

Tomcat Standalone

Unzip Tomcat by issuing the following command from your download directory:

tar xvzf tomcat-4.1.27-LE-jdk14.tar.gz

This will create a directory called jakarta-tomcat-4.1.27-LE-jdk14. Move this directory to wherever you would like to install Tomcat. I chose /usr/local. Here is the command I issued from inside the download directory:

mv jakarta-tomcat-4.1.27-LE-jdk14 /usr/local/

The directory where Tomcat is installed is referred to as CATALINA_HOME in the Tomcat documentation. In this case CATALINA_HOME=/usr/local/jakarta-tomcat-4.1.27-LE-jdk14.

I recommend setting up a symbolic link to point to your current Tomcat version. This will save you from having to change your startup and shutdown scripts each time you upgrade Tomcat or set a CATALINA_HOME environment variable. It also allows you to keep several versions of Tomcat on your system and easily switch amongst them. Here is the command I issued from inside /usr/local to create a symbolic link called /usr/local/jakarta-tomcat that points to /usr/local/jakarta-tomcat-4.1.27-LE-jdk14:

ln -s jakarta-tomcat-4.1.27-LE-jdk14 jakarta-tomcat

Change the group and owner of the /usr/local/jakarta-tomcat and /usr/local/jakarta-tomcat-4.1.27-LE-jdk14 directories to tomcat:

chown tomcat.tomcat /usr/local/jakarta-tomcat
chown -R tomcat.tomcat /usr/local/jakarta-tomcat-4.1.27-LE-jdk14

It is not necessary to set the CATALINA_HOME environment variable. Tomcat is smart enough to figure out CATALINA_HOME on its own.

You should now be able to start and stop Tomcat from the CATALINA_HOME/bin directory by typing ./startup.sh and ./shutdown.sh respectively. Test that Tomcat is working by starting it and typing http://localhost:8080 into your browser. You should see the Tomcat welcome page with links to documentation and sample code. Verify Tomcat is working by clicking on some of the examples links.

Selecting A Connector

At this point, Apache and Tomcat should be working separately in standalone mode. You can run Tomcat in standalone mode as an alternative to Apache. In fact, in some cases, it is said that Tomcat standalone is faster than serving static content from Apache and dynamic content from Tomcat. However, there are compelling reasons to use Apache as the front end. If you run Tomcat standalone:

  1. You will have to run Tomcat as root on port 80. This is a security concern.
  2. You will not be able to use a connector such as mod_jk to load balance amongst several Tomcat instances.
  3. You will not be able to take advantage of Apache features such as cgi and PHP.
  4. You will not be able to take advantage of Apache modules such as mod_rewrite.
  5. You will not be able to isolate virtual hosts in their own Tomcat instances.

I think the increased functionality obtained by using Apache on the front end far outweighs the effort required to install and configure a connector. With that said, I selected the tried and true mod_jk connector. It has been around a long while and is very stable. mod_jk2 is the wave of the future, but I'm holding off on that for now. In early 2002 I invested a considerable amount of time on the "wave of the future" connector at that time, mod_webapp, which is now no longer being developed. For that reason, I am being cautious about migrating to mod_jk2.

Building the mod_jk Connector

The mod_jk connector is the communication link between Apache and Tomcat. It listens on port 8009 for requests from Apache.

In my experience, it's safest to think of connectors as being version dependent. If you upgrade Tomcat and you have a connector issue, try compiling the connector using the version-specific connector source.

Download the connector source for your version of Tomcat from http://www.apache.org/dist/jakarta/tomcat-4/source/. I used jakarta-tomcat-connectors-4.1.27-src.tar.gz. The source for all the different connectors (mod_jk, mod_jk2, coyote, etc.) is distributed in this one file.

Unzip the contents of the file into your download directory as follows:

tar xvzf jakarta-tomcat-connectors-4.1.27-src.tar.gz

This will create a folder called jakarta-tomcat-connectors-4.1.27-src. Move this folder to wherever you store source files on your system. I chose /usr/src. Here is the command I issued from inside the download directory:

mv jakarta-tomcat-connectors-4.1.27-src /usr/src/

I refer to the folder where the connector source is installed as CONN_SRC_HOME. In my case CONN_SRC_HOME = /usr/src/jakarta-tomcat-connectors-4.1.27-src.

Run the buildconf script to to create the CONN_SRC_HOME/jk/native/configure file.


Run the configure script with the path to the apxs file on your system and the options below:

./configure --with-apxs=/usr/sbin/apxs

Build mod_jk with the following command:


If all went well, the mod_jk.so file was successfully created. Manually copy it to Apache's shared object files directory:

cp CONN_SRC_HOME/jk/native/apache-2.0/mod_jk.so /etc/httpd/modules

Configuring Tomcat


The workers.properties file contains information so mod_jk can connect to the Tomcat worker processes.

Create a directory called CATALINA_HOME/conf/jk and place the workers.properties file found in the Appendix in this directory.


The server.xml file contains Tomcat server configuration information. The default CATALINA_HOME/conf/server.xml file that comes with Tomcat contains so much information that I recommend saving it for future reference (e.g. server.xml.bak) and starting from scratch. The default server.xml is great for verifying that Tomcat works in standalone mode and for viewing the examples that come with the application, but I have found it is not the best starting point when you want to integrate Apache with Tomcat. Instead, create a bare bones server.xml file as follows:

<Server port="8005" shutdown="SHUTDOWN" debug="0">

	<Service name="Tomcat-Apache">

		<Connector className="org.apache.ajp.tomcat.Ajp13Connector"
			port="8009" minProcessors="5" maxProcessors="75" 
			acceptCount="10" debug="0"/>   

		<Engine name="your_engine" debug="0" defaultHost="your_domain.com">
			<Logger className="org.apache.catalina.logger.FileLogger"
				prefix="apache_log." suffix=".txt" 
			<Host name="your_domain" debug="0" appBase="webapps" 
				<Context path="" docBase="/home/tomcat/your_application" 
				debug="0" reloadable="true" />


  1. The setup assumes you will put your Tomcat applications in /home/tomcat, not CATALINA_HOME/webapps. This will allow you to easily upgrade Tomcat and back up your Tomcat applications.
  2. If you do keep the default server.xml, make sure you comment out any other connectors besides mod_jk that are listening on port 8009. The default file comes with the Coyote/JK2 connector enabled for the Tomcat-Standalone service. This will conflict with the mod_jk connector in your Tomcat-Apache service. You should comment this connector out. It isn't needed when you connect directly to Tomcat in standalone mode (port 8080), so I'm not sure why this connector is enabled by default.

Configuring Apache


Apache is configured with directives placed in the Apache configuration file, /etc/httpd/conf/httpd.conf. You will notice that there are three sections labeled in the httpd.conf file supplied by RedHat: (1) Global Environment, (2) Main Server Configuration, and (3) Virtual Hosts.

Add the following to the bottom of the existing LoadModule directives in the Global Environment section:

LoadModule jk_module modules/mod_jk.so

Add the following to the bottom of the existing AddModule directives in the Global Environment section:

AddModule mod_jk.c

Add the following to the bottom of the Main Server Configuration section:

JkWorkersFile "/usr/local/jakarta-tomcat/conf/jk/workers.properties"
JkLogFile "/usr/local/jakarta-tomcat/logs/mod_jk.log"
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

The configuration above assumes you created a symbolic link /usr/jakarta-tomcat that points to the directory where your version of Tomcat is installed.

Set up a Virtual Host directive in the Virtual Hosts section of httpd.conf. Below is an example of how to set up the your_domain website to forward all URLs with "servlet" in the path to Tomcat:

NameVirtualHost *:80

	ServerAdmin webmaster@your_domain
	ServerName your_domain
	DocumentRoot /home/www/your_domain/html
	ErrorLog /home/www/your_domain/logs/error_log
	CustomLog /home/www/your_domain/logs/access_log common
	JkMount /servlet/* ajp13

The configuration above assumes that your application's static html files will be served from the /home/www/your_domain/html directory.

You can test your Apache configuration by typing the following:

apachectl configtest

You will receive the response "Syntax OK" if there are no errors in httpd.conf.

Setting Up your_domain

your_domain.com does not need to be a domain name with a DNS entry. For testing purposes, you can set up any domain you want in the /etc/hosts file of the machine that you will be using to access your_application.

The example below shows the entry for your_domain when running Apache and Tomcat on a single machine, typical for a development computer.	your_domain


We will now create and install a simple Hello World servlet so we can test our setup.

Hello World Servlet

Copy the following into a file called HelloWorld.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld
    extends HttpServlet {
    public void doGet(HttpServletRequest request, 
                       HttpServletResponse response)
                throws IOException, ServletException {
		PrintWriter out = response.getWriter();
		out.println("Hello World");



Compile the source into a class file as follows:

javac -classpath /usr/java/jakarta-tomcat/common/lib/servlet.jar HelloWorld.java

This will create a file called HelloWorld.class.

Tomcat Application

Create the following directories and files in /home/tomcat/your_application:


The web.xml file is where you map the name of your servlet to a URL pattern so Tomcat can run your servlet when requested. Below is the web.xml file that runs the HelloWorld servlet whenever the URL http://your_domain/servlet/HelloWorld is entered in the browser:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"



Copy the HelloWorld.class file to the /tomcat/home/your_application/WEB-INF/classes directory.

Restart Tomcat as follows:


Restart Apache as follows:

service httpd restart

You should now be able to type http://your_domain/servlet/HelloWorld into your browser and see the always-exciting "Hello World" message.

Advanced Configuration

The following steps are not mandatory, but are suggested for a better, tighter Tomcat installation.

Tomcat Startup Script

If you want to automatically start Tomcat when your system boots and manage it using the service command as we do Apache, you must create an initialization script.

Create the following Tomcat initialization script as /etc/rc.d/init.d/tomcat

# 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 <mmillson@meritonlinesystems.com>
# version 1.02 - Clear work directory on shutdown per John Turner suggestion.
# version 1.01 - Cross between RedHat Tomcat RPM and Chris Bush scripts

# Tomcat name :)
# if TOMCAT_USER is not set, use tomcat like Apache HTTP server
if [ -z "$TOMCAT_USER" ]; then


# start and stop functions
start() {
    echo -n "Starting tomcat: "

    chown -R $TOMCAT_USER:$TOMCAT_USER /usr/local/jakarta-tomcat/*    
    chown -R $TOMCAT_USER:$TOMCAT_USER /home/tomcat/*
    su -l $TOMCAT_USER -c '/usr/local/jakarta-tomcat/bin/startup.sh'
    [ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat
    return $RETVAL

stop() {
    echo -n "Stopping tomcat: "
    su -l $TOMCAT_USER -c '/usr/local/jakarta-tomcat/bin/shutdown.sh'
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/tomcat /var/run/tomcat.pid    
    rm -rf /usr/local/jakarta-tomcat/work/*

# See how we were called.
case "$1" in
	# Ugly hack
	# We should really make sure tomcat
	# is stopped before leaving stop
        sleep 2	
	echo "Usage: $0 {start|stop|restart}"
	exit 1

exit $RETVAL

Add the startup script to your system as follows:

chkconfig --add tomcat

You will be able to start/stop/restart it using the following commands:

service tomcat start
service tomcat stop
service tomcat restart

If you want Tomcat to start automatically when your system boots, you need to add tomcat to your runlevel as follows:

chkconfig --level 5 tomcat on

Runlevel 5 is the X Window System, typical for a development computer. Runlevel 3 is typical for a dedicated web server.

The start order of Apache and Tomcat is very important. Tomcat must be started before you start Apache so Apache can attach itself to the Tomcat processes.

Development Setup

During development, you will need access to your tomcat application directory. Add the user account under which you will be doing development to the tomcat group in /etc/group. For example, this is what the tomcat entry might look like in /etc/group if you do development under the



Make sure the tomcat group has write permission to /home/tomcat so you can publish files (e.g. using ant) to your Tomcat application in /home/tomcat/your_application. Issue the following command as root:

chmod g+w /home/tomcat



# workers.properties
# This file provides jk derived plugins with the needed information to
# connect to the different tomcat workers.  Note that the distributed
# version of this file requires modification before it is usable by a
# plugin.
# As a general note, the characters $( and ) are used internally to define
# macros. Do not use them in your own configuration!!!
# Whenever you see a set of lines such as:
# x=value
# y=$(x)\something
# the final value for y will be value\something
# Normaly all you will need to do is un-comment and modify the first three
# properties, i.e. workers.tomcat_home, workers.java_home and ps.
# Most of the configuration is derived from these.
# When you are done updating workers.tomcat_home, workers.java_home and ps
# you should have 3 workers configured:
# - An ajp12 worker that connects to localhost:8007
# - An ajp13 worker that connects to localhost:8009
# - A jni inprocess worker.
# - A load balancer worker
# However by default the plugins will only use the ajp12 worker. To have
# the plugins use other workers you should modify the worker.list property.
# OPTIONS ( very important for jni mode )
# workers.tomcat_home should point to the location where you
# installed tomcat. This is where you have your conf, webapps and lib
# directories.
# workers.java_home should point to your Java installation. Normally
# you should have a bin and lib directories beneath it.
# You should configure your environment slash... ps=\ on NT and / on UNIX
# and maybe something different elsewhere.
#------ ADVANCED MODE ------------------------------------------------
#------ DEFAULT worket list ------------------------------------------
# The workers that your plugins should create and work with
worker.list=ajp12, ajp13
#------ DEFAULT ajp12 WORKER DEFINITION ------------------------------
# Defining a worker named ajp12 and of type ajp12
# Note that the name and the type do not have to match.
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
#  ----> lbfactor must be > 0
#  ----> Low lbfactor means less work done by the worker.
#------ DEFAULT ajp13 WORKER DEFINITION ------------------------------
# Defining a worker named ajp13 and of type ajp13
# Note that the name and the type do not have to match.
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
#  ----> lbfactor must be > 0
#  ----> Low lbfactor means less work done by the worker.
# Specify the size of the open connection cache.
#------ DEFAULT LOAD BALANCER WORKER DEFINITION ----------------------
# The loadbalancer (type lb) workers perform wighted round-robin
# load balancing with sticky sessions.
# Note:
#  ----> If a worker dies, the load balancer will check its state
#        once in a while. Until then all work is redirected to peer
#        workers.
worker.loadbalancer.balanced_workers=ajp12, ajp13
#------ DEFAULT JNI WORKER DEFINITION---------------------------------
# Defining a worker named inprocess and of type jni
# Note that the name and the type do not have to match.
#------ CLASSPATH DEFINITION -----------------------------------------
# Additional class path components.
# Setting the command line for tomcat.
# Note: The cmd_line string may not contain spaces.
# Not needed, but can be customized.
# worker.inprocess.cmd_line=-config
# worker.inprocess.cmd_line=$(workers.tomcat_home)$(ps)conf$(ps)server.xml
# worker.inprocess.cmd_line=-home
# worker.inprocess.cmd_line=$(workers.tomcat_home)
# The JVM that we are about to use
# This is for Java2
# Windows
# #worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)bin$(ps)classic$(ps)jvm.dll
# IBM JDK1.3
# worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)bin$(ps)classic$(ps)libjvm.so
# Unix - Sun VM or blackdown
# RH + JDK1.4
# And this is for jdk1.1.X
# worker.inprocess.jvm_lib=$(workers.java_home)$(ps)bin$(ps)javai.dll
# Setting the place for the stdout and stderr of tomcat
# Setting the tomcat.home Java property
# worker.inprocess.sysprops=tomcat.home=$(workers.tomcat_home)
# Java system properties
# worker.inprocess.sysprops=java.compiler=NONE
# worker.inprocess.sysprops=myprop=mypropvalue
# Additional path components.
# worker.inprocess.ld_path=d:$(ps)SQLLIB$(ps)bin

Related Linux Gazette Articles

Installing Tomcat on Linux by Allan Peda, August 2001


Mike is a Web Systems Engineer with Merit Online Systems in Atlanta, GA. His first computer experience came programming BASIC on an IBM PC in 1981. When he isn't wearing his propeller cap, he enjoys spending time with his wife, Debora, and spoiling his Golden Retriever, Belle.

© 2003 Merit Online Systems, Inc.


[BIO] Mike is a Web Systems Engineer with Merit Online Systems in Atlanta, GA. His first computer experience came programming BASIC on an IBM PC in 1981. When he isn't wearing his propeller cap, he enjoys spending time with his wife, Debora, and spoiling his Golden Retriever, Belle.

Copyright © 2003, Mike Millson. Copying license http://www.linuxgazette.net/copying.html
Published in Issue 95 of Linux Gazette, October 2003

<< Prev  |  TOC  |  Front Page  |  Talkback  |  FAQ  |  Next >>