Jan
26
Guide for Installing Tomcat 5.0 on OS X 10.4 Tiger
Filed Under Computers & Tech, Software Development on January 26, 2006 at 10:10 pm
This is a step-by-step guide to installing Tomcat 5.0.x onto OS X 1.4.x. Note that this is the Tomcat branch for the 1.4 JDK and not for the 1.5 JDK. I know the latest versions of OS X now have Java 5 as part of the OS but my work is not yet ready to migrate to Java 5 so I’m staying with Tomcat 5.0 for now. The chances are that these instructions with only tiny and obvious alterations will work for Tomcat 5.5 with Java 5 but I’m not making any promises.
These instructions result in a Tomcat install that does things the RightWay(tm) and results in a Tomcat service that does not run as root, starts on boot, can be turned on and off with /etc/hostconfig
, runs headless to allow your web apps to use AWT classes and provides you with nice aliases for starting and stopping Tomcat as well as getting at the main log file.
Step 1 – Install the Files
Download and extract the core package in latest stable binary distribution in the 5.0 branch of Tomcat from http://tomcat.apache.org/ (currently 5.0.28). Then fire-up a terminal with root privileges (you can get to root with sudo bash
) and move the folder you extracted (jakarta-tomcat-5.0.28
) to /usr/local/
with mv jakarta-tomcat-5.0.28 /usr/local/
. Then, to make things easier in the future when upgrading tomcat and to make things easier in general you should create a simlink called tomcat
to the full name of the tomcat folder with ln -s /usr/local/jakarta-tomcat-5.0.28 /usr/local/tomcat
.
Step 2 – Set the Needed Environment Variables
Still with your root terminal open /etc/profile
with your favorite editor (for me that would be vi). We now need to add two environment variables, JAVA_HOME
to tell Tomcat what JVM to use and CATALINA_HOME
to tell Tomcat where to find itself. Because we are installing Tomcat 5.0 and not 5.5 we need to make JAVA_HOME
point at the 1.4 JDK and not the 1.5 JDK. To set these variables add the following to the bottom of /etc/profile
:
#For Tomcat export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home export CATALINA_HOME=/usr/local/tomcat
Then re-start your terminal for those changes to be noticable. You should now be able to start Tomcat with sudo /usr/local/tomcat/bin/startup.sh
and stop it with sudo /usr/local/tomcat/bin/shutdown.sh
.
Step 3 – Creat a Tomcat User and Group
You should create the user using the Accounts pane in the System Preferences App. Since this user will never need to log in you should set the password to something very long and very secure and don’t worry if you won’t remember it! The simplest thing is to call the user tomcat. When you create a user a group with the same name is automatically created as well so we don’t have to do that explicitly.
You now need to set this new user and group to own the Tomcat folder with chown -R tomcat:tomcat /usr/local/jakarta-tomcat-5.0.28
.
Step 4 – Create a Startup Item
Again, get yourself a root terminal and go to the folder /Library/StartupItems/
. Then create a new folder for the Tomcat startup item with mkdir Tomcat
and change into that folder with cd Tomcat
. Then, with your favorite editor create a file called Tomcat
and add the following content to it:
#!/bin/sh . /etc/rc.common export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home" export CATALINA_HOME="/usr/local/tomcat" export JAVA_OPTS="-Djava.awt.headless=true" if [ "${TOMCAT}" = "-YES-" ]; then ConsoleMessage "Starting Tomcat" su tomcat - /usr/local/tomcat/bin/startup.sh fi
Note that this version of the file runs Tomcat in so called Headless mode to allow you to play with cool graphics stuff from within your Servlets etc. For more see Decapitating java.awt!.
Next using your favorite editor again create another file called StartupParameters.plist
and add the following content to it:
{ Description = "Tomcat"; Provides = ("Tomcat"); Requires = ("Resolver"); OrderPreference = "None"; Messages = { start = "Starting Tomcat"; stop = "Stopping Tomcat"; }; }
Before we leave this folder we need to ensure that all the permissions are right. The Tomcat
folder and both files in it should be owned by root:wheel
, the folder should have permissions drwxr-xr-x
as should the file Tomcat
. StartupParameters.plist
should have permissions -rw-r--r--
.
Next you need to edit /etc/hostconfig
to add the line below to the end of the file:
TOMCAT=-YES-
Step 5 – Create a Controller Script
To easily stop and start tomcat running as our non-root user and headless we need a script. Tomcat often gets very grumpy when running as a non-root user if it does not own everything in it’s folders so my script also sets the ownership of the tomcat folders correctly before starting Tomcat. I called this script tomcatCtrl.pl
and put it in /Library/StartupItems/Tomcat/
. The script should contain the following:
#!/usr/bin/perl use strict; $|++; # # Tomcat Controler # # # Define constants # my $tomcatStartupItem = '/Library/StartupItems/Tomcat/Tomcat'; my $tomcatHome = '/usr/local/tomcat'; my $tomcatUser = 'tomcat'; my $tomcatGroup = 'tomcat'; # # Don't edit any lines below here # # first ensure we are root my $uname = `whoami`; unless($uname =~ m/root/){ print "\nERROR - tomcatCtrl.pl must be run as root. Please use sudo.\n\n"; exit; } # then ensure we have one argument and that it is either 'start' or 'stop' unless(scalar(@ARGV) == 1){ print "\nERROR - tomcatCtrl.pl takes 1 argument, either 'start' or 'stop'\n\n"; exit; } my $act = $ARGV[0]; unless($act eq 'start' || $act eq 'stop'){ print "\nERROR - the argument to tomcatctrl must be either 'start' or 'stop'\n\n"; exit; } # then chown the web-apps folder to the right user to ensure we don't have any funny business my $execString = "chown -R $tomcatUser:$tomcatGroup $tomcatHome/webapps"; print "\nEnsuring the tomcat user owns all the files in the webapps folder:\n $execString"; print `$execString`."\n"; # if we're bringing tomcat up, do so if($act eq 'start'){ $execString = "cd /;$tomcatStartupItem start"; print "\n\nBringing up Tomcat - $execString\n"; print `$execString`; } # if we're bring tomcat down, do so if($act eq 'stop'){ $execString = "$tomcatHome/bin/shutdown.sh stop"; print "\n\nBringing down Tomcat - $execString\n"; print `$execString`; }
Before this script will work you will have to make it executable with: sudo chmod 755 /Library/StartupItems/Tomcat/tomcatCtrl.pl
.
Step 6 – (Optional) Add Aliases
Rather than adding these aliases globally I think it’s better to just put them into your own account so open up a terminal as yourself. Then use your favorite editor to edit ~/.bash_profile
and add the following lines to the end:
# Tomcat Aliases alias tomcatup="sudo /Library/StartupItems/Tomcat/tomcatCtrl.pl start" alias tomcatdown="sudo /Library/StartupItems/Tomcat/tomcatCtrl.pl stop" alias tomcatlog="tail -f /usr/local/tomcat/logs/catalina.out"
And that’s it! You now have a tomcat install that starts on boot, runs as a non-privileges user and can be easily started and stopped.
Note (added 06 July 2006):
Depending on how you extract the tomcat files from the archive you download from their web page you may have to manually edit the permissions of some files in Tomcat’s bin
directory. If you get an error saying something like “startup.sh not found” then change into the bin
directory and set the permissions to 755:
cd /usr/local/tomcat/bin
sudo chmod 755 *.sh
[tags]Tomcat, OS X[/tags]
Note
This post was initially posted to my old blog here . Comments may no longer be posted there and should be posted here but there are still old comments there that people may be interested in.
[…] I’ve previously done a guide on the right way of installing Tomcat 5.0 on the Mac but things are a little different on Linux so I figured I’d do another guide. This one is a little less advanced because it only covers running Tomcat as root and not as a non-root user. Depending on how busy I am in the next while I may or may not do a follow-up article on the additional steps needed to run Tomcat as a non-root user. I have tested this procedure on RHEL ES 4 with Tomcat 5.5.17 and the Sun JDK version 1.5.0_6 but it should be the same on all Linux distros and for all 5.X Tomcat versions, the only thing that is likely to change is the location of $JAVA_HOME. Correction, the startup script included is for Redhat based distros only (RHEL, Fedora, CentOS etc). […]
From a non-tech graphics guy. If things are too hectic to answer the following, I understand.
Many thanks for a clearly presented tutorial.
I followed (I think) your instructions and the updates (in the replies) but permissions don’t seem to be working. After going through the tutorial, I restarted the computer and logged in as an admin user (not root). Checking Apple’s Activity Monitor under All Processes showed no sign of tomcat running. Logged in as root and had the same result.
Tried to manually start tomcatCtrl.pl as admin user (not root) from Terminal and got the following:
lw0000:~ username$ sudo /library/startupitems/tomcat/tomcatCtrl.pl start
-bash: sudo/library/startupitems/tomcat/tomcatCtrl.pl: No such file or directory
lw0000:~ username$ ls /library/startupitems/tomcat/
Tomcat tomcatCtrl.pl
Tomcat.StartupParameters.plist
Tried as root and the error was slightly different:
lw0000:/library/startupitems/tomcat root# tomcatCtrl.pl start
-sh: tomcatCtrl.pl: command not found
Checked the dirlist
lw0000:/library/startupitems/tomcat root# ls /library/startupitems/tomcat
.DS_Store Tomcat.StartupParameters.plist
Tomcat tomcatCtrl.pl
Don’t know how to set permissions via the command line. Here’s what my Finder File> Get Info looks like for the Tomcat folder and the files inside:
Owner: system
Access: Read & Write
Group: wheel
Access: Read only
Others: Read only
Do I need to set permissions in Terminal? While root? Which command is used to read and set permissions in Terminal? chmod? Do I paste the strings “drwxr-xr-x” and “-rw-r–r–” in the appropriate command or is something else required?
Thanks,
Michael
Hi Michael,
Firts off, I owe you an appology, I recently moved my blog to a new host and new blog software and in the process all my posts with code in them got totally messed up. I’ve been going through them one by one fixing them but I’m being rather slow about it and this one wasn’t done yet.
Anyhow, as you can see if you scroll up it has now been fixed. The chances are your problems were caused by copying and pasting messed up code. Can you just verify that your files all have the correct content etc before we go any further. If that doesn’t solve your problems then we’ll move on to plan B!
Bart.
Hi Bart,
Excellent tutorial. Thanks for posting this.
I have just one problem. I can do a:
tomcatup
and start up tomcat, but for some reason, it won’t startup with the OS on its own. Do you have any suggestions on debugging this?
Thanks,
-Jon
Hi Jon,
The first thing I’d check would be that the permissions on the startup item are all correct. Then I’d double check that the name of the startup script in the startup folder is identical to the name of the startup folder (e.g. /Library/StartupItems/Tomcat/Tomcat).
Then I’d try the following:
sudo su –
cd /
/Library/StartupItems/Tomcat/Tomcat
and see what happens.
Failing that I’d have a look at the logs in the console viewer (Applications->Utilities->Console).
Hope that helps,
Bart.
Bart,
Thank you very much for your posting of this information! It is by far the best I’ve been able to find (and I’ve search quite a few) –very clearly presented.
My installation is on a PowerPC G5 dual 2.0 GHz running OS X 10.4.8.
Despite that, I am stumped… I can’t get tomcat to run (via http://localhost:8080/) — I receive the following message: can’t establish a connection to the server at localhost:8080.
I have completed Steps 1-3, with no apparent errors, and then executed the startup command “/usr/local/tomcat/bin/startup.sh” while logged in as the root user.
I don’t believe it should make any difference, but my tomcat distribution is apache-tomcat-5.5.17. I made all the appropriate changes with that in mind.
I have checked and rechecked repeatedly all the items in Steps 1 thru 3, including system restarts along the way. I am now just stumped and would greatly appreciate any suggestions you might have.
(I have actually completed all the steps, but just can’t get tomcat to run… or run properly. My Java distribution is jdk-1_5_0_09-linux-i589.bin. I note that Apple has a Java15Release1.dmg — should I be using this? Does it matter that the default Java installation is also on the system?)
Thank you.
Ray
Hi Ray,
Thanks for your kind words.
OS X 10.4.8 has Java 5 so I’d advise you use that. In fact it has Java 5 as the default JVM so setting your JAVA_HOME environment variable to /Library/Java/Home in the startup item should do the trick.
I’d also recommend starting Tomcat using the startup item rather than by directly running the startup script because the startup item sets environment variables that won’t be set if you run startup.sh directly. To do that use:
sudo bash
cd /
/Library/StartupItems/Tomcat/Tomcat start
I have no idea why but if you don’t change to the root directly the startup item won’t work right.
If that doesn’t work have a look in the Tomcat log file at /user/local/tomcat/logs/catalina.out (assuming you followed my conventions).
Feel free to reply again if you have any more questions.
Bart.
Bart,
Changing the JAVA_HOME assignment appears to have taken care of the problem. Thanks!
Using my browser and going to localhost:8080 to verify that Tomcat was runging — I now have the Apache Tomcat administration page. I do not believe I missed any steps, but it appears that I do not have the username and password necessary to log into the Tomcat Administration and/or Tomcat Manager.
Did I miss something? If not, how do I set-up an administrator/manager user and password?
Ray
Hi Ray,
I never actually use the the administrator web-app. I’ve always manually set up my server and my webapps and just got used to doing it that way.
Anyhow, I think this page should give you all the details you need to get the manager up and running: http://tomcat.apache.org/tomcat-5.5-doc/manager-howto.html
Hope that helps,
Bart.
Bart,
That link did provide the nfo I needed — I can now access the Tomcat administrator web-app.
Thank you!
I’ve still much more to learn, but I am making baby-steps forward. I much appreciate your help and your web site & blog!
Bart,
As others have said before me… Thank you for this outstanding tutorial that allowed me to, at last, achieve a working installation of Tomcat on my 10.4.8 machine !
I’ve been looking for such thorough information on the, IMHO, elaborate operation of getting Tomcat up and running under OS X. It is so well done that I even decided to go immediately for the apache-tomcat-5.5.20, and it all worked out perfectly. Thanks to you !
The only “glitch” I am experiencing is that I defenitly couldn’t get Tomcat running as a non-root user. I spent a whole afternoon trying to figure out where things get blocked but my Linux/UNIX skills are still limited and, unfortunately, I can’t find the time right now to dig the problem. Moreover, since you explained things so well, I was able to have Tomcat running at startup without problem, but as root, which I can live with for the moment !
If I ever find the time to fix the problem, I’ll try to post here again with a description of my “experience”.
Thibaut.
Hi Bart,
Great tutorial!
I wanted an easy way to start and stop Tomcat without having it running all the time via the Startup-Item route, so I have a created tool for this.
Its freely available on my site http://www.activata.co.uk
Thanks
John
Hi John,
That looks like a very promising little App. It didn’t work for me on my machine but that’s probably because the App makes assumptions as to where it should find Tomcat and my install obviulsy didn’t quite fit the bill.
If you are looking for ways to enhance the app then a quick button to open the logs in the Consol App would also be a great feature.
Good Luck with this App,
Bart.
Thanks for this great information! This is just what I’ll need!
I haven’t tried it yet, but do you see any reason this wouldn’t work as outlined for Tomcat 6? (I am new to Tomcat, and just installed 6.0.10 and just today got it working!)
Also…what is the purpose of /Library/StartupItems/Tomcat/tomcatCtrl.pl as shown? is it required? or is it for convenience to allow easier startup/shutdown?
Thanks!
Hi Jeff,
I’m sorry to say I have no experience at all with Tomcat 6. However, these instructions were basically sound for Tomcat 4 so there’s a good chance they’ll work for 6.
As for tomcatCtrl.pl, that’s purely for convenience. I generally use these instructions on development machines so I’m constantly starting and stopping Tomcat.
I’d love to hear how you get on with these instruction on Tomcat 6.
Bart:
Thanks! I didn’t see anything that was version-specific for Tomcat, but I thought I should ask anyway.
I’ll be adding this in the next few days, so I’ll let you (and anyone reading!) know how it goes.
Thanks for the great info!
–Jeff
So I finally installed the service startupitem. I found 2 problems/issues with the instructions:
1) the command in the startupitems/tomcat/tomcat file needs to be:
sudo -u tomcat /usr/local/tomcat/bin/startup.sh
not
su tomcat – /usr/local/tomcat/bin/startup.sh
The latter gave me “incorrect permissions” error on startup (su requires that you enter the users password, even if you are root, sudo does not.
Also in the same file it should have a structure which is pretty standard for services, and also causes an automatic restart of the service if it crashes.
The one as shown will work fine, but I am suggesting this:
after the three export commands in place of:
if [ “${TOMCAT}” = “-YES-” ]; then
ConsoleMessage “Starting Tomcat”
su tomcat – /usr/local/tomcat/bin/startup.sh
fi
use:
StartService ()
{
if [ “${TOMCAT}” = “-YES-” ]; then
ConsoleMessage “Starting Tomcat”
sudo -u tomcat /usr/local/tomcat/bin/startup.sh
fi
}
StopService ()
{
ConsoleMessage “Stopping Tomcat”
sudo -u tomcat /library/usr/local/bin/shutdown.sh
}
RestartService ()
{
StopService
StartService
}
RunService “$1”
[…] I’ve done a few previous articles on Apache Tomcat (one for installing it on Linux and one for installing it on OS X), but I haven’t yet mentioned installing the JK Connector (mod_jk) in any environment. For those who are wondering what on earth I’m on about, mod_jk allows the Apache web server to serve your Tomcat web apps so they appear on port 80. There are a number of reasons why you might want to do this. Firstly, it provides a simple and secure way to get Tomcat to respond to requests on port 80 without having to have it run as root. Apache is more efficient at serving static pages so it can help increase the efficiency of your web app, and finally it allows you leverage all the power of Apache’s many features for your Java web app. […]
Thanks! That was a good, concise and helpful howto.
Hi
Firstly thanks for this tutorial it’s nice and easy to follow. I’m a bit of a novice and I get this issue once I’ve followed steps 1 – 3. I dont want to go any further until I’ve got the basics right.
The file startup.sh is for sure there and I dont understand what I’m doing wrong
Last login: Sat May 5 10:44:35 on ttyp1
Welcome to Darwin!
PowerMac-1:~ andrewthomas$ sudo bash
PowerMac-1:~ root# sudo /usr/local/tomcat/bin/startup.sh
sudo: /usr/local/tomcat/bin/startup.sh: command not found
PowerMac-1:~ root#
Thanks
Andy
Hi Andy,
If you install from the ZIP rather than the tar.gz uniz file permissions are lost. The chances are that startup.sh is missing the execute permission. The first thing I’d try would be to execute the following as root to give execute permission on all shell scripts in the bin folder:
chmod 755 /usr/local/tomcat/bin/*.sh
Thanks so much Bart
Worked like a treat!!!
Anyone that is having problems executing the startup.sh needs to use Bart’s comment (#21). It fixed my problem. It’s exactly what he said… I downloaded the ZIP file and was having permission issues. Thanks!!!
[…] http://www.bartbusschots.ie/blog/?p=201 […]