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.

httpd.conf

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.

username.conf

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:

#!/usr/bin/perl -wT
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.

It's a by-product of having known hunger intimately during my life. I can't stand seeing perfectly good food go to waste. (Go figure; now I'm in a wasteful business.)

Today, however, I couldn't have been more overjoyed by the act of throwing away most of a perfectly good chicken salad sandwich.

Because dad's chemotherapy compromises his liver's ability to produce bile, until today eating has not been an option for him for three weeks (just too painful 'cause it bloats him). The doctors have finally figured out a way to allow him to eat solid food without causing him serious gastric distress. Dad did not tell us this.

We were sitting around and a tap comes at the door and the lady with the tray comes in. They've been giving him a bland diet in small portions in hopes that he'll eat. He sips on his juice; that's about it. I was making way for the new tray when I came upon the source of my optimism. A plate was on dad's table that contained a sandwich. Cut into neat triangles, one was intact. The other had three bites taken from it; revealing the bland-looking sandwich filling within.

"Hey, did anyone eat your lunch?"

"No."

"Somebody ate this sandwich." I held up the plate.

"I ate a little of it. Oh — I didn't tell you; I can eat a little, now..." He told me how they'd finally figured out a way for him to eat without pain. This is the key to building up his strength, so his chemotherapy can resume.

Two weeks since he entered the hospital. Two weeks of thinking my father would die, wasting away from not eating, vanished in a split second.

The tray lady put the new tray down and reached out, trying to take from me the plate, a soiled napkin and a couple of empty juice containers.

"Show me where to put this, on your cart."

"No, that's okay, I'll take it."

"I insist."

She sheepishly showed me to the large cart that carries the trays. A plastic box at the bottom of the cart serves to hold dirty plates and refuse. There I deposited the garbage, and the plate with the partially-eaten sandwich. No pang of guilt here. I looked for a moment at the three bites dad had taken from the sandwich, and went back inside dad's room.

My voice was inappropriately loud as I spoke my next words. Tray lady gave me a look as though perhaps I ought to be hospitalized there; in a padded room.

I said, "That's the best sandwich I ever threw away in my life!"

An update on May 4, 2006:

After a day of deliberation, I tossed some clothes in a bag, called in some nonexistent personal days at my temp job here in Tempe, and hopped on the earliest eastbound plane to Michigan. By Friday afternoon, I was standing in a driveway in Clarkston, MI, right back where I started, still reeling from a distance that took 4 days when leaving and 4 hours coming back.

But it was the right call. My grandmother died that weekend, faster than even the doctors predicted but with tremendous dignity and grace.

The entire family was gathered together. Her husband of 62 years. Her 5 sons and daughters, and spouses, and grandkids. The lights were dimmed. The house was full of sad but stoic midwesterners, old photographs and flowers. The old clock above the sofa continued its loud tocking. No one was so dramatic as to stop the pendulum from swinging when she breathed her last breath. The world continued in its slow rotation.

Given her diagnosis, acute lymphoma turning into leukemia, I feel that this was actually the "best" that could've happened, both for her or for us. The other alternative was weeks of drawn-out suffering and round-the-clock vigils. My grandma was not the type that liked to keep people waiting. The last of a long line of cool-headed, kind-hearted Norwegian farmers, she lived her whole long life by the philosophy of "That's the way it goes. Life is pretty good. It could be worse... It could be worse..."

The service was held the following Tuesday. The town's Methodist pastor gave an eloquent and touching eulogy.

On Wednesday, I was back on a plane headed West...


So that's the news from Lake Wobegon, where all the women are strong, all the men are good-looking, and all the children are above-average. Meanwhile, I'm back in the blazing heat of Tempe, AZ, planning the same move I'd been planning before the rest of this came down.

Thanks to help from relatives, my brief return to Michigan only cost about $50, but my next few months are still as uncertain as ever. In 45 days, I'll be leaving this sunny valley by a westbound freeway.

I can't tell you any more about where I'll settle, because I don't know myself. I'm aiming towards Seattle by way of San Diego north through Oregon, if only to have a direction in which to wander.


To continue the switchboard metaphor, I've been living a very muted life these past 5 months in Arizona. Soon it'll be time to kick open some of the old circuits and crank up the volume again.

I'm still looking forward to another great summer.

Wow. I haven't noded anything since February. When last I bored you all to death with the ins and outs of my life, I was getting ready to move from San Jose, CA to Colorado Springs, CO. My wife and I made the move relatively unscathed; our savings account is a bit bruised, but applying for and taking out a mortgage will have that effect. I've figured out what a Satellite Systems Engineer (my new job) does. He sits through a bunch of training and occasionally gets to cobble together some code in C#. Eventually the courses will end, and my job will turn toward helping to launch and maintain a communcations satellite. Very cool, if a bit overwhelming at time.

On the home front, my wife and I have adopted a Pembroke Welsh Corgi puppy to go along with our two cats, Brutus and Cassius. Her name is Huxley, and we couldn't be happier with her. And in a strange turn of events, we made friends with the neighbors all because I lent Jenny one of my Anime Central staff shirts one weekend. They're really neat folks, like the same stuff we do (video games, board games, card games, Star Wars, etc.), and they're a blast to hang out with.

Also, I was trying to work with the Advanced Media Network to write reviews and post news and whatnot. Well, whomever is in charge of the Gadgets section has a hard time organizing things and delegating tasks, so I fired off an email to the proprietor of http://www.etoychest.org, and now I post news and write reviews for them. So far I've reviews Mark Ecko's Getting Up: Contents Under Pressure and Drakengard 2. Neither was very good, but I'm looking forward to my next assignment: both the PS2 and DS version of Over the Hedge (which also looks like a fun movie). If it hasn't been done by the time I review it, I'll be sure to write up something here about both versions, too.

I know I've been away from E2 for a little while, but I couldn't ever stay away forever. I've got a couple of nodes brewing in my head just waiting for when I have access to the Internet again from my desk (gotta be able to alt-tab away and I don't like to tie up the two public access computer). I can give you mini-synopses of each: Lego Star Wars: The Video Game: if you're looking for good co-op, this is a great game to pick up. New Super Mario Bros.: Mario is back, and it feels great to finally have him in a 2-D platformer again. If you own a DS, pick this one up.

Log in or registerto write something here or to contact authors.