首页 > IT基础架构 > 网络安全 > Tomcat-Apache HOWTO Tomcat+Apache HOWTO (转)

Tomcat-Apache HOWTO Tomcat+Apache HOWTO (转)

原创 网络安全 作者:worldblog 时间:2007-12-07 10:27:28 0 删除 编辑
Tomcat-Apache HOWTO Tomcat+Apache HOWTO (转)[@more@]">The Jakarta Project

The mighty a href=tomcat - Meow!" border=0 height=71 src="/images/1165979275562.gif" width=100>

Tomcat-Apache HOWTO

This document explains how to connect Tomcat to the popular open source web server, Apache. It was originally part of Tomcat: A Minimalistic User's Guide by Gal Shachor, but has been split off for organizational reasons. It should be considered a work in progress. Since the Tomcat source tree is constantly changing, the information herein may be out of date. The only definitive reference at this point is the source code.

Other important documents:

Other Tomcat-Apache HOWTOs: [should be integrated into this one?]

  • Howto Configure Tomcat 3.1 with Apache by Freddie Mendoza
  • .NET/cca/software/unix/apache/index.shtml">Apache Web Server / JServ / Tomcat / SSL Installation on UNIX by Jan K. Labanowski
  •">Tomcat and JServ by Jun Inamori
  • Java/Tech/servlet/tomcat.html"> in Japanese

Table of Contents

    [write me]

Apache - Tomcat Cooperation - Sample Server Integration

Up until now we have not discussed Tomcat as a server add on, instead we have considered it as a stand-alone container and discussed how it can be used.  There are however a few problems with this picture:

  1. Tomcat is not as fast as Apache when it comes to static pages.
  2. Tomcat is not as configurable as Apache.
  3. Tomcat is not as robust as Apache.
  4. There are many sites with long time investment in certain web servers, for example, sites that are using CGI scripts/Server api modules/perl/PHP.  We cannot assume that all of them will want to ditch this legacy.

For all these reasons it is recommended that real world sites use an industrial strength web server, such as Apache, for serving the static content of the site, and use Tomcat as a Servlet/JSP add-on.

Our agenda:

  1. Cover the fundamental behavior of the web server.
  2. Explain what configuration is needed.
  3. Demonstrate this on Apache.

Common Installation and Configuration Problems

This section isn't meant to be your one-stop shop for all troubles Tomcat, but a resource for stumbling blocks common to many first-time Tomcat'ers.  See the help section for additional links.

  http://webserver:8007/ gives a 500

This is what you should see in your tomcat.log file:


By default, Tomcat listens for AJP connections on port 8007.  AJP is the protocol used to communicate between the web server and Tomcat, not Tomcat and your browser.  To test your Tomcat installation, FIX ME ?

  and directives ignored

FIX ME Apache never applies because forwarded to Tomcat.

  Web server won't start when Tomcat is running

FIX ME Port conflict.

  "Bad command or filename" when executing Tomcat scripts

[FIX ME] UNIX file format on DOS.  Because Tomcat is developed on *nix (rather, the jars are built and distributed there), you may have to convert the files to PC (versus UNIX) format.

Starting Tomcat From Another Directory

Setting Tomcat to Cooperate with the Apache Web Server

Web Server Operation

In a nutshell a web server is waiting for client HTTP requests. When these requests arrive the server does whatever is needed to serve the requests by providing the necessary content. Adding a servlet container may somewhat change this behavior. Now the web server needs also to perfoRM the following:

  • Load the servlet container adapter library and initialize it (prior to serving requests).
  • When a request arrives, it needs to check and see if a certain request belongs to a servlet, if so it needs to let the adapter take the request and handle it.
The adapter on the other hand needs to know what requests it is going to serve, usually based on some pattern in the request URL, and to where to direct these requests.

Things are even more complex when the user wants to set a configuration that uses virtual hosts, or when they want multiple developers to work on the same web server but on different servlet container JVMs. We will cover these two cases in the advanced sections.

What is the Needed Configuration

The most obvious configuration that one can think of is the identity of the servlet URLs that are under the responsibility of the servlet container. This is clear; someone must know what requests to transmit to the servlet container... Yet there are additional configuration items that we should provide to the web-server/servlet-container combination:

  • We also need to provide configuration regarding the available Tomcat processes and on which TCP/IP host/port they are listening.
  • We need to tell the web server the location of the adapter library (so it will be able to load it on startup).
  • We need to set adapter internal information such as where and how much to log, etc.
All this information must appear either in the web server configuration, or in a private configuration files used by the adapter. The next section will demonstrate how configuration can be implemented on Apache.

Making it on Apache

This section shows you how to configure Apache to work with Tomcat; it tries to provide explanations as well as insight for the configuration directives that you should use. You can find additional information in the jserv install page .

When Tomcat starts up it will automatically generate a configuration file for Apache in TOMCAT_HOME/conf/jserv/tomcat-apache.conf. Most of the time you don't need to do anything but include this file (appending "Include TOMCAT_HOME/conf/jserv/tomcat-apache.conf") in your httpd.conf. If you have special needs, for example an AJP port other the 8007, you can use this file as a base for your customized configuration and save the results in another file. If you manage the Apache configuration yourself you'll need to update it whenever you add a new context.

Tomcat: you must restart tomcat and apache after adding a new context; Apache doesn't support configuration changes without a restart. Also the file TOMCAT_HOME/conf/jserv/tomcat-apache.conf is generated when tomcat starts, so you'll need to start Tomcat before Apache. Tomcat will overwrite TOMCAT_HOME/conf/tomcat-apache.conf each startup so customized configuration should be kept elsewhere.

The Apache-Tomcat configuration uses Apache core configuration directives as well as Jserv unique directives so it may confuse you at first, there are however two things simplifying it:

  • In general you can distinguish between the two directive "families" by noting that all the Jserv unique directives start with an "ApJServ" prefix.
  • The entire Tomcat related configuration is concentrated in a single configuration file named tomcat.conf, or the automatically generated tomcat-apache.conf, so you can look at a single file.
Lets look now at a sample tomcat.conf file. A Minimalistic Apache-Tomcat Configuration

########################################################### # A minimalistic Apache-Tomcat Configuration File # ########################################################### # Note: this file should be appended or included into your httpd.conf # (1) Loading the jserv module that serves as Tomcat's apache adapter. LoadModule jserv_module libexec/ # (1a) Module dependent configuration. # (2) Meaning, Apache will not try to start Tomcat. ApJServManual on # (2a) Meaning, secure communication is off ApJServSecretKey DISABLED # (2b) Meaning, when virtual hosts are used, copy the mount # points from the base server ApJServMountCopy on # (2c) Log level for the jserv module. ApJServLogLevel notice # (3) Meaning, the default communication protocol is ajpv12 ApJServDefaultProtocol ajpv12 # (3a) Default location for the Tomcat connectors. # Located on the same host and on port 8007 ApJServDefaultHost localhost ApJServDefaultPort 8007 # (4) ApJServMount /examples /root # Full URL mount # ApJServMount /examples ajpv12://hostname:port/root

As you can see the configuration process was split into 4 steps that will now be explained:

  1. In this step we instruct Apache to load the jserv shared-object (or the NT world dll). This is a well known Apache directive. If the loading went well and the module came from a file named mod_jserv.c (1a) we can start with the rest of the Jserv-Tomcat configuration.
  2. This step sets various Jserv internal parameters, these parameters:
    • Instruct jserv not to start the Tomcat process. Automatically starting Tomcat is not implemented yet.
    • Disable the secret key challenge/response between Apache and Tomcat. Again, the secret key work is not implemented yet.
    • Instruct jserv to copy the base server mount points (see next section) in case of virtual hosting.
    • Instruct jserv to use the notice log level. Other log levels include emerg, alert, crit, error, warn, info and debug.
  3. This step sets the default communication parameters. Basically it says that the default protocol used for the communication is ajpv12 (do not mess with this one) and that the Tomcat process runs on the same machine and listens on port 8007. If you run Tomcat on a machine other than the one used for Apache you should either update your ApJServDefaultHost or use a full URL when mounting contexts (see next). Also, if you configured the Tomcat connectors to use a port other then 8007, you should update your ApJServDefaultPort or use a full URL when mounting contexts.
  4. This step mounts a context to Tomcat. Basically it says that all the web server paths that start with /examples go to Tomcat. This ApJServMount example is a rather simple one, in fact ApJServMount can also provide information regarding the communication protocol to be used and the location where the Tomcat process listens, for example:
    ApJServMount /examples ajpv12://hostname:port/rootmounts the context /examples to a Tomcat process that runs on host "hostname" and listens on port number "port".

    来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录