Version 32

                           Using mod_jk with JBoss

     

    -


                    WARNING: DO NOT USE THIS WIKI PAGE!!!

       Apache Tomcat and JBoss recommend using mod_jk1.2.x connectors only. Both mod_jk and mod_jk2 are deprecated, unsupported and no further developments are going on in the community

    Please use this other wiki instead: UsingMod_jk1.2WithJBoss.

    -


     

     

     

     

     

    Note: Soon, the functionality provided by mod_jk1.2 will be replaced by a new module, mod_proxy_ajp, that will be included in Apache 2.1. *

    Preface

     

     

    There are relatively few differences between the non-integrated stock Tomcat that you download from Tomcat. The main differences are that it has been made into an MBean and placed in an exploded archive SAR; it is also integrated with JBossSX security via some custom valves and the such; and it has been integrated with JBoss's deployers/classloaders. You will find the standard server.xml embedded in the server/xxx/deploy/jbossweb-tomcat41.sar/META-INF/jboss-service.xml or for JBoss 3.2.4 and later in the

    server/xxx/deploy/jbossweb-tomcat50.sar/server.xml.

     

    Integrating with mod_jk2

     

    The author recommends that you use mod_jk2, and not mod_jk. mod_jk2 is a much more robust and performant implementation, and it works both with Apache 1.3 and Apache 2.0.

     

     

    NOTE: This wiki page is old. Current recommendation from Apache Tomcat team and JBoss is that you should use mod_jk1.2 connectors only.

     

     

     

     

    1) First step : install Apache.

     

     

    This should be pretty straigthforward. Apache is available at http://httpd.apache.org. We recommend that you take the latest version of Apache 2. For the rest of this document, APACHE_HOME will represent your apache install directory.

     

     

     

     

    2) Second step : configure Apache to serve your static content.

     

     

    Apache can be used to serve your static content (images, css, javascript files) faster than JBoss. As an example, if you store your static content in c:\static, add those lines to APACHE_HOME/conf/httpd.conf.

     
    Alias /static 'c:/static/' 
    <Directory 'c:/static/'> 
     Options Indexes MultiViews 
     AllowOverride None 
     Order allow,deny 
     Allow from all 
    </Directory> 
    

     

     

     

    3) Third step : download mod_jk2.

     

     

    mod_jk2 is available on Jakarta's 'binary downloads' page http://jakarta.apache.org/site/binindex.cgi, under the name 'Tomcat Web Server Connectors'.

    Copy the mod_jk2 library into the APACHE_HOME/modules directory.

    Add this line at the end of APACHE_HOME/conf/httpd.conf :

     
    LoadModule jk2_module modules/mod_jk2-xx.xx.xx.xxx 
    

     

    (replace mod_jk2-xx.xx.xx.xxx by the name of your mod_jk2 library).

     

     

     

     

    4) Fourth step : Configure Apache to forward requests to mod_jk2.

     

     

    Create a file called APACHE_HOME/conf/workers2.properties.

    As an example, if you have 2 web applications, 'webapp1' and 'webapp2', write into that file :

     
    # Log file, replace APACHE_HOME by your Apache installation directory 
    [shm] 
    file=APACHE_HOME/logs/mod_jk.log 
    size=1048576 
     
    # Example socket channel, override port and host. 
    [channel.socket:localhost:8009] 
    port=8009 
    host=127.0.0.1 
     
    # define the worker 
    [ajp13:localhost:8009] 
    channel=channel.socket:localhost:8009 
     
    # Uri mapping 
    # The JBoss JMX Console 
    [uri:/jmx-console/*] 
    worker=ajp13:localhost:8009 
     
    #The JBoss Web Console 
    [uri:/web-console/*] 
    worker=ajp13:localhost:8009 
     
    [uri:/webapp1/*] 
    worker=ajp13:localhost:8009 
     
    [uri:/webapp2/*] 
    worker=ajp13:localhost:8009 
    

     

     

     

    5) Fifth step : Use Apache/mod_jk2 as a load balancer.

     

     

    Apache/mod_jk2 is commonly used as a software load balancer (see http://jroller.com/page/julien.dubois/20040111).

    Let's edit APACHE_HOME/conf/workers2.properties, add a load balancer that redirects to 2 servers (server1 and server2), and redirect the requests of webapp1 and webapp2 to this load balancer.

     

     
    # Log file, replace APACHE_HOME by your Apache installation directory 
    [shm] 
    file=APACHE_HOME/logs/mod_jk.log 
    size=1048576 
     
    # The load balancer 
    [lb:lb] 
     
    # Server 1, override port and host. 
    [channel.socket:server1:8009] 
    port=8009 
    host=server1 
     
    # Server 2, override port and host. 
    [channel.socket:server2:8009] 
    port=8009 
    host=server2 
     
    # define worker for server 1 
    [ajp13:server1:8009] 
    channel=channel.socket:server1:8009 
    group=lb 
     
    # define worker for server 2 
    [ajp13:server2:8009] 
    channel=channel.socket:server2:8009 
    group=lb 
     
    # Uri mapping 
    [uri:/webapp1/*] 
    group=lb 
     
    [uri:/webapp2/*] 
    group=lb 
    

     

     

     

    6) Sixth step : Using sticky sessions.

     

     

    With the previous configuration, each request is load balanced, either on server1 or server2.

     

    But when a user opens a Session on one server, it is a good idea to always forward this user's requests to the same server. Otherwise the user's session data would need to be synchronized between both servers.

     

    This is called a "sticky session", because the user is always using the same server he reached on his first request.

     

    For achieving this result, two files need to be configured.

    In workers2.properties, each worker must be given a tomcatId :

     

    [channel.socket:localhost:8009]
    port=8009
    host=127.0.0.1
    tomcatId=server1
    

     

    On each JBoss server, this tomcatId must be set as a Tomcat jvmRoute.

    The Tomcat server.xml file can be found in server/xxx/deploy/jbossweb-tomcat41.sar/META-INF/jboss-service.xml (JBoss 3.2.3) or

    server/xxx/deploy/jbossweb-tomcat50.sar/server.xml (JBoss 3.2.4 an later).

    In this file, search for :

     <Engine name="MainEngine" defaultHost="localhost">
     

    And change it into :

     

     <Engine jvmRoute="server1" name="MainEngine" defaultHost="localhost">
     

     

    Caveat: Using the "." (dot) character in jvmRoute/tomcatId (as in e.g. "my.host.com:8080") seems to break sticky sessions (at least with JBoss 3.2.3 and mod_jk2 2.0.4).

     

    Resources

     

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

     

    http://www.onjava.com/pub/a/onjava/2002/11/20/tomcat.html (Configuring Tomcat  and Apache With JK 1.2)

     

    http://www.cymulacrum.net/tomcat/tomcat_toc.html ( Apache Tomcat on Linux )

     

    http://www.mail-archive.com/tomcat-user%40jakarta.apache.org/

     

    http://raibledesigns.com/tomcat/(Apache 2.x + Tomcat 4.x + Load Balancing)