Saturday, September 15, 2012

Install Tomcat on your Mac

This post discusses how to install Apache Tomcat on your Mac.  The instructions are specifically for Tomcat 7.0.29 on Mac OS X 10.7.4 "Lion", but generally the steps will be the same for other versions.

Introduction
Apache Tomcat is an open-source web server and servlet container for deploying Java Servlets and JSPs.  Tomcat 7.0 requires Java 1.6 or later, so make sure you have it installed.  To check your current Java version, open a Terminal window and at the prompt type:
$ java -version
You should see something like the following:
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)
If you do not have Java, or have a version earlier than 1.6, you will want to visit the Apple Support page Java for OS X Lion Update 1.


Download
Installing Tomcat will be easy, but first you will need to download it.  There are several pre-built binaries and source code available from the Tomcat 7.x download page.  For Mac installation, download a tar.gz file from the "Core" group of "Binary Distributions".


Extract and Uncompress
The tar.gz archive will need to be extracted and uncompressed to be useful.  Very simply, you open your Downloads folder in Finder, and then double-click the downloaded file (apache-tomcat-7.0.29.tar.gz).  Finder will extract and uncompress the contents and place them in a similarly-named folder (apache-tomcat-7.0.29).


Move Unpacked Folder
Now that the archive is unpacked, let's move it out of the Downloads folder to a more standard location.  Typically that is /usr/local.  First, we want to make sure that directory exists, so open a Terminal window and at the prompt type:
$ sudo mkdir /usr/local
When prompted, enter your Mac password, and the directory will be created.  (If it already exists, you will see a warning.)  Next we will perform the move operation.  Again in the Terminal window, type:
$ sudo mv ~/Downloads/apache-tomcat-7.0.29 /usr/local
You need to use sudo because /usr/local is owned by root.


Make Executable
By design, the scripts in the unpacked archive will not be executable.  To fix this, type the following at the prompt in your Terminal window:
$ cd /usr/local
$ chmod +x apache-tomcat-7.0.29/bin/*.sh
Now we are ready to start the server and test!


Test Installation
To start Tomcat, type the following at the prompt in your Terminal window:
$ apache-tomcat-7.0.29/bin/startup.sh  
You will see something like the following output:
Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.29
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.29/temp
Using JRE_HOME:        /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.29/bin/tomcat-juli.jar
Then, point your browser to http://localhost:8080/.  You should see a page like this:
Yay!  You have finished installing Tomcat on your Mac.  To stop Tomcat, type the following at the prompt in your Terminal window:
$ apache-tomcat-7.0.29/bin/shutdown.sh
Again, you will see some output like the following: 
Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.29
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.29
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.29/temp
Using JRE_HOME:        /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.29/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.29/bin/tomcat-juli.jar
And when you reload the page at http://localhost:8080/, you will see an error page because the server has stopped running.


Symbolic Link
Finally, it may be useful to create a symbolic to the current Tomcat installation.  This is because, as we add new releases, it will be easier to remember a single path which always points to the current release.  At the prompt in your Terminal window, type:
$ sudo ln -s /usr/local/apache-tomcat-7.0.29 /Library/Tomcat
Now, to start the server, we do not need to remember it is version 7.0.29.  Instead, we can just refer to the symbolic link, thusly:
 $ /Library/Tomcat/bin/startup.sh
Similarly, to stop the server, we just need to do this:
$ /Library/Tomcat/bin/shutdown.sh 
When you install a new version, you will only need to change the symbolic link.