Integrating Tomcat with ApacheIf you write JavaServer Pages or use Servlets to provide the functionality of your Web site,...
Once installed, you can startup and shutdown Tomcat using the provided scripts. These are in the bin directory and called ...
JkLogFile specifies the location of a log for mod_jk errors and information. JkLogLevel and JkLogStampFormat specify what ...
If the error was generated by Tomcat, then you need to check the Tomcat logs in the main Tomcat                           ...
Upcoming SlideShare
Loading in …5
×

Integrating tomcat with apache

1,364 views

Published on

Published in: Design, Technology
  • Be the first to comment

  • Be the first to like this

Integrating tomcat with apache

  1. 1. Integrating Tomcat with ApacheIf you write JavaServer Pages or use Servlets to provide the functionality of your Web site, youre probably already aware of Tomcat. Tomcat is the Apache Foundationsreference implementation of the JavaServer Pages and Servlet technologies. Tomcat 3 covers the Servlet 2.2 and JSP 1.1 revisions, while Tomcat 4 covers Servlet 2.3 and JSP1.2. Tomcat itself is part of the Jakarta Project, which is a suite of Java development tools developed through the Apache foundation.Installing Tomcat itself is relatively easy — download the corresponding installer from the Tomcat pages at Apache, expand the files or run the installer, and then use thecorresponding script to start up the Tomcat service. Tomcat has its own built-in HTTP service that handles and services requests from clients. Well look at the specific stepslater in this article.To integrate the Tomcat server processes with the Apache HTTP server we need the mod_jk module, which implements the interface between Tomcat and Apache, combinedwith some small steps to configure Apache and Tomcat to our needs.Tomcat is written in Java, while Apache and its modules are written in C, so how do the two work together?Well, when Tomcat executes as well as creating an HTTP listener service, it also creates a number of "worker" processes responsible for executing JSPs/servlets. The mod_jkmodule is written in C, so its incapable of interpreting Java classes directly. Instead, mod_jk communicates with the various worker processes created by Tomcat through anetwork connection. There is, of course, slightly more to it than that, but the core execution is handled through Apache to Tomcat and back again.There are some advantages to this method, even though it looks convoluted. The primary advantage is flexibility. For example, if you wanted to run Apache (and mod_jk) onone physical server but the Tomcat service and the actual JSPs and servlets on another machine, you can. Some companies use this method to provide an additional level ofsecurity, with the Tomcat server behind another firewall only accessible from the Apache server.Another advantage is stability — if a significant failure within Tomcat caused it to fail completely, it wouldnt render your entire Apache service unusable, just your servlets andJSP pages.Well be using Tomcat 4.1.18, although the connector pack well be using will actually work with any version of Tomcat you want to use.Installing TomcatTomcat is very easy to install, as the only prerequisite is Java. If you dont already have the Java SDK (you need the SDK rather than the runtime) you can get a copy of Javafor most platforms here.After youve installed Java, make sure you set your JAVA_HOME environment variable to point to your Java installation, as its needed not only by Tomcat but also by othertools we use in this article. Youll probably also want to set your PATH variable to point to the bin directory within the Java directory so you have easy access to all the Javatools.Now download Tomcat. Theres no installer under Unix/OS X, so once downloaded, extract the package and then copy the directory created to where you want to store yourTomcat installation. Within Windows use the installer.
  2. 2. Once installed, you can startup and shutdown Tomcat using the provided scripts. These are in the bin directory and called startup (Windows) or startup.sh (Unix). By defaultTomcat runs on port 8080. To change this, edit the file conf/server.xml in the Tomcat directory and replace 8080 with the port number you want to use.You can test your installation by opening up a web browser and entering a suitable URL for your machine.Shutdown scripts are provided in the bin directory if you want to shutdown your Tomcat installation.Installing mod_jkThe easiest way to get hold of a suitable mod_jk library is to go to the projects build directory. From there youll find a number of directories for different platforms. Within eachdirectory are two files, mod_jk for use with Apache 1.3.x and the other for use with Apache 2.x.Download the file and copy it into your libexec or modules directory within your Apache server installation.Building mod_jkIf you really want (or need) to, you can build your own mod_jk from the source. Youll need to download the Jakarta Tomcat Connectors pack. Youll also need Ant, the ApacheJava build engine.Original date of publication, 05/03/2003Next: Configuration and BeyondAs with other modules, there are two parts to the configuration. The first just tells Apache that the module exists and where to find it using a LoadModule directive. The secondpart tells the module how to function within the Apache environment. For mod_jk the minimum we need is:LoadModule jk_module libexec/mod_jk.soJkWorkersFile /usr/local/apps/jakarta-tomcat-4 /conf/workers.propertiesJkLogFile /usr/local/apache/logs/mod_jk.logJkLogLevel infoJkLogStampFormat "[%a %b %d %H:%M:%S %Y] "The JkWorkersFile should point to the location of the file that specifies how mod_jk should connect to the Tomcat service and interact with the various worker processescreated by Tomcat.
  3. 3. JkLogFile specifies the location of a log for mod_jk errors and information. JkLogLevel and JkLogStampFormat specify what the log contains and what it looks like.The Workers file defines the various links between Apache, mod_jk and Tomcat. Essentially, Apache forwards any request for a given URL to the Tomcat service, but we musttell Apache how to identify which Tomcat process to communicate and forward the request.If you built mod_jk from the source you can find a sample workers.properties file that will work perfectly with the default configuration file for Tomcat in the jk/conf directory. Infact, its probably worth downloading the connector source code for that file alone!If you dont want to download the source, then you should be able to get by using the following:workers.tomcat_home=/usr/local/apps/jakarta-tomcat-4workers.java_home=/usr/java1.4.1ps=/worker.list=ajp13worker.ajp13.port=8009worker.ajp13.host=localhostworker.ajp13.type=ajp13That creates a connector called ajp13 (well need that in a minute), which is located on the local machine on port 8009. This is one of the standard ports listened to by Tomcat.You can define as many different connectors as you like, providing of course you have a corresponding Tomcat process listening on the right port.Forwarding RequestsThe final step is to tell Apache which files should be assigned to Tomcat for processing and which of the workers to direct the request to. You do this using the JkMountdirective, which effectively mounts a Tomcat worker process to a given file specification. For example, to redirect all requests for files ending in .jsp on this server to the workeridentified as ajp13, you would use:JkMount /*.jsp ajp13The file specification uses Unix-like wildcard specifications, so we could have specified that all files be forwarded to Tomcat using:JkMount /* ajp13Testing it OutTo test out your installation, copy a suitable JSP or servlet into your Web server directory and then try accessing it from a browser. Remember that you must have both Tomcatand Apache running. Assuming everything is working OK then you should get the desired result. If theres an error, youll need to check one of the various log files for moreinformation. To help you determine which one, look at the error message reported in the browser.
  4. 4. If the error was generated by Tomcat, then you need to check the Tomcat logs in the main Tomcat installation directory. It probably means either there is an error in your JSP or a class on which it relies or there is a problem with accessing the file in question. Because requests are forwarded to Tomcat immediately, a missing file will be reported by Tomcat, not Apache.Unsure About an Acronym or Term? If its an error generated by Apache, check the Apache logs and the mod_jk logs. If the error is reported in Search the ServerWatch Glossary the mod_jk log, then it probably points to a problem with the configuration somewhere. Go! If you get an Internal Server Error from Apache, then check whether Tomcat is running before diggingany deeper.Original date of publication, 05/03/2003

×