The Common Gateway Interface

The Common Gateway Interface (CGI) defines a way in which a web server can interact with external programs, often referred to as CGI programs or CGI scripts, for generation of dynamic content based on a client request. To configure the Apache web server to interact with a CGI program, the first task to complete is to let the web server know where the CGI programs reside.

One method of doing this is with the ScriptAlias directive, which tells Apache that a particular directory has been set aside for CGI programs. Apache will assume that every file in a given directory is a CGI program. Thus the web server will attempt to execute a program when a client requests a particular resource. The ScriptAlias directive is much like the Alias directive, which defines a URL webspace that is mapped to a particular directory on the filesystem. Consider the following example:

ScriptAlias /cgi-bin/ /srv/www/cgi-bin/

In the default SUSE configuration, this line is in the file /etc/apache2/default-server.conf. Equivalent lines may be defined per virtual host in the relevant configuration files. For the given example, the directive marks the target directory, /srv/www/cgi-bin, as containing CGI scripts that are processed by the Apache mod_cgi module 's cgi-script handler.

The effect is that any HTTP request — for example, http://servername/cgi-bin/ test.cgi — causes the server to run the script that is actually located at /srv/www/cgi-bin /test.cgi.

However, CGI programs can also be invoked from arbitrary directories. For CGI programs to reside anywhere in the directory of a site, two configuration steps need to be completed. First, the cgi-script handler must be activated using the AddHandler directive. Second, ExecCGI must be specified in the Options directive for any directory or subdirectories that may contain a CGI program.

AddHandler cgi-script .cgi

<Directory /srv/www/htdocs/dir> Options +ExecCGI </Directory>

The AddHandler directive tells the server to treat all files with the .cgi file extension as CGI programs. In addition, the <Directory> container with the Options + ExecCGI directive tells Apache to permit the execution of CGI programs that reside in the directory /srv/www/htdocs/dir or any subdirectories therein.

Listing 16-2 is a Perl script and an example CGI program that prints a welcome message to a client along with the date.

Was this article helpful?

0 0

Post a comment