2010-10-06

Running Apache Tomcat on your Mac

  

Apache Tomcat is Apache's other web server. Also runs Java Servlets and JSPs. I prefer running Tomcat as a web server much better that Apache's more famous (and finicky) "httpd".

Tomcat is pure Java, so it runs very well on your Mac OS X computer. Apple builds Java into every Mac.

  • The current version 6 runs on many older generations of Macs as well as new ones. 
  • The still-beta version 7 will run only on Java 6, which requires an Intel Mac running Snow Leopard.
There is one big catch to running Tomcat on your Mac: By default, the permissions are set incorrectly on the downloaded files. When you try launching Tomcat you will get errors, complaining that you do not have permission to complete certain operations. The fix is so easy: just a drag-and-drop and a few mouse clicks. Here's how.

Download the free app (donations accepted) BatChmod. It has the best icon, by the way. This app is a very convenient gui wrapper around an otherwise very difficult arcane set of command-line instructions. You will be entrusting your system password to them, and do so at your own risk. They've been in business for years with no issues I've heard of. Tip: To detect any software phoning home without your permission, get the Mac app Little Snitch.

Download Tomcat. Version 6.0.29 is current nowadays. Grab the zipped one, as the tarred files may not work with Mac OS X' tar tools. You'll end up with a folder named something like "apache-tomcat-6.0.29". If you’re paranoid, hit the MD5 link, read the number, then drag and drop the downloaded zip file to a Terminal window after typing "md5 ", and compare your number to theirs. If no difference, you downloaded without corruption.

You can place the Tomcat folder anywhere you like. I avoid nesting too many folders because extremely long path names can cause problems. I suggest the top level of your home folder.
  1. Drag and drop your unzipped Tomcat folder to the BadChmod app. 
  2. Check all the checkboxes except "Clear xattrs".
  3. Click Apply.
  4. Enter your system password.
  5. Quit.

CAVEAT: I am not expert at these Unix permissions and related issues. Do all this at your own risk, and after your own research. I'm just sharing what works for me, but taking no responsibility for you.

This problem of Unix file permissions often occurs on downloaded files, but the effects are more obvious in Tomcat. I usually run BatChmod on anything I download, unless packaged as a .DMG file. 

Now you're ready to launch Tomcat.
  1. Open a window in Terminal app, found in Applications > Utilities folder.
  2. In the Finder, locate inside the "bin" folder inside the Tomcat folder.
  3. In "bin" folder, locate the file named "startup.sh".
    (Ignore the .bat files; those are for MS Windows)
  4. Drag and drop that "startup.sh" to your Terminal window.
  5. Press Return to run that shell script.
    (".sh" means "shell")
  6. See several lines of text appear in the Terminal window.
    (If you see about 5 lines of "Using " this-or-that, you are in good shape.)
Now test your web server.
  1. Launch a web browser, such as Safari, Camino, Firefox, Opera, Chrome, etc.
  2. In the URL address bar, type:
    localhost:8080
    Or if that seems to fail, type:
    127.0.0.1:8080
  3. Press Return to make the web browser hit your web site.
    If successful, you should see a web page displayed with the cute tiger Tomcat logo and the Apache feather logo.
To get rid of the need to add the ":8080" see my other blog post on port-forwarding.

To shutdown the web server, in that same Terminal window, drag and drop the "shutdown.sh" file. Press Return to execute, of course.

Tip: Press the Up-Arrow key to re-enter previously used lines in that Terminal window. Press Return to execute one of them. This is a handy way to easily repeatedly start and stop Tomcat.

If you got this far, lastly you replace Tomcat's default web site with your own. Replace the contents of:
Tomcat folder > webapps > ROOT

Note that we did not do any configuration settings in Tomcat whatsoever. I like that about Tomcat: For a single web site, Tomcat is ready to go out-of-the-box. You can do much more configuration if you wish as Tomcat is relatively flexible and full-featured as web servers go. You can change the port number. You can turn on logging to record who hits your web site and when. You can enable virtual hosting to serve multiple web sites. You can set up security restrictions controlling access to portions of your web site.

In passing, I'll mention another popular Java-based alternative to Tomcat: Jetty. If you want a bunch of additional functionality, beyond just web serving and Java servlets, for a big application server consider Glassfish. But for me and most folks I know, Tomcat or Jetty are the way to go.

Hope that works for you. Happy serving!

1 comment:

  1. Thanks for review, it was excellent and very informative.
    thank you :)

    ReplyDelete