Wednesday, September 9, 2009

Integrate Apache, Apache-Tomcat


Obtaining mod_jk






mod_jk can be obtained in two formats - binary and source.  Depending on the platform you are running your web server on, a binary version of mod_jk may be available.  It is recommended to use the binary version if one is available.  If the binary is not available, follow the instructions for building mod_jk from source.  Notes at the end of this section offer recommendations for specific platforms.
Building mod_jk

Apache

  1. Make sure your Apache has DSO support. You can check this with $APACHE_HOME/bin/httpd -l. If you see "mod_so.c" in the output, DSO support is available. If it's missing, you may have to recompile or reinstall Apache.
  2. Find out whether your Apache has EAPI support. If you compiled it yourself from source, EAPI is probably not compiled in, unless you added it yourself (perhaps with mod_ssl). You need to build mod_jk.so with or without EAPI to match your Apache configuration. If you install a mismatched mod_jk.so, $APACHE_HOME/bin/apachectl configtest will warn you.
  3. Make sure you have Perl 5 installed. The apxs script used to build the module is written in Perl.
  4. Change directory to TOMCAT_HOME/native/mod_jk/apache1.3 (or apache2.0).
  5. Build mod_jk.so. Following are three techniques you can try, in order of simplicity:
    1. Run the build script for your platform.  If a build script is not available for your platform, you may be able to build mod_jk using ./build-unix.sh. This script will set some variables, call apxs as below, and try to copy mod_jk.so to $APACHE_HOME/libexec. If it fails, you need to do the following manually:
      • set JAVA_HOME in your shell, e.g. "set JAVA_HOME=/usr/local/jdk1.3.1; export JAVA_HOME"
      • set APACHE_HOME in your shell, e.g. "set APACHE_HOME=/usr/local/apache; export APACHE_HOME"
      • uncomment the following line in the build-unix.sh file, replacing "linux" with the name of your platform as specified in the Java include directory for your installation
      • # JAVA_INCLUDE="-I ${JAVA_HOME}/include -I ${JAVA_HOME}/include/linux"
    2. If build-unix.sh fails, you may have better luck with the Makefiles in the same directory, e.g. "make -f Makefile.linux mod_jk.so"
    3. Finally, you can try to build it manually. Run the apxs command that came with your apache distribution (hint: look in /usr/local/apache/bin, /usr/sbin, or wherever you installed apache). Type the command all on one line.
      For Linux:
      apxs -o mod_jk.so -I../jk -I/usr/local/jdk/include -I/usr/local/jdk/include/linux -c *.c ../jk/*.c
      Your build may fail because the object files from the ../jk directory have been compiled to the current directory, rather than their source directory. Running gcc -shared -o mod_jk.so *.o should finish the build. 
       

Configuring Apache

This section details the configuration that is required for the Apache Web Server to support mod_jk.

Removing mod_jserv directives

If you've previously configured Apache to use mod_jserv, remove any ApJServMount directives from your httpd.conf. If you're including tomcat-apache.conf or tomcat.conf, you'll want to remove them as well - they are specific to mod_jserv.  The mod_jserv configuration directives are not compatible with mod_jk!

Configure Apache to use mod_jk

The simplest way to configure Apache to use mod_jk is to use Tomcat to generate the mod_jk configuration file and put the following include directive at the end of your Apache httpd.conf file (make sure you replace TOMCAT_HOME with the correct path for your Tomcat installation: Include TOMCAT_HOME/conf/auto/mod_jk.conf
Example:
Include /usr/local/jakarta-tomcat/conf/auto/mod_jk.conf
This will tell Apache to use directives in the mod_jk.conf file in the Apache configuration.  This file is created by starting Tomcat with the "jkconf" option. Tomcat will initialize, write the configuration file and then exit. This may be done while an instance of Tomcat is running. Options for controlling how the mod_jk configuration file is generated are described in the configuring Tomcat section below [Configuring Tomcat].
NOTE:  If you plan to use the Tomcat generated configuration, skip the rest of this section and continue with the Configuring Tomcat section.
Custom configurations can be created by enabling the auto-configuration and copying the TOMCAT_HOME/conf/auto/mod_jk.conf file to your own configuration file, such as TOMCAT_HOME/conf/jk/mod_jk.conf.
The basic configuration is as follows:
  • You will need to instruct Apache to load Tomcat. This can be done with Apache's LoadModule and AddModule configuration directives.
  • You must inform mod_jk the location of your workers.properties file. Use mod_jk's JkWorkersFile configuration directive.
  • You should specify a location where mod_jk is going to place its log file and a log level to be used. Use the JkLogFile and JkLogLevel configuration directives. Possible log levels are debug, info, error and emerg. If the JkLogLevel is not specified, no log is generated.
  • The directive JkLogStampFormat will configure the date/time format found on mod_jk logfile. Using strftime() format string it's set by default to "[%a %b %d %H:%M:%S %Y] "
A simple example would be to include the following lines in your httpd.conf file:
LoadModule    jk_module  libexec/mod_jk.so
AddModule     mod_jk.c
JkWorkersFile /usr/local/jakarta-tomcat/conf/workers.properties
JkLogFile     /usr/local/apache/logs/mod_jk.log
JkLogLevel    info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

Assigning URLs to Tomcat

If you have created a custom or local version of mod_jk.conf as noted above, you can change settings such as the workers or URL prefix. Use mod_jk's JkMount directive to assign specific URLs to Tomcat. In general the structure of a JkMount directive is:
JkMount  
For example the following directives will send all requests ending in .jsp or beginning with /servlet to the "ajp13" worker, but jsp requests to files located in /otherworker will go to "remoteworker".
JkMount /*.jsp ajp13
JkMount /servlet/* ajp13
JkMount /otherworker/*.jsp remoteworker
You can use the JkMount directive at the top level or inside sections of your httpd.conf file.
 

 Table 4 - Excerpt from Apaches httpd.conf showing JK directives.

# Load mod_jk
#
LoadModule    jk_module  libexec/mod_jk.so
AddModule     mod_jk.c

# Configure mod_jk
#
JkWorkersFile /usr/local/jakarta-tomcat/conf/jk/workers.properties
JkLogFile     /usr/local/apache/logs/mod_jk.log
JkLogLevel    info

# First Virtual Host.
#

  DocumentRoot /web/host1
  ServerName host1.apache.org
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13


# Second Virtual Host. Also accessible via HTTPS
#

  DocumentRoot /web/host2
  ServerName host2.apache.org
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13



  DocumentRoot /web/host2
  ServerName host2.apache.org
  SSLEngine On
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13



No comments:

Post a Comment

tag ur valuable ideas below