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:
Tomcat User's Guide Mod_jk HOWTO [??? Should Be Rolled Into Tomcat-Apache Howto] Jakarta FAQ Page
Other Tomcat-Apache HOWTOS: [SHOULD BE Integrated Into this one?]
Howto Configure Tomcat 3.1 with Apache by Freddie Mendoza Apache Web Server / JServ / Tomcat / SSL Installation on by Jan K. Labanowski Tomcat and JServ by Jun Inamori http://www.dmz.hitachi-sk.co.jp/Java/ UNIX 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 ConchedRed It As a Stand-Alone Container and Discussed How It Can Be Used. There Arevel A Few Problems with this picture Picture:
Tomcat is not as fast as Apache when it comes to static pages. Tomcat is not as configurable as Apache. Tomcat is not as robust as Apache. 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 WorldTes Use An Industrial Streath Web Server, Such As Apache, for Serving The Static Content of the Site, And Use Tomcat As a servlet / JSP Add-on.
OUR Agenda: Cover The Fundamental Behavior of The Web Server. Explain What Configuration In Needed. 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 shop see in your tomcat.log file:
Handler Thread Problem: java.io.ioException: stream broker
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?
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 the ", 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 .
What is the needed concigurity
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 Implement 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 do not 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 does not 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. 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 Arevel 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 shop be appended or incrons instpd.conf
# (1) Loading The Jserv Module That Serves As Tomcat's Apache Adapter.
LoadModule jserv_module libexec / mod_jserv.so
# (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
ApjServdefault 8007
# (4)
ApjServmount / Examples / root
# Full Url Mount
# ApjServmount / Examples Ajpv12: // Hostname: Port / root
Ifmodule>
As you can see the configuration process WAS SPLIT INTO 4 Steps That Will Now Be Explained:
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 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. 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 listen s 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. 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 AND PROVIDE INFORMATION REGARDING The Communication Protocol To BE Used and the location where the tomcat process listens, for example:
ApjServmount / Examples Ajpv12: // Hostname: Port / root
mounts the context / examples to a Tomcat process that runs on host "hostname" and listens on port number "port". Now that you understand the different configuration instructions in the sample file, how can you add it to the Apache configuration? One " simple "method is to write it's content in the httpd.conf (the Apache configuration file), this however can be very messy. Instead you should use the Apache include directive. At the end of the Apache configuration file (httpd.conf) add The Following Directive: Include
For example:
INCLUDE / TOME / TOMCAT / TOMCAT.CONF
This will add your Tomcat configuration to Apache, after that you should copy the jserv module to the Apache libexec (or modules in the Win32 case) directory and restart (stop start) Apache. It should now be able to connect to Tomcat.
Obtaining the jserv module (mod_jserv)
AS Previously Stated, We NEED A Web Server Adapter to Sit in apache and redirect requests to tomcat. For apache, this adapter is a slightly modified version of mod_jserv.
You may try to look here and see if there is an already pre-built version of mod_jserv that suites your OS (Usually there is one for NT), however, being a native library you should not expect that yet (too many OS's, not enough developers, life too short ...). moreover, small variations in the way you built Apache / your specific UNIX variant may result in dynamic linking errors. you should really try to build mod_jserv for your system (do not panic, it Is Not That Hard!).
Building Mod_JSERV ON UNIX Involves The Following:
Download The Source Distribution of Tomcat from here. Uncompress it inteo some directory. Building the module:
Change Directory Into Jakarta-Tomcat / SRC / Native / Apache / Jserve / Execute The Build Command
APXS -C -O MOD_JSERV.SO * .C
APXS IS Part of The Apache Distribution and Should Be Located in Your Apache_Home / Bin. Building MOD_JSERV for
Win32 is Less Likely (You Already Have A Downloadable DLL for Win32). YET IF YOU
Want to Build It You Should Install Visual C and Perform The Following:
Download The Source Distribution of Tomcat from here. Unzip it into Some Directory. Building the module:
Change Directory Into Jakarta-Tomcat / SRC / Native / Apache / JSERV Add Visual C INTO YOUR Environment By Executing The Script vcvars32.bat. Execute The Build Command
nmake -f makefile.win32
NMAKE IS The Visual C Make Program. That's it; you have built mod_jserv ...
Making Apache Serve Your Context's Static Files
The previous Apache-Tomcat configuration file was somewhat inefficient, it instructed Apache to send any request for a resource that starts with the / examples prefix to be served by Tomcat. Do we really want that? There are many static files that may be a part OUR Servlet Context (for EXAMPLE IMAGES AND Static HTML, Why Should Tomcat Serve these Files?
You May Actually Have Reasons for Doing That, for example:
You may want to configure Tomcat based security for these resources You may want to follow users requests for static resources using interceptors In general however, this is not that case;.. And making Tomcat save static files is just a CPU waste We should instead. Have apache serve these static files and not tomcat. lets look now at a name Tomcat.conf File That Does Exactly That:
Having Apache Serve The Static Files Requires The Following:
Instructing Apache to send all servlet requests to Tomcat Instructing Apache to send all JSP requests to Tomcat and leaving Apache to handle the rest Lets look now at a sample tomcat.conf file that does exactly that:... Apache-Tomcat Configuration where Apache Serves The Static Content
######################################################################################################################################################################################################################################################################################################## ####################
# Apache-Tomcat Smart Context Redirection #
######################################################################################################################################################################################################################################################################################################## ####################
LoadModule Jserve Jserve_Module Modules / ApachemodulejServ.dll
ApjServmanual on
ApjServdefaultProtocol Ajpv12
ApjServsecretkey Disabled
ApjServmountCopy ON
ApjServloglevel Notice
ApjServdefaulthost Localhost
ApjServdefault 8007
#
# Mounting a single smart context:
#
# (1) make apache know about the context location.
Alias / Examples D: / Tomcat / WebApps / Examples
# (2) Optional, Customize Apache Context Service.
Options Indexes FollowSymlinks
# (2a) No Directory Indexing for the Context root.
#Boptions -indexes
# (2b) Set index.jsp to be the directory index file.
# DirectoryIndex index.jsp
Directory>
# (3) Protect The Web-INF Directory from Tampering.
ALLOWOVERRIDE NONE
Deny from all
Location>
# (4) Instructing apache to send all the .jsp Files Under the context to the
# Jserve Servlet Handler.
SetHandler jserv-servlet
Locationmatch>
# (5) Direct Known Servlet Urls to Tomcat.
APJSERVMOUNT / EXAMPLES / Servlet / Examples
# (6) Optional, Direct Servlet Only Contexts To Tomcat.
ApjServmount / servlet / root
Ifmodule>
As you can see, the beginning of this configuration file is the same as seen in the previous example The last step (mounting a context), however, was replaced in a long series of Apache and ApJServ configuration directives that will now be explained.: This step informs Apache of the context location and aliases it to an Apache virtual directory This way Apache can serve files from this directory This optional step instructs Apache more about how to serve the context;.. for example you can decide if Apache will allow directory indexing (listing) or set a special index file. This step instructs Apache to protect the WEB-INF directory from client access. for security reasons it is important to prevent visitors from viewing the content of the WEB-INF directory, for example web. xml can provide valuable information for intruders. This step blocks the WEB-INF content from visitors. This step instructs Apache to serve all the jsp locations within the context using the jserv servlet handler. The servlet handler redirects these requests based on the default host and port. This step mounts specific servlet URLs to Tomcat. You should note that you should have as many such mount directives as the number of specific servlet URLs. This last step is an example for the addition of servlet only context to Tomcat. It is easy to see that this configuration is much more complex and error prone then the first example, this however is the price that you should (for now) pay for improved performance.
Configuring for Multiple Tomcat JVMS
Sometimes it is useful to have differences handled by Different JVMS, for Example:
When each context serves a different, specific task and runs on a different machine. When we want to have multiple developers work on a private Tomcat process but use the same web server. Implementing such schemes where different contexts are served by different JVMs is very easy And The Following Configuration File Demonstrates this: Apache-Tomcat Configuration with Per Context JVM
######################################################################################################################################################################################################################################################################################################## ####################
# Apache-Tomcat with JVM Per Context #
######################################################################################################################################################################################################################################################################################################## ####################
LoadModule Jserve Jserve_Module Modules / ApachemodulejServ.dll
ApjServmanual on
ApjServdefaultProtocol Ajpv12
ApjServsecretkey Disabled
ApjServmountCopy ON
ApjServloglevel Notice
ApjServdefaulthost Localhost
ApjServdefault 8007
# Mounting the first context.
ApjServmount / Joe Ajpv12: //joe.corp.com: 8007 / Joe
# Mounting the second context.
ApjServmount / Bill Ajpv12: //bill.corp.com: 8007 / BILL
Ifmodule>
As you can see in the previous example, using several JVMs (even even those run on different machines that) can be accomplished easily by using a full ajp URL mount. In this full URL we actually specify the host where the Tomcat process is located and It's port.
Had the two Tomcat processes run on the same machine, we would have to configure each of them with different connector ports For example, assuming that the two JVMs runs on localhost, the Apache-Tomcat configuration should have something that looks like.:
Same Machine Multiple JVM Apache-Tomcat Configuration
######################################################################################################################################################################################################################################################################################################## ####################
# Apache-Tomcat with Same Machine JVM per context ################################################################# ###############################################
LoadModule Jserve Jserve_Module Modules / ApachemodulejServ.dll
ApjServmanual on
ApjServdefaultProtocol Ajpv12
ApjServsecretkey Disabled
ApjServmountCopy ON
ApjServloglevel Notice
ApjServdefaulthost Localhost
ApjServdefault 8007
# Mounting the first context.
ApjServmount / Joe Ajpv12: // localhost: 8007 / JOE
# Mounting the second context.
ApjServmount / Bill Ajpv12: // Localhost: 8009 / Bill
Ifmodule>
Looking at the above file you can see that we have two explicit ApJServ mount points each pointing to a different port on the same machine. It is clear that this configuration requires support from the configuration found in the server.xml files. We will need in . these files different
Joe's Server.xml File
Xml Version = "1.0" encoding = "ISO-8859-1"?>
Note, The Log Files Are Suffixed with _joe to Distinguish
Them from the Bill Files.
->
Path = "logs / tomcat_joe.log" CustomOutput = "YES" /> Path = "logs / servlet_joe.log" CustomOutput = "YES" /> PATH = "logs / jasper_joe.log" verbositylevel = "information" />
Note, The Work Directory Is Suffixed with _joe to Distinguish IT from the bill Work Directory. -> ClassName = "org.apache.tomcat.context.autosetup" /> ClassName = "org.apache.tomcat.context.defaultcmsetter" /> ClassName = "org.apache.tomcat.context.workdirinterceptor" /> Classname = "org.apache.tomcat.context.webxmlreader" /> ClassName = "org.apache.tomcat.context.loadOnStartupinterceptor" /> ClassName = "org.apache.tomcat.request.simplappper" debug = "0" /> ClassName = "org.apache.tomcat.request.sessioninterceptor" /> ClassName = "org.apache.tomcat.request.securityCheck" /> ClassName = "org.apache.tomcat.request.fixheaders" /> ClassName = "org.apache.tomcat.service.simpletcpconnector"> Name = "Handler" Value = "org.apache.tomcat.service.connector.ajp12connectionhandler" /> Connectionor> ContextManager> Server> When Looking At Server_Joe.xml You Can See That The Bill's Server.xml File Xml Version = "1.0" encoding = "ISO-8859-1"?>
Note, The Log Files Are Suffixed with _bill to distinguish . -> PATH = "logs / tomcat_bill.log" CustomOutput = "YES" /> Path = "logs / servlet_bill.log" CustomOutput = "YES" /> PATH = "logs / jasper_bill.log" Verbositylevel = "Information" />
Note, The Work Directory Is Suffixed with _bill to distinguish IT from the Joe Work Directory. -> ClassName = "org.apache.tomcat.context.autosetup" /> ClassName = "org.apache.tomcat.context.defaultcmsetter" /> ClassName = "org.apache.tomcat.context.workdirinterceptor" /> Classname = "org.apache.tomcat.context.webxmlreader" /> ClassName = "org.apache.tomcat.request.simplappper" debug = "0" /> ClassName = "org.apache.tomcat.request.sessioninterceptor" /> ClassName = "org.apache.tomcat.request.securityCheck" /> ClassName = "org.apache.tomcat.request.fixheaders" /> for it's ajp communication -> Name = "Handler" Value = "org.apache.tomcat.service.connector.ajp12connectionhandler" /> Connectionor> Context> ContextManager> Server> The port configuration is not the only place where the joe and bill configuration differs. We have @@@ marks in the xml files marking the four places where changes had to be made. As you can see, this difference is necessary to avoid the two Tomcat Processes from Overwriting EACH Other's logs and workspace. Tomcat Processes Using The Tomcat Processes Using The -f Command Line Option: Bin / starup -f conf / server_joe.xml Bin / starup -f conf / server_bill.xml And The Access Them From Apache Based on The Different URL PATH Prefixes. Configuring Virtual HostingIt is possible to support virtual hosts under Tomcat Ver3.1, in fact the virtual host configuration is very similar to configuring for multiple JVM (as explained in the previous section) and the reason is simple; in Tomcat 3.1 each virtual host is Implement by a different Tomcat Process. With the current (Ver3.1) Tomcat, virtual hosting awareness is provided by the web server (Apache / Netscape ...). The web server virtual hosting support is used by the Tomcat adapter to redirect requests belonging to a certain virtual host to the JVM (s) containing the contexts of this virtual host This means that if (for example) we have two virtual hosts (vhost1 and vhost2), we will have two JVMs:. one running the contexts of vhost1 and the other running the contexts of vhost2 . These JVMs are not aware of each others existence, in fact, they are not aware of the concept of virtual hosting. All the virtual hosting logic is inside the web-server adapter. To make things clearer, lets look at the following sample Apache -Tomcat configure file: Apache-Tomcat Configuration with Virtual Hosts Support ######################################################################################################################################################################################################################################################################################################## #################### # Apache Tomcat Virtual Hosts Sample Configuration # ######################################################################################################################################################################################################################################################################################################## #################### LoadModule Jserve Jserve_Module Modules / ApachemodulejServ.dll ApjServmanual on ApjServdefaultProtocol Ajpv12 ApjServsecretkey Disabled ApjServmountCopy ON ApjServloglevel Notice ApjServdefaulthost Localhost ApjServdefault 8007 # 1 CREANG An Apache Virtual Host Configuration NamevirtualHost 9.148.16.139 # 2 mounting the first Virtual Host ApjServmount / Examples Ajpv12: // localhost: 8007 / Examples Virtualhost> # 3 mounting the second virtual host Servername www.vhost2.com ApjServmount / Examples Ajpv12: // localhost: 8009 / Examples Virtualhost> Ifmodule> As can be seen, steps 1,2 and 3 define two Apache virtual hosts and for each of them, mount the / examples context to a certain ajpv12 URL. Each such ajpv12 URL points to a JVM that contains the virtual host. The configuration of the two JVMs is very similar to the one demonstrated in the previous section, we will need again to use two different server.xml files (one for each virtual host process) and we will need to start the Tomcat processes with the -f command line Option. after doing this will beable. The need for improved virtual host supportHaving each virtual host implemented by a different JVM is a huge scalability problem. The next versions of Tomcat will make it possible to support several virtual hosts within the same Tomcat JVM. Credits .................. WITH HELP FROM (in alphabetical order): Jonathan Bnayahu Alex Chaffee Fiona Czuczman Costin Manolache Rob Slifka Copyright © 1999-2001 The Apache Software Foundationlegal Stuff The Make US SayContact Information