Apache Guide: Dynamic Content with CGI
With this column Rich Bowen begins his weekly look at basic tasks that every Apache Webmaster must face. Rich is the author of Apache Server Unleashed.
The CGI is the simplest, and most common, way to put dynamic content on your web site. This week's column will be an introduction to setting up CGI on your Apache Web server and getting started writing CGI programs.The CGI (Common Gateway Interface) is the simplest, and most common, way to put dynamic content on your web site. This week's column will be an introduction to setting up CGI on your Apache Web server and getting started writing CGI programs.
Configuring Apache to Permit CGI
In order to get your CGI programs to work properly, you'll need to have Apache configured to permit CGI execution. There are several ways to do this.
ScriptAlias
The ScriptAlias directive tells Apache that a particular directory is
set aside for CGI programs. Apache will assume that every file in this
directory is a CGI program and will attempt to execute it, when that particular
resource is requested by a client.
The ScriptAlias direcive looks like:
ScriptAlias /cgi-bin/ /usr/local/apache/cgi-bin/
The example shown is from your default httpd.conf configuration file, if you
installed Apache in the default location.
The ScriptAlias directive is much like the Alias directive, which defines
a URL prefix that is to mapped to a particular directory. Alias and ScriptAlias
are usually used for directories that are outside of the DocumentRoot directory.
The difference between Alias and ScriptAlias is
that ScriptAlias has the added meaning that everything under that URL prefix
will be considered a CGI program.
So, the example above tells Apache that any request
for a resource beginning with /cgi-bin/ should be served from the directory
/usr/local/apache/cgi-bin/, and should be treated as a CGI program.
For example, if the URL http://dev.rcbowen.com/cgi-bin/test.pl is requested,
Apache will attempt to execute the file /usr/local/apache/cgi-bin/test.pl and
return the output. Of course, the file will have to exist, and be executable,
and return output in a particular way, or Apache will return an error message.
CGI Outside of ScriptAlias Directories
Occasionally you will want to have CGI programs outside of ScriptAlias'ed
directories. Usually, this will be for the purpose of letting users have web
content in their home directories with the UserDir directive. If they
want to have their own CGI programs, but don't have access to the main
cgi-bin directory, they will need to be able to run CGI programs
elsewhere.
.htaccess Files
A .htaccess file is a way to set configuration directives on a per-directory
basis. When Apache serves a resource, it looks in the directory from which it
is serving a file for a file called .htaccess, and, if it finds it, it will
apply directives found therein. .htaccess files can be permitted with the
AllowOverride directive, which specifies what types of directives
can appear in these files, or if they are not allowed at all. To permit
the directive we will need for this purpose, the following configuration
will be needed:
AllowOverride OptionsIn the
.htaccessfile, you'll need the following directive:Options +ExecCGIwhich tells Apache that execution of CGI programs is permitted in this directory.
Explicitly using Options to Permit CGI Execution
