PB ONLY - Enabling CGI support in Apache

Dec 28, '00 08:29:43PM

Contributed by: robg

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:

http://127.0.0.1/cgi-bin/test-cgi

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
GATEWAY_INTERFACE = CGI/1.1
... 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!

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20001228202943763