Serve JSP and PHP from the same WebRoot

Sep 14, '05 08:57:00AM

Contributed by: deleted_user10

For a long time, I've wished to serve http from only one directory, but with support for both JSP and PHP. It's a bit hard to accomplish, because Tomcat can't handle PHP and Apache can't handle JSP. Therefore, some kind of integration is needed. There have been a couple of MacOSXHints concerning this subject, but none on exactly this aspect.

In this guide I'm using:

These are the current stable versions of each item, and from now on, I will only refer to these versions. If you are following this guide at a later time than September 7th, 2005, there might be newer versions. URL links and names on files to download etc., might differ.

[robg adds: Read on for the detailed setup instructions. I haven't tested this one, and there's a bit of assumed Terminal knowledge, but if you're installing Tomcat, you've probably got that already.]

Step 1: Download software

Go to the Tomcat 55 download page, and download the 5.5.9 tar.gz archive. You will probably need the 5.5.9 Compat tar.gz archive as well, from that same page. This is needed when running Tomcat with versions of Java older than 1.5, and Mac OS X 10.4.2 is using 1.4.2. Of course, you can install Java 1.5.x or later, and then you won't need Compat, but that's not covered in this guide.

Go to the this page, and navigate to find the latest version of JK for Apache 1.3.33. In my case, that's version 1.2.14 for Apache 1.3.33 [direct download link - 658KB].

Step 2: Installing and starting Tomcat

Locate the downloaded files. Double-click jakarta-tomcat-5.5.9.tar.gz and then the jakarta-tomcat-5.5.9-compat.tar.gz packages. When done, two folders will have been created:

Move these files: Trash jakarta-tomcat-5.5.9 2 and rename jakarta-tomcat-5.5.9 to Tomcat. Then move the Tomcat folder into /Library. Open a new Terminal window, and type pico ~/.profile. Pico is a command line text editor, and the previous command creates a new file and opens it for editing. Now type:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
Press control-X, type Y, and then hit the Enter key. Tomcat is now installed, but not started.

Step 3: Make Tomcat start before login

To make Tomcat start as a deamon before you login to your computer, we need to create a startup item. Create a folder called Tomcat in /Library/StartupItems. Within that folder, create a text file called Tomcat with the following content:
#!/bin/sh
##
# Start Tomcat
##

. /etc/rc.common

ConsoleMessage "Starting the Java WebServices framework"

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
export CATALINA_HOME="/Library/Tomcat"
export TOMCAT_HOME="/Library/Tomcat"
export JWSDP_HOME="/Library/Tomcat"

sh ${TOMCAT_HOME}/bin/startup.sh
Make sure the file you just created is executable by typing the following in the terminal:
chmod 755 /Library/StartupItems/Tomcat/Tomcat
Then, create a file called StartupParameters.plist in /Library/StartupItems/Tomcat, with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
    <key>Description</key>
    <string>Tomcat Servlet and JSP Server</string>
    <key>Messages</key>
    <dict>
        <key>start</key>
        <string>Starting Tomcat Server</string>
        <key>stop</key>
        <string>Stopping Tomcat Server</string>
    </dict>
    <key>OrderPreference</key>
    <string>None</string>
    <key>Provides</key>
    <array>
        <string>Tomcat</string>
    </array>
    <key>Requires</key>
    <array>
        <string>Resolver</string>
    </array>
</dict>
</plist>
Step 4: Install Tomcat Connector JK and configure Apache to use JK and PHP

In the Terminal, cd into the download directory where the previously-downloaded Jakarta-Tomcat connectors file is located is located. Then type:
sudo mv jakarta-tomcat-connectors-jk-1.2.14-macosx-apache-1.3.33.so \
/usr/libexec/httpd/mod_jk.so
We now have to make some changes to the httpd.conf (Apache's configuration file). Open httpd.conf in pico by typing the following in the Terminal:
sudo pico /etc/httpd/httpd.conf
Of course you can use other text editors like TextWrangler, as long as you can edit with root privileges. The basics you need to know when using pico are that Control-W is used to locate a text string (search), and control-X exits the program and asks if you want to save (if you have made any changes). Using Control-W, locate the following two lines, and remove the # at the front of each line:
#LoadModule php4_module
#AddModule mod_php4.c
Next locate this line:
DocumentRoot "/Library/WebServer/Documents/"
and change it to read:
DocumentRoot "/Library/Tomcat/webapps/ROOT"
Now find this line:
Directory "/Library/Webserver/Documents"
and change it to:
Directory "/Library/Tomcat/webapps"
Next, find this line:
DirectoryIndex index.html
and change it to:
DirectoryIndex index.jsp index.html
Finally, go to the end of the file and paste this bit of code:
#
# Tomcat connector configuration
#
# Loads and adds the module
 LoadModule jk_module  libexec/httpd/mod_jk.so
 AddModule  mod_jk.c

#
# Tells Apache where the worker configuration file is
 JkWorkersFile /Library/Tomcat/conf/workers.properties

# Logging options
 JkLogFile /Library/Tomcat/logs/mod_jk.log
 JkLogLevel error
 JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
 JkRequestLogFormat "%w %V %T"

# All Applications in Tomcat automatically gets 
# mapped and WEB-INF are automatically protected
 JkAutoAlias /Library/Tomcat/webapps
 
# Deny direct access to WEB-INF in ROOT
#
 <Directory /Library/Tomcat/webapps/ROOT/WEB-INF/>
 AllowOverride None
 Deny From All
 </Directory>

# Files ending with .jsp or files with a directory named 
# "servlet" in the url is sent to Tomcat (to worker ajp13)
 JkMount /*.jsp ajp13
 JkMount /*/servlet/* ajp13
Save your changes and close httpd.conf. Now everything is installed and configured.

Step 5: Starting it up

In the Terminal, cd into Tomcat's bin directory and start tomcat:
$ cd /Library/Tomcat/bin
$ ./startup.sh
Tomcat should now be started and accessible at http://localhost:8080/. Go into System Preferences > Sharing and enable "Personal Web Sharing." Now try http://localhost/. Your webroot is /Library/Tomcat/webapps/ROOT/; that's where to put all your web files. Tomcat applications are accessible as a sub-directory (i.e. http://localhost/application_name/).

The only thing I haven't got working so far is that Apache doesn't respect index.jsp as an index file in Tomcat applications. If anyone finds a solution, please submit as a comment. Please, submit comments on this. Suggestions about the security and other configurations are more than welcome.

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050909171712625