Getting CGI scripts to work on Mac OS X's built-in web server
One of the countless nice things about Mac OS X is that it comes with a whole bunch of Unix stuff free because of the Unix base it's been rebuilt on. If you know how to use Unix, you can do all kinds of stuff with it.
I don't know how to use Unix very well.
Fortunately I've got a few friends who do. I still hit snags though when I'm doing something they've never done before. Case in point, I've been learning Perl lately. I've been doing a lot of site maintenance on E2 with automated scripts I've written, most notably filling in the gaps where the original Webster 1913 autonoder missed a few spots (huge thanks to mauler for his help with this). Getting Perl running on the Mac isn't a huge deal, since OS X comes with Perl pre-installed. The snag I hit was when I tried to start testing CGI scripts on my local machine.
I've done some CGI already, enough to get cookies working and build a simple content management system for web sites. But uploading to my friend's server, changing the permissions, bug testing, wash, rinse, repeat is pretty slow and tedious compared to doing it right on my own hard drive. So I really wanted to get this running.
Getting the Apache web server running is a matter of clicking "Personal Web Sharing" in System Preferences. Getting CGI enabled was a little trickier.
There is a file at /etc/httpd/httpd.conf where you can configure some details for the Apache web server. Several web sites will be more than happy to tell you what to change in this file in order to get CGI running. What they won't tell you is the second file you need to edit, /private/etc/httpd/users/*.conf, where * is your login name under OS X. The location of this file is found all the way at the bottom of httpd.conf.
Without further ado, this is what you need to do to get CGI scripts running on Mac OS X's built-in Apache server. First open the terminal (Applications/Utilities/Terminal) and type sudo pico /etc/httpd/httpd.conf to edit the configuration file. This file is owned by root user, so the sudo command gives you temporary root access, after asking for the root password. pico invokes the Unix Pico editor, a command-line text editor. The important commands are listed across the bottom. Control-W is the "find" command, which you will be using a lot because this file is really big. Control-O saves, and Control-X exits.
The first two edits listed blow are adding something to the line. The last three edits are simply removing the # from the beginning of the line. The # turns the line into a "comment", or an ignored instruction.
Options Indexes FollowSymLinks MultiViews → Options Indexes FollowSymLinks MultiViews ExecCGI Includes
DirectoryIndex index.html → DirectoryIndex index.html index.shtml
#Addhandler cgi-script .cgi → Addhandler cgi-script .cgi
#AddType text/html .shtml → AddType text/html .shtml
#AddHandler server-parsed .shtml → AddHandler server-parsed .shtml
Almost done! All we have to do to finish is sudo pico /private/etc/httpd/users/*.conf and just add one thing.
Options Indexes MultiViews → Options Indexes MultiViews ExecCGI Includes
And we're done! Just write a simple "Hello World!" program and put it in your Sites directory to test it. Here's a sample:
print "Content-type: text/html\n\n";
print "<h2>Hello, World!</h2>\n";
It took me a while to find this out. When I tried running a CGI script after making only the first file's changes, I was given a FORBIDDEN error message on my web browser. Turning to the Console (Applications/Utilities/Console), I was able to view the Logs, in this case /var/log/ → httpd → error_log. The error message was Options ExecCGI is off in this directory. Looking up this error message in Google pulled up some web pages that told me about the second file I had to edit.