Running Tomcat on Port 80 on a Mac — shell script

Posted by & filed under , .

If you’ve been messing about with Tomcat on a Mac OS X, you probably came across the problem of not being able to run the damn Tomcat on port 80. This to many won’t come as a problem, as in most cases port 8080 works fine for local development/testing. However, I found out that this becomes a bit annoying when you are doing some hosts-file hacking to redirect live browser requests to a local instance of Tomcat (for testing purposes) — as the requests would actually end up on port 80, while your Tomcat is running on 8080. So I finally got off my lazy arse to cobble together this script to redirect port 80 to whatever port your Tomcat is running on.

Granted this is not something new — just a bit tidy if you want — so even though you might have solved the above problem already, this post might give you a nicer/tidier script to do this.

It’s just a simple case of using ipfw and wrapping it up in a nicer shell script so one can do script.sh start or script.sh stop — which might come to the Linux-y users more natural (as per each service in /etc/init.d/). To use it, simply adapt the port numbers in the script to match the one you run Tomcat on. Simply change TOMCAT_PORT to whatever port you need (it’s set in the script to 8080 since that’s the default setup I believe for Tomcat).

In terms of invocation, the script supports 2 parameters: start to redirect the traffic from port 8080 to 80, and stop to stop the redirection. Any other parameter (or lack of parameters) will simply dump on the console the current ipfw list.

If you’re familiar with ipfw, you will know it needs root access — as such the script invokes ipfw via sudo, so if you’re not in the sudoers passwordless list, you will be prompted for your root password.

The rest is pretty straight-forward, so here’s the script — if you just want to quickly inspect the ipfw syntax or if you want, you can download the full script (link below):

#!/bin/bash
 
TOMCAT_PORT=8080
 
case $1 in
	start)
		echo -n "Redirecting port 80 to 8080..."
		sudo ipfw add 100 fwd 127.0.0.1,$TOMCAT_PORT tcp from any to any 80 in
		echo "done."
		;;
 
	stop)
		echo -n "Stopping redirection from port 80 to 8080..."
		sudo ipfw del 100
		echo "done."
		;;
 
	*)
		sudo ipfw list
		;;
esac

And as promised, here’s the source for download.

4 Responses to “Running Tomcat on Port 80 on a Mac — shell script”

  1. Liv
  2. bluishoul

    Thanks

  3. Siyuan

    Thanks! It help a lot!

  4. Anonymous

    No longer works on latest macos