Google
   
       
  HOWTO : Apache2, Mod_JK2, Tomcat 4.1.24 and JDK 1.4 on RedHat Linux 7.3 : JNI    
       
  00. intro    
 

This document will describe how to setup Apache2, Java JDK 1.4, Tomcat 4.1.x and ModJK2 on Redhat Linux 7.3. Tomcat will communicate with Apache2 using JNI and will be launched automatically when Apache2 is started. Even though RPMs are available, most of the components will be built from source. The exceptions being the JDK 1.4.1 RPM and Tomcat 4.1.24 from a binary tarball.

NOTE: THIS DOCUMENT IS AN ATTEMPT TO DOCUMENT MY CURRENT CONFIGURATION. I am still having problems using JNI, including "jk2_init() Can't find child xxxxx in scoreboard" and browser hangups while attempting to access Tomcat content. I have moved on to using TCP/IP sockets due to time constraints.

 
     
  01. install apache2  
 

Download Apache2 from www.apache.org. The latest version at the time of this document was Apache2.0.46.

<INSTRUCTIONS ON DOWNLOADING AND UNCOMPRESSING THE APACHE2 TARBALL>

To build Apache, perform the following commands...

 
  ./configure --prefix=/usr/local/apache2 --enable-ssl --enable-module=so
make
make install
 
  A version of Apache2 should now be in /usr/local/apache2. You should now test the default config by using  
  /usr/local/apache2/bin/apachectl configtest  
  If the test returns "Syntax OK", you should start up Apache2 to verify it's operation  
  /usr/local/apache2/bin/apachectl start  
  Open http://localhost in a browser, which should display the Apache welcome page.  
     
  02. install Jdk1.4.1  
  <JDK INSTALATION INSTRUCTIONS HERE>  
     
  03. INSTALL TOMCAT  
 

<TOMCAT INSTALLATION INSTRUCTIONS HERE>

Note: Tomcat will not be configured to run at boot time since Apache2 will launch it automaticlly when started.

Finaly, set CATALINA_HOME and TOMCAT_HOME to point to your Tomcat installation. Add the following lines to /etc/profile.

 
 

export CATALINA_HOME='/usr/local/tomcat'
export TOMCAT_HOME='/usr/local/tomcat'

 
     
  04. INSTALL / BUILD MOD JK2  
 

In this HOWTO, ModJK2 was built from source since a prebuild version was not yet avaliable on the Jakarta site. You can find the source tarball on the Apache website.

http://jakarta.apache.org/builds/jakarta-tomcat-connectors/jk2/release/v2.0.2/src/

<explain how to decompress the archive into /user/local/src/jk2/>

The ModJK2 tarball uses Ant to build both the Java connectors for Tomcat and the native modules for Apache2. I was unable to use Ant to build ModJK2 but was successful building just the native components using make.

cd to /usr/local/src/jk2/jakarta-tomcat-connectors-jk2-2.0.2-src/jk/native2 and run configure with the following options...

 
  ./configure \
--with-apxs2=<apache-root-directory>/bin/apxs \
--with-tomcat41=<tomcat-root-directory> \
--with-java-home=<java-root-directory> \
--with-jni \
--with-pcre
 
  An example configure statement might look like this...  
  ./configure --with-apxs2=/home/apache/bin/apxs \
--with-tomcat41=/home/tomcat \
--with-java-home=/usr/java \
--with-jni \
--with-pcre
 
 

Finally, run make

 
  make  
 

This will build mod_jk2.so and jkjni.so in jakarta-tomcat-connectors-jk2-2.0.2-src/jk/build/jk2/apache2

Copy these files into the modules directory of your Apache2 installation and restart Aapache. On my system, the modules directory was located at...

/usr/local/Apache2/modules

 
     
  05. configure apache2  
 

ModJk2 is a Apache module, which is loaded using the LoadModule command in <apache2 install>/conf/httpd.conf. The syntax for loading the module is as follows..

 
  #
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule jk2_module modules/mod_jk2.so
 
  While many of the required modJK2 commnads can be placed directly in httpd.config, we will define what content Apache should pass to Tomcat in the workers2.properties file.  
     
  06. Create Workers2.properties  
 

The workers2.properties file configures runtime options of ModJK2 and defines what content should be passed on to Tomcat. The syntax for this file is documented at...

http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jk2/jk2/configtc.html

http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jk2/jk2/configtccom.html

To help you get started, here is the workers2.properties file for my system.

TODO: add virtual hosting to config

 
 

# workers2.properties

[config:]
#file=${serverRoot}/conf/workers2.properties
file=/usr/local/Apache2/conf/workers2.properties
debug=0
debugEnv=0

[uriMap:]
info=Maps the requests. Options: debug
debug=0

[shm:]
info=Scoreboard. Required for reconfiguration and status with multiprocess servers
file=/usr/local/apache2/logs/jk2.shm
size=1000000
debug=0
disabled=0

[workerEnv:]
info=Global server options
timing=1
debug=0

[lb:lb]
info=Default load balancer.
debug=0

[channel.socket:localhost:8009]
info=Ajp13 forwarding over socket
debug=0
tomcatId=localhost:8009

[channel.jni:jni]
info=The jni channel, used if tomcat is started inprocess

[vm:]
info=Parameters used to load a JVM in the server process
JVM=/usr/java/Java/jre/lib/i386/server/libjvm.so
OPT=-Djava.class.path=/usr/local/tomcat/bin/tomcat-jni.jar;/usr/local/tomcat/lib/commons-logging.jar
OPT=-Dtomcat.home=${TOMCAT_HOME}
OPT=-Dcatalina.home=${TOMCAT_HOME}
OPT=-Xmx128M
#OPT=-Djava.compiler=NONE
disabled=0

[worker.jni:onStartup]
info=Command to be executed by the VM on startup. This one will start tomcat.
class=org/apache/jk/apr/TomcatStarter
ARG=start
disabled=0
stdout=/usr/local/Apache2/logs/stdout.log
stderr=/usr/local/Apache2/logs/stderr.log

[worker.jni:onShutdown]
info=Command to be executed by the VM on shutdown. This one will stop tomcat.
class=org/apache/jk/apr/TomcatStarter
ARG=stop
disabled=0

[uri:/examples]
info=Example webapp in the default context.
context=/examples
debug=0

[uri:/examples/servlets/*]
info=Prefix mapping

[uri:/examples/*.jsp]
info=Extension mapping

[uri:/examples/*]
info=Map the whole webapp

[uri:/examples/servlets/HelloW]
info=Exampel with debug enabled.
debug=10

 
  Note: you may need to edit the paths in this file if you have installed Tomacat, Apache or the JDK in a different folder than suggested above.  
     
  06. create Jk2.properties  
  The file j2k.properties sets up the Tomcat side of the connection and where we configure Tomcat to run in process with Apache.  
  ## THIS FILE MAY BE OVERRIDEN AT RUNTIME. MAKE SURE TOMCAT IS STOPED
## WHEN YOU EDIT THE FILE.
## COMMENTS WILL BE _LOST_
## DOCUMENTATION OF THE FORMAT IN JkMain javadoc.
# Set the desired handler list
# handler.list=apr,request,channelJni
#
# Override the default port for the socketChannel
# channelSocket.port=8019
# Default:
# channelUnix.file=${jkHome}/work/jk2.socket
# Just to check if the the config is working
#shm.file=/usr/local/Apache2/logs/jk2.shm
# In order to enable jni use any channelJni directive
channelJni.disabled = 0
# And one of the following directives:
# apr.jniModeSo=/opt/apache2/modules/mod_jk2.so
# If set to inprocess the mod_jk2 will Register natives itself
# This will enable the starting of the Tomcat from mod_jk2
apr.jniModeSo=inprocess
 
     
  07. configure tomcat  
 

<GIVE BRIEF OVERVIEW OF TOMCAT CONFIG FILE>

Here is my trimmed down version of server.xml

 
 


<Server port="8005" shutdown="SHUTDOWN" debug="0">

<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" (and therefore the web applications visible
within that Container). Normally, that Container is an "Engine",
but this is not required.
Note: A "Service" is not itself a "Container", so you may not
define subcomponents such as "Valves" or "Loggers" at this level.
-->
<!-- Define the Tomcat Stand-Alone Service -->
<Service name="Tomcat-Standalone">
<!-- Define a Coyote/JK2 AJP 1.3 Connector on port 8009 -->
<Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
port="8009" minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="10" debug="0" connectionTimeout="0"
useURIValidationHack="false"
protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler"/>

<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host). -->
<!-- You should set jvmRoute to support load-balancing via JK/JK2 ie :
<Engine name="Standalone" defaultHost="localhost" debug="0" jmvRoute="jvm1">
-->

<!-- Define the top level container in our container hierarchy -->
<Engine name="Standalone" defaultHost="localhost" debug="0">
<!-- Global logger unless overridden at lower levels -->
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="catalina_log." suffix=".txt"
timestamp="true"/>
<!-- Define the default virtual host -->
<Host name="localhost" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- Tomcat Root Context -->
<!--
<Context path="" docBase="ROOT" debug="0"/>
-->
<!-- Tomcat Examples Context -->
<Context path="/examples" docBase="examples" debug="0"
reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_examples_log." suffix=".txt"
timestamp="true"/>

</Context>
</Host>
</Engine>
</Service>

</Server>

 
     
  08. Credits  
 

Here are just a few of the many resources used while setting up Apache2 and Tomcat. Many thanks to those who have taken the time to provide these resources online.

http://www.gregoire.org/howto/Apache2_Jk2_TC4.1.x_JSDK1.4.x.html

http://marc.theaimsgroup.com/?l=tomcat-user&m=105103815630094&w=2

http://www.johnturner.com/howto/rh72-howto.html

 
       
  Last updated on 06.04.2003