Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

PB ONLY - Enabling CGI support in Apache Apps
The Apache web server includes the ability to process CGI (Common Gateway Interface) scripts. By default, this functionality is disabled and needs to be manually enabled in order to function. Read the rest of this article for step-by-step instructions on how to enable CGI's (enabling SSI (Server Side Includes) is also covered. SSI is a way of using environment variables to return some variable information on a web page.

Most CGI's are written in Perl, but you can also find a few in C, C++, and some of the UNIX shells (sh, ksh, csh, etc.). The CGI Resource Index (see below) also lists six CGI's written in Applescript!

Once you've enabled Apache, you'll probably want some scripts to run. Here are a few of my favorite sources:Read the rest for the step-by-step on how to enable CGI support in Apache...

The following instructions assume that you have enabled the web server already, and have it up and running in at least its basic state. Once that's done, here's how you can enable SSI and CGI on your machine.
  1. Open a terminal session, and type:
    su [become root; enter your root password]
    apachectl stop [stops the web server]
  2. Change to the web server configuration directory, and see what's there
    cd /Library/WebServer/Configuration
    ls apache.con*
    This should list at least one file, apache.conf. It will hopefully also list apache.conf.bak, but if it doesn't, then type this:
    cp apache.conf apache.conf.bak
    This will create a backup of your configuration file in case you make a mistake.

  3. Edit the configuration file in your favorite editor; the example shows vi, but pico or emacs will work just as well.
    vi apache.conf
  4. Move down to (roughly) line 336; the section we're looking for looks like this:
    # This may also be "None", "All", or any combination of "Indexes",
    # "Includes", "FollowSymLinks", "ExecCGI", or "MultiViews".
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    Options Indexes FollowSymLinks MultiViews
    You need to change the last line to read:
         Options Indexes FollowSymLinks MultiViews ExecCGI +Includes
    The ExecCGI turns on the CGI option; the +Includes enables SSI. When I tried without the "+" sign, my SSI calls didn't work.

  5. Around line 431, you'll want to edit the default file types that Apache looks for as index files, since SSI files are traditionally named ".shtml". The section you're looking for looks like this:
    # DirectoryIndex: Name of the file or files to use as a pre-written HTML
    # directory index. Separate multiple entries with spaces.

    DirectoryIndex index.html
    Make this line read:
         DirectoryIndex index.html index.htm index.shtml
    I added "index.htm" as this is also a fairly common index file name, especially for Windows-sourced files.

  6. Finally, find this section around line 800:
    # To use server-parsed HTML files
    #AddType text/html .shtml
    #AddHandler server-parsed .shtml
    Uncomment (remove the '#') the AddTypeand AddHandler lines to enable SSI processing.

  7. Save your changes, quit the editor, and then restart the web server:
    apachectl start
  8. Next, we need to make sure the test CGI file is properly permissioned. Enter the following into your terminal session:
    cd /Library/WebServer/CGI-Executables/
    chmod 755 test-cgi
    This will give everyone (important for the web user!) the ability to execute the cgi script.

  9. Disconnect as root and end your terminal session by typing exit twice.
To test your CGI functionality, you can use the test CGI that's included with Apache. Launch a web browser, and enter this URL:

If CGI has been enabled correctly, you should see something like this:
CGI/1.0 test script report:

argc is 0. argv is .

SERVER_SOFTWARE = Apache/1.3.12 (Darwin)
SERVER_NAME = griffx
... etc etc etc.
That's it! Now you can download and install CGI's that add things like bulletin boards, shopping carts, counters, ad managers, etc., to your webserver. Just be careful you do not get any Mac-style line breaks while downloading and installing the files - they will not work if this happens! See this article for all the details!

Installing CGI's is not overly difficult (you should be comfortable with the terminal and understand the basics of UNIX file permissions before starting). Be aware that there may be some malicious CGI scripts on the web, so make sure you trust the source before you install a third-party CGI!
  • Currently 1.78 / 5
  You rated: 5 / 5 (9 votes cast)

PB ONLY - Enabling CGI support in Apache | 12 comments | Create New Account
Click here to return to the 'PB ONLY - Enabling CGI support in Apache' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Problems with CGI
Authored by: Anonymous on Mar 16, '01 09:01:12PM

Hi. I configured Apache and got it to use the default cgi scripts in the CGI-Executables directory. However, when I created some very simple perl scripts, made the executable and tried to access them from the browser, I got the following error message in the Apple Error Log:
Premature end of script headers:
followed by the path name.
Any idea what is wrong?

[ Reply to This | # ]
Problems with CGI
Authored by: robg on Mar 16, '01 10:22:18PM

If it's short enough, can you post it here? Or compare it to the test CGI script and see if you notice any obvious differences? How did you create the scripts? In vi, or using a GUI-based editor? Have you checked for Mac line breaks vs. UNIX line breaks?

[ Reply to This | # ]
Problems with CGI
Authored by: Anonymous on Mar 19, '01 09:22:35PM

It's not the cgi script. On my W2K server, to get cgi scripts to work, I put them in a directory which I create called cgi-bin in the site root directory. Then I just type: http://localhost/cgi-bin/file_name.cgi and the browser processes the cgi script. My problem seems to be how different the installation of Apache is in OS X. Things are in different places and named differently than they are in a normal Unix installation. If I put my cgi scripts in CGI-Executables, then I can't access them because they're one level above the site root directory, so http://localhost, etc. doesn't work. So I tried creating a cgi-bin directory inside the Documents directory, which I suppose is the same as htdocs or site root directory . I gave the directory executable privileges, but when I type in http://localhost/cgi-bin/file_name.cgi, the browser says it can't find the file. Gee, what the heck do you do in OS X's Apache to make cgis work?
P.S. I configured Apache as per your instructions in your article.

[ Reply to This | # ]
Odd, works fine here...
Authored by: robg on Mar 19, '01 09:35:18PM

I have another installation of OS X PB where I hadn't enabled CGI's yet, so I just did. I followed the steps outlined in this thread, and it worked perfectly. I can open the two test CGI's, and a quick install of one of my CGI's worked as well. You shouldn't have to create any new directories anywhere. I was able to run the CGI by typing in "http://localhost/cgi-bin/test-cgi". You could also replace "localhost" with "" and it will also work.

There must be something about the particular CGI. One of the things that's bit me more than once is Mac line breaks vs. UNIX line breaks. Open the file in 'vi' or 'emacs', and make sure it's free of ^M characters.

If you like, you could email me the CGI, and I'll see if I can get it running here...

[ Reply to This | # ]
Odd, works fine here...
Authored by: Anonymous on Mar 20, '01 05:30:42PM

You know what the problem was? I had earlier edited my apache.conf file using TextEdit and that screwed things up. I started over with a clean apache.conf file, enabled php and followed your instructions. Now perl CGIs work fine without a hitch. Boy am I happy now. I'm using Jedit to do my perl coding with. I'm wondering how the heck I can get it to do php tags and html tags. I guess that's asking a lot. I'm spoiled from being able to do HTML, CFML, Perl, PHP, and ASP inside ColdFusion Studio.

[ Reply to This | # ]
Odd, works fine here...
Authored by: joestalin on Aug 16, '02 06:07:58PM

Funny you should ask. I just started messing with PHP in my install. Check this out:

[ Reply to This | # ]
No Config dir?
Authored by: ijonb on Feb 23, '02 03:05:24PM

While following the directions, I changed to /Library/WebServer, but I do not have a Configuration Directory in there. There are only 2 directories in there- "CGI-Executables" and "Documents". Am I not doing something correctly?

[ Reply to This | # ]
No Config dir?
Authored by: piddle spank on Mar 07, '02 07:12:49AM

same here damn confusing even searchs did not show the file needed

[ Reply to This | # ]
No Config dir?
Authored by: piddle spank on Mar 07, '02 07:19:10AM

btw i am awear that this is for the PB. damn i feel like a retard now :)

[ Reply to This | # ]
slight mod for Jaguar
Authored by: jpkang on Dec 16, '02 06:32:07PM
This may be old news, but this hint works for OS X 10.2.x Jaguar, but the file is httpd.conf, located in the invisible /private/etc/httpd/ directory. It's easiest to search for "httpd.conf" using Sherlock, making sure that the visibility attribute is "off." Edit it in a text editor like BBEdit (you'll have to have appropriate privileges since the file is owned by root), and save it. You will find that typing "apachectl start" will not work after this, but you can restart Web Sharing in the "Sharing" System Preferences pane.

[ Reply to This | # ]
slight mod for BBEdit
Authored by: mikerose on Apr 11, '03 11:51:06PM

This works as advertised. Note that BBEdit 6.5 or higher can open and save (with "sudo" permissions) root-owned files.

Now, on to the Moveable Type install... yikes!

[ Reply to This | # ]
PB ONLY - Enabling CGI support in Apache
Authored by: BodhiCat on Dec 18, '07 11:27:01AM

Yes, adding ExecCGI to Options in the Apache2 configuration file worked. If this isn't done you will get a 403 permissions error when trying to run CGI programs. New config file is in /etc/Apache2/httpd.conf. Changes to the old httpd.conf file in /etc/httpd/ won't work.

This is for 10.5 Leopard. Now just gotta get rid of that annoying space desktop background.

[ Reply to This | # ]