Home Articles Computing & Technology Tips & Tricks Installing Tomcat on Mac OS X

Installing Tomcat on Mac OS X

E-mail Print PDF

Having to work with Tomcat for an important project I am working on, I installed it on my Mac. However, on the beginning, I didn't make much effort: I just uncompressed the official archive in my /usr/local, and I was able to start and stop tomcat manually, that's all... But today I decided to learn a few things about Mac OS X, trying to install it properly. Let's see what I did!

This how-to is based on my experience installing Tomcat 6.0.16 on a Mac OS X 10.5.2 system.

Download and uncompress Tomcat

First, download from Tomcat's website the version of Tomcat you want to use. Choose the .tar.gz file in the Core section of the Binary Distributions. Let's say you just downloaded it to your Downloads directory (usually /Users/<your_short_username>/Downloads/). Open a Terminal and execute the following commands:

% cd /usr/local
% sudo tar xvzf ~/Downloads/apache-tomcat-6.0.16.tar.gz
% sudo ln -s apache-tomcat-6.0.16 tomcat

Tomcat should already be working, but we should not run it as root, since it may be a huge security issue. That is why we need to first create an unpriviledged user as well as its primary group.

Creating an unpriviledged user

We will be using the command line utility dscl for this. First, you need to choose the User ID and the Group ID, which must be a positive integer, lower than 500, since User IDs above 500 are reserved to normal users. You just have to pick a number between 0 and 500 which is not yet used by any other user or group. Check this out with the commands:

% dscl . -list /Groups PrimaryGroupID
% dscl . -list /Users UniqueID

For this how-to, I will be using the number 100. We can create the group and the user with the following commands:

% sudo dscl . -create /Groups/_tomcat PrimaryGroupID 100
% sudo dscl . -create /Groups/_tomcat RealName "Tomcat Users"
% sudo dscl . -create /Groups/_tomcat Password \*
% sudo dscl . -create /Users/_tomcat UniqueID 100
% sudo dscl . -create /Users/_tomcat PrimaryGroupID 100
% sudo dscl . -create /Users/_tomcat HomeDirectory /usr/local/tomcat
% sudo dscl . -create /Users/_tomcat UserShell /usr/bin/false
% sudo dscl . -create /Users/_tomcat RealName "Tomcat Administrator"
% sudo dscl . -create /Users/_tomcat Password \*

Choosing /usr/bin/false as the UserShell, and setting the Password to "*" turns this account unusable as a standard user account.

Changing ownership

Since Tomcat will be run as the _tomcat user, it can be better if its files own to this user (at least, it is more simple for permissions management). Just run the following command:

% sudo chown -R _tomcat:_tomcat /usr/local/tomcat/

Setting Tomcat as a StartupItem

Now, we must create a little startup script which will help us managing Tomcat for tasks such as starting, stoping and restarting. On Mac OS X, we can put such scripts in /Library/StartupItems/. I will assume you are familiar with vim, but you can use any other text editor:

% cd /Library/StartupItems/
% sudo mkdir Tomcat
% cd Tomcat/
% sudo vim Tomcat

Then, you need to copy-paste the following script:

/Library/StartupItems/Tomcat/Tomcat

#!/bin/sh
#
# /Library/StartupItems/Tomcat/Tomcat
#
# A script to automatically start up Tomcat on system bootup
# for Mac OS X.

# Suppress the annoying "$1: unbound variable" error when no option
# was given
if [ -z $1 ] ; then
echo "Usage: $0 [start|stop|restart] "
exit 1
fi

# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
. /etc/rc.common

# Tomcat's path
CATALINA_HOME=/usr/local/tomcat

StartService ()
{
if [ "${TOMCAT}" = "-YES-" ]
then
ConsoleMessage "Starting Tomcat Servlet/JSP Server..."
cd /
sudo -u _tomcat $CATALINA_HOME/bin/startup.sh
fi
}

StopService ()
{
ConsoleMessage "Stoping Tomcat Servlet/JSP Server... "
cd /
sudo -u _tomcat $CATALINA_HOME/bin/shutdown.sh
}

RestartService ()
{
ConsoleMessage "Restarting Tomcat Servlet/JSP Server... "
cd /
sudo -u _tomcat $CATALINA_HOME/bin/startup.sh
sleep 5
sudo -u _tomcat $CATALINA_HOME/bin/shutdown.sh
}

RunService "$1"

Then, you have to make this file executable:

% sudo chmod +x Tomcat

Then, you must create another file:

% sudo vim StartupParameters.plist

with the following content:

/Library/StartupItems/Tomcat/StartupParameters.plist

{
Description = "Tomcat Servlet/JSP Server";
Provides = ("Tomcat");
Requires = ("Resolver");
OrderPreference = "None";
Messages =
{
start = "Starting Tomcat Servlet/JSP Server... ";
stop = "Stopping Tomcat Servlet/JSP Server... ";
};
}

We now have a startup script, but we still need to say to Mac OS X to launch Tomcat on boot time... You just have to do it:

% sudo sh -c 'echo "TOMCAT=-YES-" >> /etc/hostconfig'

Using the startup script

From now on, each time you boot your system, Mac OS X will launch Tomcat as the user _tomcat. But if you want, you can also manage it manually. For example, to start Tomcat, you can run:

% sudo /Library/StartupItems/Tomcat/Tomcat start

Enjoy!

Last Updated ( Saturday, 17 May 2008 11:56 )  

Add your comment

Your name:
Subject:
Comment (you may use HTML tags here):
Type the characters you see in the picture below:
The word for verification. Lowercase letters only with no spaces.