Newsroom Update

Beginning in May, a special Today at Apple series titled “Made for Business” will offer small business owners and entrepreneurs free opportunities to learn how Apple products and services can support their growth and success. Learn more >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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.

Posted on Jan 28, 2014 10:11 AM

Reply
4 replies

Apr 2, 2015 9:50 AM in response to James Bucanek

Hi James,


The way you have it configured, tomcat will always be running, since it is controlled by launchd directly. The webapp.plist provides a `launchKeys` key which, according to the sample configuration, says "Launchd plists in /Applications/Server.app/Contents/ServerRoot/System/Library/LaunchDaemons are loaded when webapp is started". This implies to me that if one copied their launchctl .plist into this directory, and added it as a key to the `launchKeys` array that `webappctl` would `load` and `unload` this configuration from launchd when the webapp was started and stopped (either from `webappctl` or from the Server.app GUI). I'm curious if you've tried this, and what your experiences were, since I'm not having any luck getting this aspect to work as I expect.


Many thanks,


Levi

Apr 17, 2016 11:58 PM in response to James Bucanek

I've tried this and numerous other methods on OS X Server 10.11.4 El Capitan (Server 5.1). I first obtained Tomcat using Homebrew, then I followed this as well as a slightly modified version that levigroker posted in another thread here: Configure Yosemite Server WebApp with LaunchD however all I see in my logs are messages such as "Service exited with abnormal code: 1". every time I try to have it load up as a LaunchDaemon. I'm able to load tomcat just fine manually from the command line with catalina start command. As for the web app portion in the Server.app. I added a new site gave it a different name from my server so like tomcat.mydomain.com instead of myservername.mydomain.com and it's configured to run on port 80 by default, which is what I want. When I setup the web app plist file I see you do the following:


<key>includeFiles</key>

<array>

<string>/Library/Server/Web/Config/apache2/httpd_tomcat_jsp_app.conf</string>

</array>

and then you also create the httpd_tomcat_jsp_app.conf file


<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>


Tomcat is configured to run on port 8080 out of the box why do you have it set to go to port 8009? Should this be changed to 8080 if tomcat is running on 8080?


Also are there any other logs I can look at to see what's going on with these abnormal code errors?


Thanks.

Adding Tomcat and JSP to Mavericks Server

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.