Integrating Tomcat and Apache on Red Hat Linux 9 or Red Hat Enterprise Linux 3

原创 2004年08月13日 09:56:00

Integrating Tomcat and Apache on Red Hat Linux 9 or Red Hat Enterprise Linux 3

Mike Millson
Web Systems Engineer
February 5, 2004
Merit Online Systems, Inc.

1.0 Introduction

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 Red Hat Linux 9 or Red Hat Enterprise Linux 3. The goal is to provide a simple, stable configuration that will allow users to gain confidence using Tomcat.

Please note the following code conventions:
  • All commands are issued as root unless otherwise noted.
  • Highlighted values should be customized to your setup.

2.0 Installing Apache

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

  • Updates and bug fixes can be installed automatically from the Red Hat Network.
  • Startup and shutdown scripts are already configured and available.

I recommend using the Red Hat up2date command line utility to install Red Hat RPMs. 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.

Red Hat RPMs that must be installed:

  • httpd: the Apache web server
  • httpd-devel: development tools that will be needed to create the mod_jk connector

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.

3.0 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.

3.1 Java SDK

I chose to install Sun's Java 2 Platform, Standard Edition, which can be downloaded from 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

/etc/profile is run at startup and when a user logs into the system, so you will need to log out and log back in for JAVA_HOME to be defined.

su -

Check to make sure JAVA_HOME is defined correctly using the command below. You should see the path to your Java SDK.


3.2 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.

3.3 Download Tomcat

Download the latest release binary build from 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
    • Full binary distribution
    • Includes all optional libraries and an XML parser (Xerces)
    • Can be run on JDK 1.2+
  2. LE
    • Lightweight binary distribution
    • Designed to be run on JDK 1.4
    • Does not include an XML parser because one is included in JDK 1.4
    • Can be run on JDK 1.2 by adding an XML parser
    • All the components of this distribution are open source software
    • Does not include any of the following optional binaries: JavaMail, Java Activation Framework, Xerces, JNDI, or the JDBC Standard Extension

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

3.4 Tomcat Standalone

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

tar xvzf jakarta-tomcat-4.1.29-LE-jdk14.tar.gz

This will create a directory called jakarta-tomcat-4.1.29-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.29-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.29-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.29-LE-jdk14:

ln -s jakarta-tomcat-4.1.29-LE-jdk14 jakarta-tomcat

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

chown tomcat.tomcat /usr/local/jakarta-tomcat
chown -R tomcat.tomcat /usr/local/jakarta-tomcat-4.1.29-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 ./ and ./ 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.

3.5 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.

3.6 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 jk connector source from I used jakarta-tomcat-connectors-jk-1.2-src-current.tar.gz.

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

tar xvzf jakarta-tomcat-connectors-jk-1.2-src-current.tar.gz

This will create a folder called jakarta-tomcat-connectors-jk-1.2.5-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-jk-1.2.5-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-jk-1.2.5-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 file was successfully created. Manually copy it to Apache's shared object files directory:

cp CONN_SRC_HOME/jk/native/apache-2.0/ /etc/httpd/modules

4.0 Configuring Tomcat


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

Create a directory called CATALINA_HOME/conf/jk and place the file below in this directory.

# - ajp13
# List workers
# Define wrkr


  1. There is an example file located in the CONN_SRC_HOME/jk/conf directory. The example file provides a lot of useful information and insight into the file, but it contains so much information that it can be confusing. I recommend using it as a learning tool but creating your own file from scratch.
  2. The configuration above assumes Apache and Tomcat are located on the same box and requests are forwarded to Tomcat using type ajp13 workers. Type ajp13 workers forward requests to out-of-process Tomcat workers using the ajpv13 protocol over TCP/IP sockets.
  3. The name of the worker in the JkMount directive in httpd.conf must match the name of the worker in worker.list ("wrkr" in the configuration above).

4.2 server.xml

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="0fbb9aebcbfbef203eca71b6be367859" debug="0">

	<Service name="Tomcat-Apache">
		<Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
			address="" port="8009" minProcessors="5" maxProcessors="75"
			enableLookups="false" acceptCount="10" debug="0"/>
		<Engine name="" debug="0" defaultHost="">
			<Logger className="org.apache.catalina.logger.FileLogger"
				prefix="catalina_log." suffix=".txt" 
			<Host name="" debug="0" unpackWARs="true">
				<Context path="" docBase="/home/tomcat/" 
				debug="0" reloadable="true" />



This 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.

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.

The Server address defines the interface that Tomcat will listen on for mod_jk requests from Apache. In my configuration, Apache and Tomcat reside on the same box, so I have set the address to the loopback address. The default is for Tomcat to listen on all interfaces, so restricting it to the loopback interface improves security.

The Server shutdown property is the text string that is sent over a socket connection to stop Tomcat. The default value is "SHUTDOWN". The shutdown port is always on the loopback interface, which provides host-level protection. However, there is the possibility that the host could be compromised and someone could send the command SHUTDOWN to all ports and knock Tomcat offline. To prevent this, replace the default value with one that is difficult to guess. Do not use the example string above. Create your own by feeding random bytes into md5sum as follows:

head -1024c /dev/random | md5sum

Change the permissions on server.xml so no one can read the shutdown string:

chmod 600 $CATALINA_HOME/conf/server.xml

5.0 Configuring Apache

Apache is configured with directives placed in the main Apache configuration file, /etc/httpd/conf/httpd.conf. In addition, Apache 2 has configuration files for perl, php, and ssl located in /etc/httpd/conf.d/.>

Rename the /etc/httpd/conf.d/ssl.conf file to ssl.conf.bak. The default Red Hat Apache 2 installation comes with ssl support enabled. If ssl is needed, you can re-enable it after you have sucessfully integrated Apache and Tomcat.

5.1 httpd.conf

You will notice that there are three sections labeled in the httpd.conf file supplied by Red Hat: (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/

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

JkWorkersFile "/usr/local/jakarta-tomcat/conf/jk/"
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 so Tomcat handles all jsp pages and requests with "servlet" in the path:


	ServerAdmin webmaster@
	DocumentRoot /usr/www//html
	ErrorLog /usr/www//logs/error_log
	CustomLog /usr/www//logs/access_log common
	JkMount /*.jsp wrkr
	JkMount /servlet/* wrkr

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

The argument for the NameVirtualHost directive must match exactly the argument for the VirtualHost directive (

You can test your Apache configuration by typing the following:

httpd -t -D DUMP_VHOSTS

You should get something like the following response:           is a NameVirtualHost
         default server your_domain (/etc/httpd/conf/httpd.conf:1056)
         port 80 namevhost your_domain (/etc/httpd/conf/httpd.conf:1056)
Syntax OK

6.0 Setting Up your_domain

your_domain 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.	

7.0 Testing Apache

We will create and install a simple Hello World html page so we can test to make sure Apache handles requests for static html pages.

7.1 Hello World HTML

Copy the following text into a file called HelloWorld.html and install the file in the /usr/www/your_domain/html directory.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World HTML!</title>
<h1>Hello World HTML!</h1>

If Apache has not been restarted since you added your virtual host, do so as follows:

service httpd restart

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

8.0 Testing Tomcat

We will create and install a simple Hello World servlet so we can test to make sure Apache forwards servlet requests to Tomcat for handling.

8.1 Hello World JSP

Copy the following into a file called HelloWorld.jsp:

<%@ page contentType="text/html;charset=WINDOWS-1252"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Hello World JSP</title>
<h1><% out.println(" Hello World JSP!"); %></h1>

Copy the HelloWorld.jsp file to the /home/tomcat/your_application/ directory.

8.2 Hello World Servlet

Copy the following into a file called

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 Servlet!");



Compile the source into a class file as follows:

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

This will create a file called HelloWorld.class. Copy the HelloWorld.class file to the /home/tomcat/your_application/WEB-INF/classes directory.

8.3 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"



Restart Tomcat as follows:


Restart Apache as follows:

service httpd restart

You should now be able to type the following into your browser and see the always-exciting "Hello World" message:

9.0 Advanced Configuration

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

9.1 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/
# Mike Millson <>
# 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 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/'
    [ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat
    return $RETVAL

stop() {
    echo -n "Stopping tomcat: "
    su -l $TOMCAT_USER -c '/usr/local/jakarta-tomcat/bin/'
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/tomcat /var/run/    
    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  tomcat on

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

Apache and Tomcat can be started in any order, and each can be restarted independently of the other.

9.2 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 yourname account:


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

chmod g+rw /home/tomcat

10.0 Troubleshooting

10.1 Log Files To Watch


Look here for clues to Apache httpd.conf configuration issues, for example VirtualHost setup.


Look here for clues to Tomcat server.xml configuration issues. This file is written to when Tomcat starts and stops. It also catches System.out and System.err.


Look here for clues to mod_jk configuration issues.

© 2003 Merit Online Systems, Inc.

Red Hat Enterprise Linux 5 64 位安装

Red Hat Enterprise Linux 5 64 位的安装使用
  • 2017年03月15日 18:48

Red Hat Enterprise Linux AS 5下载链接

  • 2014年04月02日 16:00
  • 412B
  • 下载

Red Hat Enterprise Linux 6下安装Apache Httpd

1、进入Linux控制台并下载相关软件# wget、解压并进入安装目录# ...
  • u010397369
  • u010397369
  • 2013-07-22 20:10:47
  • 4736

在Red Hat Enterprise Linux AS release 3 (Taroon) 配置TOMCAT

在Red Hat Enterprise Linux AS release 3 (Taroon) 配置TOMCAT安装JDK我自己安装的是jdk1.5.0_09版本 在目录/usr/java/jdk1....
  • xuejiayue1105
  • xuejiayue1105
  • 2007-03-01 17:39:00
  • 1476

在虚拟机上安装Red Hat Enterprise Linux 5的教程

一、新建虚拟机 二、设置处理器及内存 三、添加ISO映像文件...
  • x_lychee
  • x_lychee
  • 2016-12-17 15:54:57
  • 966

Red Hat Enterprise Linux 6 64位安装

  • 2017年07月12日 19:38
  • 1.26MB
  • 下载

Red Hat Enterprise Linux 7.2

简介RedHat Enterprise Linux是Red Hat公司的Linux发行版,面向商业市场,包括大型机。红帽公司从Red Hat Enterprise Linux 5开始对企业版LINUX...
  • zuozewei
  • zuozewei
  • 2018-02-28 14:03:28
  • 64

Red Hat Enterprise Linux 6 系统安装

[1] Insert an installation DVD and restart Computer. Next, the following screen is shown, then Push ...
  • hanzheng260561728
  • hanzheng260561728
  • 2016-05-11 09:42:16
  • 667

虚拟机下red hat enterprise linux6 网络连接

一、设置本机网络:VM虚拟机安装后会在本机当中添加两个网络连接VMnet1和VMnet8,其中 网络连接VMnet1是host-only方式,在host-only方式下,虚拟系统和宿主机器系统是可以...
  • jesscia_p
  • jesscia_p
  • 2014-12-23 16:55:19
  • 1685

Red Hat Enterprise Linux Server 7.2

  • 2016年11月13日 16:00
  • 126B
  • 下载
您举报文章:Integrating Tomcat and Apache on Red Hat Linux 9 or Red Hat Enterprise Linux 3