Tomcat For Mac Os X



I observed that there is no full guide for installing tomcat on OS X that will include setting it up to start at boot time. Here is the quick guide: install macports. Suport port upgrade; sudo port upgrade outdated. Sudo port install tomcat6, or if you want other version check port list grep tomcat; config is now at: /opt/local/share/java. Install Maven on Mac. Maven is the most widely used build and project dependency management tool for Java-based applications. We can install Maven on Mac OS using a package manager such as HomeBrew or through XCode Command Line Tools. But, in this tutorial, we will learn how to install Maven on Mac OS without using any other software. For more information concerning Tomcat Native, see the Tomcat Native site. Source (please choose the correct format for your platform) Native 1.2.25 Source Release tar.gz (e.g. Unix, Linux, Mac OS).

Apache Tomcat is a flexible, powerful, and widely popular application server and servlet container, which the Apache Software Foundation has developed since 1999, first under the Jakarta project, and now as its own top-level project. Users value Tomcat for its fast start-up, reliable, mature, and fully open codebase, and highly extensible and configurable functionality.

Apache Tomcat 6 has the ability to run either as a stand alone web server, or as a pure servlet container or cluster of containers behind an HTTP web server proxy or load balancer. This makes Tomcat a favorite choice for a broad spectrum of different scenarios, from small, one-server websites to large-scale, high-availability enterprise production environments. Tomcat 6 implements the Servlet 2.5 and JSP 2.1 specifications, and includes many new features, such as a new NIO Connector, injectable thread pools, improved logging, and more.

In this article, we'll learn how to install and configure Tomcat 6, the most recent stable Tomcat branch as of May 2010, on Linux, Windows, and Mac OS X machines.

Tip: Installing Tomcat as a Windows Service can be difficult. Tcat Server, the enterprise Tomcat application server, makes it simple! Click here to download Tcat Server today!

Tomcat 6 installation methods - Packages, installers and binaries

Before we go over the installation instructions, let's take a brief look at the different methods by which Tomcat can be installed on each platform, and the advantages and disadvantages of choosing one method over another.

On a Mac OS X machine - Binary distribution

For Mac users, there is only one way to install Tomcat 6 - by downloading the latest binary from an Apache mirror, and following the instructions we've outlined below.

In fact, if you're a Mac user, you may want to click here right now to skip the following about packaged distributions as opposed to binary distributions, and get right down to installing Tomcat.

On a Windows Machine - Binary distribution or package installer

Windows users can either download the 32 or 64-bit binary distribution and install Tomcat manually, or use the Windows installer, which in its latest version includes both 32 and 64-bit distributions, and attempts to select the appropriate distribution based on the JDK you have installed.

Either of these methods is acceptable, but we recommend you use the installer. It's well-designed and maintained, and automatically takes care of some annoying Windows-specific installation issues, such as enabling Tomcat as a Windows service and setting registry values related to the JVM.

For our step-by-step guide to installing Tomcat on a Windows machine, click here.

On a Linux machine - Binary Distribution, bundled distribution or repository package

Linux users can either download and install the binary distribution of Tomcat from an Apache mirror, use one of many available platform-specific repackaged Tomcat distributions, which are provided by various Linux vendors and usually incorporate some pre-configured settings to help Tomcat run on the chosen platform, or install from a general Linux repository package, such as JPackage or RPM.

While some of these packages are well maintained, others include outdated versions of Tomcat, non-documented modifications, or are not quickly updated with new patches released by the Tomcat team. (A notable exception is the Debian/Ubuntu Tomcat package, which is maintained with the help of contributions from the MuleSoft development team. For more information, click here to read 'A Better Tomcat For Ubuntu', a blog post about the partnership.)

Although installing from a package has the benefit of being very simple, usually consisting of no more than two or three commands, we recommend that you take the time to install and configure Tomcat yourself from the binary distribution. Completing the initial install and configuration process by hand is a great introduction to Tomcat's internal settings, and you'll never have to wonder if your package has some odd modification or is missing a vital security patch.

Click here to jump to our simple, step-by-step instructions for installing the Tomcat binary distribution on a Linux machine.

Installing Tomcat 6 on Mac OS X

The Mac OS X installation process is fairly painless and straightforward, but there are a few rough spots along the way. Follow these step by step instructions to get Tomcat up and running on your Mac OS X machine in no time.

Step 1: Configure environment variables

Tomcat finds certain resources, such as your Java runtimes, by reading values from your environment variables. On Mac OS X, the variable that Tomcat is looking for is 'JAVA_HOME', which points it to your JDK for compilation purposes.

If you are new to the Mac platform, and you are wondering why there's been no mention of installing Java, don't worry. Apple provides a special OS X version of Java, which is automatically installed and updated via Software Update.

Tomcat For Mac Os X 10.6

Additionally, you should set the 'CATALINA_HOME' variable, which points to the Tomcat home directory, and can be used during Tomcat configuration in lieu of a complicated directory path that might change.

To set the JAVA_HOME variable, open a new Terminal window and use the following command to open the system profile for editing. (You can substitute your favorite text editor. We like Vim.):

Once you've opened the profile, add the following lines to set the JAVA_HOME and CATALINA_HOME variables:

export JAVA_HOME=/Library/Java/Home export CATALINA_HOME=/Path/To/Tomcat/Home

(Note: If you're not familiar with Vim, here's a useful list of editing commands.)

Step 2: Download and install Tomcat binaries

Next, download the most recent stable build of Tomcat from an Apache project mirror site. If you're worried about checksum, be sure to run a checksum on the package with the 'md5' Terminal command and match it against the value provided on the Apache site.

Next, simply unzip the binaries into a simple location, such as /Library. By default, the unpacked folder name will be something like 'apache-tomcat-x-x-xx'. For ease of use, change it to 'Tomcat'.

Step 3: Start Tomcat

You should now be able to start Tomcat by navigating to the 'bin' folder and executing 'startup.sh' from the command line. After running the script, test to see if Tomcat has been successfully installed by visiting http:/localhost:8080 on your machine. If you followed the installation steps correctly, your browser should display the Tomcat Welcome Page.

Note: If you receive a permissions error, use the follow command to allow access to all of the Tomcat shell scripts:

Step 4: Automatic start on boot

You can configure Tomcat to start up automatically when your system restarts by using a custom script in conjunction with Mac OS X's launchd.

For more information about starting Tomcat automatically at login, please visit our guide to Tomcat Start.

Installing Tomcat on Linux

Installing Tomcat on your Linux machine is not all that different from the Mac OS X installation process. The primary difference between the two sets of instructions we provide here is that we cover installing Java on a Linux machine, as it is not automatically installed by the operating system.

Step 1: Download and install Java

You'll need to download the most recent Java runtimes in order to run Tomcat 6, which only supports Java 5 and later. You can either obtain them with the package manager of your choice, or download them as a self extracting binary from Sun's website.

Before you download anything, you can use the following command to find out if Java has already been installed on your system:

Tomcat For Mac Os X

If you have already installed Java, this will return a list of Java packages available on your system. If no results are returned, you'll need to install Java. After you have downloaded the latest Java package from the Sun website, use the following commands to complete your installation:

These commands make the package executable, and then install the package in a directory that makes it available to all users. This requires root access - if you don't have it, you can install Java in an alternative directory of your choice.

Step 2: Download and install Tomcat

Next, download the latest stable build of Tomcat from an Apache mirror. You can either use a web browser or download the release from the command line with the following command, with appropriate values for the [placeholders]:

Verify the checksum of the package after downloading it, using the 'md5sum' command to obtain the sum and comparing it to the checksum provided on the Apache website.

Next, extract the package, and move it to the desired folder:

Step 3 - Set Environmental variables

If you haven't already, you should now set the JAVA_HOME and CATALINA_HOME environmental variables. Tomcat uses JAVA_HOME to locate your Java runtimes, and CATALINA_HOME can be used in configuration files and scripts in place of a complicated file path. Under Linux, these environmental variables are set in '.bashrc'. Open the file:

You'll need to log out of bash and log back in for your additional variables to take effect.

Step 4 - Start Tomcat

You should now be able to run the Tomcat server by executing the 'startup.sh' shell script. If you want Tomcat to run automatically at start-up, you can use a custom init script to call startup.sh. You can use this technique in conjunction with some additional configuration to run Tomcat as a system-owned process for increased security.

For more information, visit Tomcat Start, our helpful guide to automatic start.

Installing Tomcat on Windows

As noted above, the easiest way to install Tomcat on a Windows machine is by using the Windows Installer. If you will be using this method, you should not need much more help; the installer will guide you through the whole process.

Step 1: Download and install Java

Installing Java on Windows is a simple process. Like the Tomcat Installer, The Java JDK and JRE Installers for Windows are robust, and can be downloaded from the Sun website. Download and follow the Installation Wizard to install Java on your machine.

Step 2: Download and install Tomcat

If you are only planning on running Tomcat as a service, and won't need to use the start up scripts, all you'll need is the Windows Service package. Otherwise, use the standard package. You can find both on the Apache project site. The Tomcat 6 Windows Installer is solidly built, and you shouldn't have any trouble.

If you will be using this install of Tomcat only for local development, you may want to consider changing the default port during the installation process from 8080 to 80, the default web port. This will allow you to access Tomcat simply by visiting http:/localhost/, without any additional port number.

For more information about running Tomcat as a Windows Service, please visit our Tomcat Service guide.

Having to use Tomcat for an important project I am working on, I installed it on my Mac. However, in 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!

Updated

  • Friday, January 30, 2009: added instructions to create an admin user for Tomcat; corrected and enhanced instructions for permissions; added comments on deploying applications which need access to the filesystem; added link to Lambda Probe; corrected the StartupItem files; many thanks to Derk Norton for suggesting some of those corrections.
  • Saturday, September 4, 2010: replaced instructions about setting up Tomcat as a StartupItem by instructions to set it up as a launchd job; added a section about Tomcat 7.

This tutorial is based on my experience installing Tomcat 6.0.29 on a Mac OS X Snow Leopard 10.6.4 system, and I have not tested those instructions with other versions of the operating system.

Downloading and uncompressing 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/username/Downloads/). Open a Terminal and execute the following commands:

sudomkdir-p/usr/local
cd/usr/local
sudotar xvzf ~/Downloads/apache-tomcat-6.0.29.tar.gz
sudoln-s apache-tomcat-6.0.29 tomcat

You also need to create an admin user for Tomcat. I will assume you are familiar with vim, but you can use any other text editor. Let’s edit the right file:

Then replace its contents by the following:

/usr/local/tomcat/conf/tomcat-users.xml

<?xmlversion='1.0'encoding='utf-8'?>
<tomcat-users>
<rolerolename='manager'/>
<rolerolename='admin'/>
<userusername='admin'password='pass'roles='admin,manager'/>
</tomcat>

Of course, you can replace the word pass by any password you want to use.

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 unprivileged user as well as its primary group.

Creating an unprivileged 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 tutorial, I will be using the number 107. We can create the group and the user with the following commands:

sudo dscl . -create/Groups/_tomcat PrimaryGroupID 107
sudo dscl . -create/Groups/_tomcat RealName 'Tomcat Users'
sudo dscl . -create/Groups/_tomcat Password *
sudo dscl . -create/Users/_tomcat UniqueID 107
sudo dscl . -create/Users/_tomcat PrimaryGroupID 107
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.

Tomcat For Mac Os X64

Setting permissions

Since Tomcat will be run as the _tomcat user, you must change the ownership of some directories and files. Just run the following commands:

cd/usr/local/tomcat
sudochown-R root:wheel .
sudochmod644 conf/*
sudochown root:_tomcat conf/tomcat-users.xml
sudochmod640 conf/tomcat-users.xml
sudomkdir conf/Catalina
sudochown _tomcat:_tomcat conf/Catalina
sudochown _tomcat:admin logs temp webapps work
sudochmod2770 logs temp webapps work
cd -

Tomcat For Mac Os X 10.12

This way, Tomcat should have the right permissions for all the directories it needs to write into, and at the same time, you won’t need to use root privileges to read the logs, and so on.

But be careful when developing an application which needs to have access to some directories in the filesystem (for storing files inside the web application for example): you might need to give Tomcat specific permissions on those directories for this to work.

However, I think that a good way to avoid a lot of Unix permissions headaches is to deploy your web applications only using tools such as the Tomcat Manager, or the excellent Lambda Probe.

Setting Tomcat as a launchd job

Tomcat For Mac Os X

If we want to have Tomcat start up automatically at boot time, or if we simply want to easily start it and stop it without having to think about details like paths and permissions, it’s a good idea to use it as a launchd job.

The first big thing we need to do for that is to create a wrapper script that will start Tomcat and not return until the Tomcat process exits, which is not what the startup.sh and catalina.sh scripts do, as they launch Tomcat as a background process.

To do that, let’s first create a new shell script in Tomcat’s bin directory:

sudotouch/usr/local/tomcat/bin/tomcat-launchd.sh
sudochmod +x /usr/local/tomcat/bin/tomcat-launchd.sh
sudovim/usr/local/tomcat/bin/tomcat-launchd.sh

Then put the following content:

/usr/local/tomcat/bin/tomcat-launchd.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /bin/sh
# tomcat-launchd.sh
#
# Wrapper script that starts Tomcat and waits for the Tomcat process
# to exit. This is needed for proper interaction with launchd.
#---------------------------------------------------------
# Helper functions
#---------------------------------------------------------
# NOTE: We are inheriting CATALINA_HOME from launchd, because its value
# was defined in the launchd plist configuration file.
function shutdown(){
# Bye Tomcat!
echo'Shutting down Tomcat... '
$CATALINA_HOME/bin/catalina.sh stop
echo'done.'
# Cleaning up the temporary file
rm-f$CATALINA_PID
}
function startup(){
# Define the file where we want the Tomcat process ID to be stored.
exportCATALINA_PID=$(mktemp/tmp/`basename-s .sh $0`.XXXXXX)
if[$?-ne0]
then
echo'$0: Failed to create temporary file. Aborting.'
exit1
fi
rm-f$CATALINA_PID
# Let's go!
echo'Starting up Tomcat... '
. $CATALINA_HOME/bin/catalina.sh start
# Register the shutdown function as callback to execute when a signal
# is sent to this process.
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP
echo'done.'
}
function wait_for_tomcat_to_exit(){
echo'Waiting for Tomcat to exit (PID: `cat $CATALINA_PID`)... '
wait`cat$CATALINA_PID`
echo'done waiting for Tomcat to exit.'
}
#---------------------------------------------------------
# Let's go
#---------------------------------------------------------
startup
wait_for_tomcat_to_exit

Then, we need to create a configuration plist file for the Tomcat launchd job:

Tomcat for mac os x el capitan
sudovim/Library/LaunchDaemons/org.apache.tomcat.plist

Then put the following content:

/Library/LaunchDaemons/org.apache.tomcat.plist

Mac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xmlversion='1.0'encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC '-//Apple Computer//DTD PLIST 1.0//EN'
'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>
<plistversion='1.0'>
<dict>
<key>Label</key>
<string>org.apache.tomcat</string>
<key>ServiceDescription</key>
<string>Tomcat Servlet/JSP Server</string>
<key>UserName</key>
<string>_tomcat</string>
<key>GroupName</key>
<string>_tomcat</string>
<key>EnvironmentVariables</key>
<dict>
<key>CATALINA_HOME</key>
<string>/usr/local/tomcat</string>
<key>JAVA_HOME</key>
<string>/System/Library/Frameworks/JavaVM.framework/Home</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>/usr/local/tomcat/bin/tomcat-launchd.sh</string>
</array>
<key>StandardOutPath</key>
<string>/usr/local/tomcat/logs/launchd-stdout.log</string>
<key>StandardErrorPath</key>
<string>/usr/local/tomcat/logs/launchd-stderr.log</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>

Using launchd to manage Tomcat

The first thing we need to do is load this configuration file:

sudo launchctl load /Library/LaunchDaemons/org.apache.tomcat.plist

Tomcat should be launching as the user _tomcat, and if you go to http://localhost:8080/ using your favorite browser, you should get to the homepage of your new Tomcat server.

With the above launchd configuration file, Tomcat is pretty much guaranteed to always be running on your system: it will be restarted automatically for you, by launchd, even if it exits for any reason (for example, a crash, or a reboot). The key that controls the fact that Tomcat is launched at boot time is actually the key named RunAtLoad. So, if you don’t like this behavior, you can always set that key to false, and reload the configuration:

sudo launchctl unload /Library/LaunchDaemons/org.apache.tomcat.plist
sudo launchctl load /Library/LaunchDaemons/org.apache.tomcat.plist

You should run those two commands to reload the configuration anytime you make a change to the plist file.

Now, let’s say that you want to just restart the Tomcat server (maybe because you changed some of Tomcat’s configuration files). With the above launchd configuration file, all you have to do is this:

Indeed, you don’t even need to do an equivalent start call, because launchd will take care of relaunching the Tomcat server. That is because we set the key KeepAlive to true. If you prefer to be able to really shut down Tomcat, then you can just change that key to false, reload the launchd configuration, and then, to achieve the same result, you’ll have to run two commands now:

sudo launchctl stop org.apache.tomcat
sudo launchctl start org.apache.tomcat

Of course, if you omit the start command, then the server will be permanently shut down (until the next reboot, if you still have the key RunAtLoad set to true).

You can customize the behavior of launchd with regard to Tomcat even further, but I suggest you just check out launchd plists’s man page if you want to learn more about that.

Update for Tomcat 7

Tomcat 7 is around the corner. It’s still considered beta software at the moment (at time of writing, the latest released version of Tomcat is 7.0.2), but it seems to work pretty well. I tried to see if those instructions would work with Tomcat 7, and apparently, they do, except for one thing. The content you need to put in the tomcat-users.xml file is a little different:

/usr/local/tomcat/conf/tomcat-users.xml

<?xmlversion='1.0'encoding='utf-8'?>
<tomcat-users>
<rolerolename='manager-gui'/>
<rolerolename='admin'/>
<userusername='admin'password='pass'roles='admin,manager-gui'/>
</tomcat>

The difference is that I had to replace the role manager by manager-gui. Tomcat’s migration page explains that difference very well.

Other than that, you should be able to install Tomcat 7 on your Mac using those instructions.

Enjoy!