WAMP Tomcat Environment
 
   
 
 
 
  www.ourbabynamer.com
 
 
 
 
 
 
 

Front Tomcat with Apache
and Mod Rewrite

Windows Vista / Apache / Mod Jk
Apache 2.2.13
Mod Jk 1.2.28

WAMP using Tomcat, PHP, MySql
Ilene Mortman, September 2009

Intro Downloaded Apache Mod JK
Configure Command Scripts Test Mod_rewrite

I have been doing development work using a Vista/Tomcat/Mysql/Php environment (WAMP using Tomcat). Since I wanted to be able to use some functionality that was available in Apache and not Tomcat, namely mod_rewrite, I decided to front my Vista web development environment with Apache. The steps I took to accomplish this are listed below. Since I already run Php and Mysql through Tomcat, I did not add them to Apache.

Please note that line breaks you may see here in the middle of lines are to format this page. No line breaks appear in the actual configuration files.

What I downloaded...
apache_2.2.13-win32-x86-openssl-0.9.8k.msi
mod_jk-1.2.28-httpd-2.2.3.so

From the following sites...
http://httpd.apache.org/download.cgi
(http://tomcat.apache.org/connectors-doc)
http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.28/
 


A.  Download and install Apache

  1. Determine which version of Apache you want, e.g. apache_2.2.13-win32-x86-openssl-0.9.8k.msi and download it.
  2. Run the msi file to install it.
  3. Determine whether you want to install it as a multi-user service or single user application. I installed it as an application.
  4. When prompted, specify localhost for your Network Domain and Server Name. Use whatever you'd like for the Admin Email address.
  5. If installed as a service, start and stop the service via the Apache Monitor (look for the icon in the system tray, bottom right of screen)
  6. If installed as a program, start by clicking on Start Apache in console (Start/ All Programs/ Apache HTTP Server 2.2/ Control Apache Server) or by typing in the following into a command window.
    cd "C:\Program Files\Apache Software Foundation\Apache2.2\bin\"
    httpd.exe
    
    Stop by typing cntl/c into the Apache console window or by typing the following into a seperate command window.
    TASKKILL /F /IM httpd.exe /T 
  7. Test Apache by starting it, bringing up a browser and entering http://localhost:80 and looking for a success page ("It works!").

    Make sure to test prior to proceeding because once changes are made to httpd.conf for the DocumentRoot, Directory tag, mod_jk and rewriteRules, you will no longer see the "It works!" page.

    Once the DocumentRoot and Directory tags are changed in the httpd.conf file, you will see the root directory when you enter http://localhost:80. Once you start adding JkMounts (Tomcat pass throughs) and RewriteRules you may get "Service Temporarily Unavailable" if you've passed things through and haven't started Tomcat, or the pass through/rewrite you desired if you did start Tomcat.

B.  Download and install the Apache Tomcat Connector Mod Jk

  1. You will need a connector for Apache to communicate with Tomcat. Download and run mod_jk_1.2.28-httpd-2.2.3.so.
  2. Change the name to mod_jk.so and put into the Apache modules folder (e.g. C:\Program Files\Apache Software Foundation\Apache2.2\modules

C.  Configure Apache and Mod Jk

  1. Modify Apache's httpd.conf file. For example, C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf.
  2. You may wish to uncomment the ServerName localhost:80 line. When I was not connected to the Internet, the following error would be displayed in the Apache console window if the line was left commented. Although I did not experience any actual problems, you may feel more comfortable uncommenting the line in order to avoid the error message and any possible problems.
    httpd.exe: Could not reliably determine the server's fully 
        qualified domain name, using 127.0.0.1 for ServerName
    
  3. Since all of the application files were already setup under Tomcat (please see the WAMP using Tomcat page and/or skip this step if it doesn't apply to your configuration) point the application directory to Tomcat. For example make the following changes,
    #DocumentRoot "C:/Program Files/Apache Software Foundation
                      /Apache2.2/htdocs"
    DocumentRoot "C:/Program Files/apache-tomcat-6.0.16/webapps"
    
    #<Directory "C:/Program Files/Apache Software Foundation
                       /Apache2.2/htdocs">
    <Directory "C:/Program Files/apache-tomcat-6.0.16/webapps">
    
  4. Add the lines below or similar ones. The JkMount lines define the file types which will be passed through to Tomcat for processing. All other file types will be handled by Apache.
    LoadModule jk_module modules/mod_jk.so
    JkWorkersFile "C:\Program Files\Apache Software Foundation\
                          Apache2.2\conf\workers.properties"
    JkLogFile "C:\Program Files\Apache Software Foundation\
                          Apache2.2\logs\mod_jk.log"
    JkLogLevel info
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
    
    JkMount /*.php worker1
    JkMount /*.jsp worker1
    JkMount /*Servlet worker1
    
  5. Create the workers.properties file in the same conf directory and add the lines below to it.
    worker.list=worker1
    worker.worker1.port=8009
    worker.worker1.host=localhost
    worker.worker1.type=ajp13
    worker.worker1.lbfactor=1
    

D.  Add to the Command Scripts

Please see the WAMP using Tomcat page for information on creating the scripts. These command lines apply to Apache if it was installed as an application. Other commands apply if it was installed as a service.

  1. Add the following lines to the beginning of startEnv.bat. Make it the first application started.
    cd "C:\Program Files\Apache Software Foundation\Apache2.2\bin\"
    start httpd.exe 
    
  2. Add the following lines to the end of stopEnv.bat. Make it the last application stopped.
    REM /F - force, /IM - image name, 
    REM /T - terminate process and all child processes 
    TASKKILL /F /IM httpd.exe /T
    

E.  Test

Test by running the startEnv.bat file and testing some of your previously created file types (e.g. .html, .php, servlets). Please see the WAMP using Tomcat page for further details on the test files. All passed through pages should be accessable whether going through port 8080 (Tomcat) or 80 (Apache). For example,
http://localhost:8080/phpFun/test.php
and
http://localhost:80/phpFun/test.php
should give you the same page.

F.  Mod Rewrite

The major reason I did this was to be able to use mod rewrite functionality. You may wish to experiment with this as well. Mod rewrite allows you to rewrite URLs. This is advantagous for making complex URLs simpler, hiding delivery methods from users, and redirecting from old pages to new. In a local development environment you can just add the rewrite rules to httpd.conf directly if you have admin access. If you do not, or would like to mimic an environment for which you do not (for example the production environment), then the rules would need to be added to a seperate .htaccess file.

Please note, if you create a .htaccess file which contains a RewriteEngine on line, you must uncomment the LoadModule rewrite_module modules/mod_rewrite.so line in the httpd.conf file in the Apache conf directory. This is whether or not you add any Rewrite rules. Otherwise you will only see pages that were passed through to Tomcat.

In your web application directory create a .htaccess file.

As it turns out I don't believe there is way to do mod rewrites of pages passed through to Tomcat using an .htaccess file. I tried alot of variations that I found by surfing (changing the order of the load modules, setting certain flags, etc. etc.), but there seems to be no way to force Apache to process the .htaccess file before doing the pass through. Because of this, the rewrites for my local development environment (in which I have full access), were done in the httpd.conf file.

For example, the following lines would redirect a .html pattern to a .php file. So if I had a file called test.php, I could access the page it generates from the URL test.html. RewriteRules have a variety of flags. In this case L was used so that no further rewrites would be done, and PT tells it to pass through. The RewriteEngine and RewriteRules lines should appear in the file before the JkMount lines. Restart Apache and Tomcat after making these and any other configuration changes before doing any testing.

RewriteEngine on
RewriteRule ^(.*).html$ $1.php [L,PT]

The completed httpd.conf changes look as follows. These include all changes to pass through php, jsp and servlets to Tomcat as well as a simple rewrite rule to rewrite .html requests to .php. Because I was playing with ordering, I left the original LoadModule rewrite_module commented out and just added my own.

LoadModule jk_module modules/mod_jk.so
LoadModule rewrite_module modules/mod_rewrite.so
JkWorkersFile "C:\Program Files\Apache Software Foundation\
                      Apache2.2\conf\workers.properties"
JkLogFile "C:\Program Files\Apache Software Foundation\
                      Apache2.2\logs\mod_jk.log"
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
RewriteLog "C:/Program Files/apache-tomcat-6.0.16/
               webapps/phpFun/rewrite.log"
RewriteLogLevel 9

RewriteEngine on
RewriteRule ^(.*).html$ $1.php [L,PT]

JkMount /*.php worker1
JkMount /*.jsp worker1
JkMount /*Servlet worker1