Adding Tomcat and JSP to Mavericks Server
I've been using Apache and Tomcat on OS X Server since 10.4. As many have noticed, support for Java and all related technologies have evaperated from OS X. I, however, have far too much invested in Apache, MySQL, Tomcat, and Java to ignore them.
I recently abandoned my trusty Xserve running 10.6 Server in favor of a shiny new MacMini running OS X Server 10.8. Naturally, I need Tomcat. After many hours of internet searches, experiments, and false starts, I was able to install Tomcat and get it running seamlessly behind Apache. I couldn't find any discussion of this, so I thought I'd document my solution in the hopes it helps someone else.
These instructions apply to Java 1.7, Tomcat 7, and OS X Server 10.8.
(Officially, Tomcat 7 currently recommends Java 1.6, but I haven't had any problems using 1.7.)
(1) Install Java and Tomcat
Get Java from Oracle: <http://www.oracle.com/technetwork/java/javase/downloads/index.html>
Get Tomcat from Apache: <http://tomcat.apache.org/download-70.cgi>
I choose to relocation my tomcat-... folder to /Library/Tomcat. All of the following scripts and paths assume this, so adjust them if you decide to locate your tomcat-... folder somewhere else.
(2) Get Tomcat to startup automatically
Based on a set of scripts from <https://gist.github.com/mystix/661713>, I installed the following files:
/Library/Tomcat/bin/launchd_wrapper.sh
#!/bin/bash
# NOTE: this is an OSX launchd wrapper shell script for Tomcat (to be placed in $CATALINA_HOME/bin)
CATALINA_HOME=/Library/Tomcat
function shutdown() {
date
echo "Shutting down Tomcat"
$CATALINA_HOME/bin/catalina.sh stop
}
date
echo "Starting Tomcat"
export CATALINA_PID=/tmp/$$
# Uncomment to increase Tomcat's maximum heap allocation
# export JAVA_OPTS=-Xmx512M $JAVA_OPTS
. $CATALINA_HOME/bin/catalina.sh start
# Allow any signal which would kill a process to stop Tomcat
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP
echo "Waiting for `cat $CATALINA_PID`"
wait `cat $CATALINA_PID`</code>
/Library/LaunchDaemons/org.apache.tomcat.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!-- NOTE: place this file in /Library/LaunchDaemons -->
<plist version="1.0">
<dict>
<key>Label</key> <string>org.apache.tomcat</string>
<key>OnDemand</key> <false/>
<key>RunAtLoad</key> <true/>
<key>EnvironmentVariables</key>
<dict>
<key>CATALINA_HOME</key> <string>/Library/Tomcat</string>
<!--<key>JAVA_HOME</key> <string>/Library/Java/Home</string>-->
<key>JAVA_OPTS</key> <string>-Djava.awt.headless=true</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>/Library/Tomcat/bin/launchd_wrapper.sh</string>
</array>
<key>ServiceDescription</key> <string>Tomcat</string>
<key>StandardErrorPath</key> <string>/Library/Tomcat/logs/launchd.stderr</string>
<key>StandardOutPath</key> <string>/Library/Tomcat/logs/launchd.stdout</string>
<key>UserName</key> <string>root</string>
</dict>
</plist>
Run the command sudo launchctl load /Library/LuanchDaemon/com.apache.tomcat.plist to start it immediately, or restart the server.
(3) Serve .jsp pages using Tomcat
To get .jsp pages to be processed by Tomcat requires Apache to hand off the processing of specific files to Tomcat. In the good ol' days, this was often a matter of hand-patching various apache .conf files. But as I soon discovered, the new OS X Server has a much more elegant solution. By creating a couple of files, I was able to turn Tomcat into a "web app" that can be enabled for a website with a click of a button. Here are the files to add:
/Library/Server/Web/Config/apache2/webapps/org.apache.tomcat.jsp.webapp.plist
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!-- See man pages for webapp.plist(5) and webappctl(8) for information about this example webapp.plist -->
<plist version="1.0">
<dict>
<key>includeFiles</key>
<array>
<string>/Library/Server/Web/Config/apache2/httpd_tomcat_jsp_app.conf</string>
</array>
<key>name</key>
<string>com.apache.tomcat.jsp.webapp</string>
<key>displayName</key> <!-- Name shown in Server app -->
<string>Java Server Pages</string>
<key>requiredModuleNames</key>
<array>
<string>rewrite_module</string>
<string>proxy_ajp_module</string>
</array>
<key>installationIndicatorFilePath</key>
<string>/Library/Tomcat/bin/tomcat-juli.jar</string>
<key>sslPolicy</key>
<integer>0</integer>
</dict>
</plist>
/Library/Server/Web/Config/apache2/httpd_tomcat_jsp_app.conf
<IfModule rewrite_module>
<IfModule proxy_ajp_module>
# Redirect all *.jsp page requests to Tomcat:8009
RewriteEngine On
RewriteRule ^/(.+\.jsp)$ ajp://localhost:8009/$1 [P]
</IfModule>
</IfModule>
The rewrite rule directs Apache to pass all .jsp pages to the AJP connector running on port 8009. OS X Server (thankfully) comes pre-installed with the rewrite and ajp modules enabled. If the files you want Tomcat to process have a different naming convension, adjust or add additional RewriteRules to suit your needs.
Quit and relaunch the Server app. "Java Server Pages" is now an option (under "Advanced Settings") for any website.
Enjoy.