Posts Tagged ‘iis’

Apache Tomcat and IIS on a Windows box

Monday, February 16th, 2009

A regular httpd server can handle page requests and pass off only the parts of the page that need to be processed by Tomcat. The following install instructions are based on http://tomcat.apache.org/connectors-doc/webserver_howto/apache.htm.

Install the software

  • Install Apache on your server and serve pages on port 80. This is based off of version 2.2.6.
  • Install Tomcat to serve pages on 8080. This is based on version 6.0.16.
  • Install IIS to serve pages on port 8090. This is based on version 5.1 for Windows XP.
  • You will need a Java virtual machine running on your PC as well. Tomcat should detect this automatically.
  • You might need to configure Tomcat via the system tray program to point at the ‘jvm.dll’ file in ‘bin/client/’.

Apache Changes

Add the module

Tomcat Changes

web.xml file

Enable directory listings

  • Open web.xml located in TOMCAT_INSTALL_DIRECTORY/conf
  • Change the line directly UNDER <param-name>listings</param-name> to have a value of true.

server.xml file, part 1

  • Open the TOMCAT_INSTALL_DIRECTORY/conf/server.xml file.
  • In the Engine section, add this line <Listener className="org.apache.jk.config.ApacheConfig" modJk="/path/to/mod_jk.so" />.

workers.properties file

  • Create a TOMCAT_INSTALL_DIRECTORY/conf/jk directory, if it’s not there already.
  • Create a workers.properties file in TOMCAT_INSTALL_DIRECTORY/conf/jk.
  • Edit the workers.properties file as per here: http://tomcat.apache.org/connectors-doc/generic_howto/workers.html or use the following example:
    # the list of workers
    worker.list=worker1
    # individual properties
    worker.worker1.type=ajp13
    worker.worker1.host=localhost
    worker.worker1.port=8009
    
  • Restart Tomcat
  • You will now have a TOMCAT_INSTALL_DIRECTORY/conf/auto/mod_jk.conf file.
  • Edit the mod_jk.conf file to make it read as follows:
    # defines the workers file
    JkWorkersFile "TOMCAT_INSTALL_DIRECTORY/conf/jk/workers.properties"
    
    # defines the log file and level
    JkLogFile "TOMCAT_INSTALL_DIRECTORY/logs/mod_jk.log"
    JkLogLevel info
    
    # sets the base directory for Apache to handoff to Tomcat
    JkAutoAlias "APACHE_DOCUMENT_ROOT_DIRECTORY"
    
    # all files served by Tomcat to start
    JkMount /*.jsp worker1
    
    # Now make it serve static files using httpd
    # this command seems to be specific to Apache 2.2
    JkUnMount /*.html worker1
    JkUnMount /*.htm worker1
    JkUnMount /*.php worker1
    JkUnMount /*.asp worker1
    JkUnMount /*.css worker1
    JkUnMount /*.js worker1
    JkUnMount /*.jpg worker1
    JkUnMount /*.jpeg worker1
    JkUnMount /*.gif worker1
    JkUnMount /*.png worker1
    
  • You can change the location of this file (see below).

server.xml file, part 2

  • Remove the line that you added above – <Listener ........> (you can just comment it out).
  • Change the line starting with <Host name="localhost" appBase="webapps" ......> to read <Host name="localhost" appBase="FOLDER_ABOVE_THE_ROOT_DIRECTORY_YOU_COPIED_EARLIER".....>.
  • I.E., if you are now serving pages out of MyDirectory/ROOT, set appBase to be MyDirectory.

Apache Changes part 2

Copy the ROOT folder from Tomcat

  • In the TOMCAT_INSTALL_DIRECTORY/webapps, there is a ROOT folder.
  • Copy the WEB-INF folder in that location INTO your existing Apache directory that you serve pages out of (usually either htdocs or www).

Copy the other folders from Tomcat

  • In the TOMCAT_INSTALL_DIRECTORY/webapps, there are several other folders as well.
  • Copy these INTO the save folder level as the ROOT.
  • Copy TOMCAT_INSTALL_DIRECTORY/webapps/ROOT/index.jsp to the root of where you will serve pages out of and rename it to tomcat.jsp. This will allow you to access the Tomcat manager directly from a page.

httpd.conf file

  • Add the line LoadModule jk_module modules/mod_jk.so to your httpd.conf file.
  • Set DocumentRoot to the ROOT folder you just copied above.
  • Note that you also need to change any other references to the DocumentRoot folder to the new location.
  • Usually there is a Directory block that references this same folder for permissions.
  • Add this line to your httpd.conf file: Include TOMCAT_INSTALL_DIRECTORY/conf/auto/mod_jk.conf.
  • You can change the location of that file as long as you change it here as well.
  • Restart Apache and you should now be able to serve jsp pages.

Serving ASP pages with IIS out of the same directory

It’s possible to have all three webservers (Apache, Tomcat, and IIS) serve pages out of one directory.

  • When you see YOUR_WEB_SERVER below, you should NOT use localhost if others will connect to your machine externally. Use the FQDN for best results.
  • Use the setup above, and have IIS point to the same directory as Apache. Choose a new port for IIS.
  • Add the following to httpd.conf for Apache:
    <IfModule dir_module>
    DirectoryIndex index.html index.jsp index.php
    </IfModule>
    <IfModule alias_module>
    RedirectMatch 301 (.*)\.asp http://YOUR_WEB_SERVER:YOUR_IIS_PORT$1.asp
    </IfModule>
    
  • Download and install ISAPI_Rewrite for IIS from here: http://www.isapirewrite.com/ The free version is sufficient.
  • Put the following lines to the httpd.ini file this program creates (only specify a port if it’s not the default):
    [ISAPI_Rewrite]
    # anything that's php or jsp goes back to Apache
    RewriteRule  (.*)\.php http\://YOUR_WEB_SERVER$1.php [RP]
    RewriteRule  (.*)\.jsp http\://YOUR_WEB_SERVER$1.jsp [RP]
    

Now any pages that end in .asp will be sent to the IIS port on that machine, and any pages that are .php or .jsp will go back to Apache. Static files will be used by whatever web server you are connected to at that point.

Possible Gotcha using default.asp

It would be nice if you could specify default.asp as one of the files under DirectoryIndex above, but doing so means that Apache will look for this file after trying the other files. When the RewriteRule sees that, it processes it as a request for a .asp file, and redirects to IIS. So opening a directory with no default page results in a redirect, when all you want to do is browse the directory.