Configuring an IIS 7 front-end for Apache Tomcat, using AppCmd.exe

I wanted to automate as much as possible the process of configuring the Apache Tomcat Redirector for IIS 7. You can find detailed how-to documents describing how to configure the ISAPI filter manually, but I couldn’t find any ready-made scripts to automate the process using IIS 7’s AppCmd command. My goal is eventually to get Apache and an IIS front-end running in Windows Azure, but this will have to wait for another post!

The first thing you need to do is of course to download the ISAPI filter binaries from the Apache repository. Be careful to download the 64bit version if you run a 64bit OS, like Windows Server 2008 R2, or you will see error messages when IIS tries to load the IIS filter.

The other thing I couldn’t find immediately are the sample configuration files that are mentioned in the Apache documentation (workers.properties and uriworkermap.properties); I finally found them in the source archive, so I would advise you to download this as well, and look in the conf directory for the samples.

Then you need to organize things somewhat on your drive; I arranged the various files like so:

 C:.
+---conf
|       uriworkermap.properties
|       workers.properties
|
+---isapi
|       isapi_redirect.dll
|       isapi_redirect.properties
|
\---logs

A couple notes:

  • You do not need to modify the registry like some older docs instructed; the configuration is contained in the isapi_redirect.properties file
  • Do not rename isapi_redirect.dll and isapi_redirect.properties; some magic configuration things seem to happen based on the name of the DLL
  • The logs directory will contain, obviously, the ISAP filter logs

And now, the isapi_redirect.properties file:

 # Configuration file for the Jakarta ISAPI Redirector

# The path to the ISAPI Redirector Extension, relative to the website
# This must be in a virtual directory with execute privileges
extension_uri=/jakarta/isapi_redirect.dll

# Full path to the log file for the ISAPI Redirector
log_file=C:\jk\logs\isapi_redirect.log

# Log level (debug, info, warn, error or trace)
log_level=info

# Full path to the workers.properties file
worker_file=C:\jk\conf\workers.properties

# Full path to the uriworkermap.properties file
worker_mount_file=C:\jk\conf\uriworkermap.properties

And now we need to configure IIS so that it will run the ISAPI filter; there are basically four tasks we need to accomplish:

  • Register the DLL as an ISAPI filter
  • Allow execution of the ISAPI filter
  • Create a virtual directory pointing to our isapi directory above
  • Allow executables (i.e., our DLL) in the new virtual directory

And without further ado, the script!

 PATH %PATH%;%SystemRoot%\System32\inetsrv

for /f "tokens=*" %i in ('appcmd.exe list site /text:name') do set SITE=%i

appcmd.exe set config /section:isapiCgiRestriction /+[@start,description='Tomcat',path='C:\jk\isapi\isapi_redirect.dll',allowed='true']

appcmd.exe set config /section:isapiFilters /+[@start,name='Tomcat',path='C:\jk\isapi\isapi_redirect.dll']

appcmd.exe add vdir /app.name:"%SITE%/" /path:/jakarta /physicalPath:c:\jk\isapi

appcmd.exe set config "%SITE%/jakarta" /section:system.webServer/handlers /accessPolicy:Read,Write,Execute

iisreset.exe /restart

Here are a few details about what is going on:

  1. Add the path to the AppCmd.exe command to the system path
  2. Run the “list site” command to retrieve the name of the default site, and store it in the SITE variable. Note: this won’t work if you have more than one IIS site! Please adapt the script if you need to!
  3. Add the DLL to the isapiCgiRestriction section
  4. Add the DLL to the isapiFilters section
  5. Create the new virtual directory (please note it has to be the same as the value provided in isapi_redirect.properties)
  6. Fix the access policy on the new virtual directory

I hope this helps, at least it Worked On My Machine! ™