Getting Started with CGI

Since you are reading this section, it is presumed that you already know what CGI processing is. If not, a good introduction can be found here. Yahoo! has a good index of CGI references.

Getting started with CGI can seem to be a daunting task. Many of the posts to comp.infosystems.www.servers.unix contain requests for help to get CGI working. Like all types of network programming, it is necessary to get more than one thing running correctly before you see any results, but it's not difficult, others have done it before, and after setting the configuration options in this section, your server should be configured to process CGI requests.

Note: Through out this document, I refer to CGI scripts. This is only a convention. CGI programs can be written in virtually any language you are conversant with. The can be compiled or interpreted or a mixture of both. The choice is yours. This guide will not help you write CGI scripts, but will help you get them to run, and help you to debug them is they don't. If you don't know how to program, you are going to have to learn, or find someone who will do the programming for you!.

Minimum Configuration

Apache provides a number of configuration options relating to CGIs. There are two main options for CGIs and Apache:
  • Configure a directory where all CGI scripts will reside or
  • Configure Apache to recognize specific file extensions as CGI programs.
We are going to use the first option, and define a directory, by convention cgi-bin which will contain all our scripts. Again this is a convention which many (most?) web sites Adhere to. Your milage may vary.

Many Linux distributions now have cgi-bin enabled in the default configuration.

CGI Location

The ScriptAlias option allows you to group all you CGI scripts into a single directory. Any files found in this directory will be treated by Apache as CGI scripts.

I have a personal dislike for the default location of the cgi-bin directory for Apache 2.x under the Ubuntu distribution. For some reason it is located in:

/usr/lib/cgi-bin>

which forces you to either be SuperUser to update it, or give yourself write privileges to a sub-directory of /usr/lib, and area that is generally read-only on most disto's and Unix's.

My personal preference is to have the cgi-bin directory adjacent to the DocumentRoot (html) directory, in some where like /var/virtual/VM_Name/, but then that's just my personal preferences.

To specify a directory where you cgi scripts will reside, edit srm.conf and change

 
# ScriptAlias: This controls which directories contain server scripts.
# Format: ScriptAlias fakename realname

#ScriptAlias /cgi-bin/ /apache/cgi-bin/
  To: (just remove the '#' comment character)  
# ScriptAlias: This controls which directories contain server scripts.
# Format: ScriptAlias fakename realname

ScriptAlias /cgi-bin/ /apache/cgi-bin/
  Here we have specified that files found in the directory /apache/cgi-bin/ should be treated as CGI scripts. NT users would specify:  
# ScriptAlias: This controls which directories contain server scripts.
# Format: ScriptAlias fakename realname

ScriptAlias /cgi-bin/ c:/apache/cgi-bin/
  Note: in both the Unix and NT examples, we have specified an absolute path from the root of the file system.

CGI scripts, in the above example would be referenced by the url:

<a href="http://your.host.name/cgi-bin/your_script_name">

or

<a href="/cgi-bin/your_script_name">

Once you have decided where you CGI directory will be located, edit access.conf and change the following to conform with the ScriptAliases changes.

 
# /apache/cgi-bin should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.

<Directory c:/apache/cgi-bin>
AllowOverride None
Options None
</Directory>
  Re-start your Apache web server and then proceed to test your CGI configuration.

References

General

Apache Directives

 


RTFM Index   Testing CGI
Copyright © 2006 Open Source Migrations ltd. All Rights Reserved.
Apache RTFM last updated Saturday October 21 2006