Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Serve JSP and PHP from the same WebRoot UNIX
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:
  • Mac OS X 10.4.2
  • The Mac OS X built-in Apache (version 1.3.33)
  • Tomcat 5.5.9
  • Jakarta Tomcat Connector 1.2.14 (JK-1.2.14)
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:
  • jakarta-tomcat-5.5.9
  • jakarta-tomcat-5.5.9 2
Move these files:
  • jakarta-tomcat-5.5.9 2/bin/jmx.jar to jakarta-tomcat-5.5.9/bin/jmx.jar
  • jakarta-tomcat-5.5.9 2/common/endorsed/xercesImpl.jar to jakarta-tomcat-5.5.9/common/endorsed/xercesImpl.jar
  • jakarta-tomcat-5.5.9 2/common/endorsed/xml-apis.jar to jakarta-tomcat-5.5.9/common/endorsed/xml-apis.jar
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.
    •    
  • Currently 2.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[16,628 views]  

Serve JSP and PHP from the same WebRoot | 10 comments | Create New Account
Click here to return to the 'Serve JSP and PHP from the same WebRoot' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Serve JSP and PHP from the same WebRoot
Authored by: davipt on Sep 14, '05 11:13:17AM

Quick comment:

You can also use mod_rewrite and mod_proxy, instead of mod_jk, and use some crazy rules to redirect the traffic to the tomcat instance.

Something like this (not double checked):
RewriteEngine On

# Rewrite and proxy all .jsp requests to tomcat
RewriteRule ^(.*\.jsp) http://localhost:8080$1 [P,L]

# Rewrite all /admin/* requests to tomcat (isn't it called admin, the tomcat's web admin interface ?)
RewriteRule ^(/admin/.*) http://localhost:8080$1 [P,L]

# If the file corresponding to the requested URL exists in the tomcat webroot's directory, proxy the url to the tomcat instance
RewriteCond /opt/tomcat/webroot/%{REQUEST_FILENAME} -f
RewriteRule ^(.*) http://localhost:8080$1 [P,L]

# If the request ends in a '/' and there is a corresponding index.jsp in tomcat, proxy the request to it
RewriteCond /opt/tomcat/webroot/%{REQUEST_FILENAME}/index.jsp -f
RewriteRule ^(.*)/ http://localhost:8080$1/index.jsp [P,L]

There's so many nice things that can be done with mod_rewrite and mod_proxy.



[ Reply to This | # ]
Serve JSP and PHP from the same WebRoot
Authored by: deleted_user10 on Sep 15, '05 05:51:30PM

Will this "integration" be transparent to the user, so that the user never see the url with :8080? Is it protecting the WEB-INF as well?



[ Reply to This | # ]
Serve JSP and PHP from the same WebRoot
Authored by: Zareste on Sep 14, '05 07:11:25PM

I followed all the directions and didn't get any errors (except when I had to restart to do step 5), and now the normal server isn't working at all. The tomcat control panel comes up when I put in localhost:8080 but the normal domain doesn't work.



[ Reply to This | # ]
Serve JSP and PHP from the same WebRoot
Authored by: deleted_user10 on Sep 15, '05 05:57:08PM
Isn't Apache starting up at all? There might be something wrong with the httpd.conf. Try to enter the fellowing in the terminal:
sudo apachectl configtest

You should now see what might be wrong in the configuration. If you can't make it work. Replace httpd.conf with the default-config by entering this in the temrinal:
sudo cp /etc/httpd/httpd.conf.default /etc/httpd/httpd.conf

Then you have to redo step 4.

[ Reply to This | # ]
Serve JSP and PHP from the same WebRoot
Authored by: Tetonne on Sep 18, '05 05:30:28AM

Can someone explain us how to do the same with Apache 2.054?
thanks



[ Reply to This | # ]
Serve JSP and PHP from the same WebRoot
Authored by: Tetonne on Sep 18, '05 05:37:50AM

Where do i declare
JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home

when using apache 2?

thanks



[ Reply to This | # ]
Serve JSP and PHP from the same WebRoot
Authored by: Tetonne on May 15, '06 07:31:27AM

export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home"


in tomcat (/Library/Startup items/



[ Reply to This | # ]
Serve JSP and PHP from the same WebRoot
Authored by: Tetonne on Sep 19, '05 09:26:06AM

a PreferencePanes to start and stop would be great (like in Serverlogistics products)



[ Reply to This | # ]
Serve JSP and PHP from the same WebRoot
Authored by: deleted_user10 on Sep 19, '05 10:40:02AM
cd into Tomcat's bin-dir

cd /Library/Tomcat/bin
To stop Tomcat:

./shutdown.sh
To start Tomcat:

./startup.sh


[ Reply to This | # ]
Warning!
Authored by: deleted_user10 on Nov 28, '05 11:25:57AM

I've discovered a serious security issue:
It is possible to make the remote web server disclose the source
code of its JSP pages by requesting the pages with a different
case (ie: filename.JSP instead of filename.jsp).



[ Reply to This | # ]