Configuring CGI on Mac OS X Leopard (10.5.7)

This is a getting started guide to running CGI C and Perl scripts on Mac OS X 10.5.7. I googled around to find several pages useful to get this working. The hardest part for me was understanding the architecture because most pages are concerned with getting it done rather than understanding the pieces. I like to understand the pieces and I did manage to dig around enough to get a basic understanding.

Starting and Stopping Apache Server

Mac OS X comes with the Apache server already installed and you can use a GUI to manage it. To turn the Apache server on and off, go to System Settings -> Sharing and click the box next to Web Sharing to toggle the server on and off. You can also use the command sudo apachectl restart to restart the Apache Server. sudo is a command that gets you to superuser mode - it should ask you for your password on your first use.

Configure CGI

cd /private/etc/apache2 and edit httpd.conf using an editor. You will probably need to put a sudo in front of the edit command. Note that this approach edits the system-level files and not the user-level files. To edit the user level files, edit the file for your username in /private/etc/apache2/users appropriately. Note that I haven't tried this but I assume that some hacking around will get it to work. I'm happy with system-level access for my purposes.

Search for <Directory "/Library/Web and change the Options line to Options Indexes ExecCGI. The ExecCGI is needed but I'm not sure if the Indexes piece is needed. At any rate, it works. One of the webpages recommended Indexes ExecCGI +FollowSymlinks AddHandler cgi-script .py but I didn't try the stuff after ExecCGI.

Save the file and restart the Apache Server.

Trying It Out

Create an executable from a C or C++ program and put it in the directory /Library/WebServer/CGI-Executables with execute permissions set (777 worked for me but that may be too generous). Alternately use a perl program. You can get sample C and Perl CGI programs at Sample CGI Programs.

Restart the Apache Server and then go to your browser and enter http://localhost/cgi-bin/<ProgramName> and it should pipe the output to your browser.

Useful links



Updated: June 22, 2009