In this article you will learn how to configure Apache Server so that will permit CGI execution. First before you start with CGI you need to have Apache configured so it will run CGI programs to work properly. Above you will find some samples on how to configure Apache Server.
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 directive looks like:
<font color="#ff0000">ScriptAlias /cgi-bin/ /usr/local/apache/cgi-bin</font>
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://www.example.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
CGI programs are often restricted to ScriptAlias'ed directories for security reasons.
In this way, administrators can tightly control who is allowed
to use CGI programs. However, if the proper security
precautions are taken, there is no reason why CGI programs
cannot be run from arbitrary directories. For example, you may
wish to let 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.
Explicitly
using Options to permit CGI execution
You could explicitly use the Options directive,
inside your main server configuration file, to specify that CGI
execution was permitted in a particular directory:
<font color="#ff0000"><Directory /usr/local/apache/htdocs/somedir>
Options +ExecCGI
</Directory></font>
The above directive tells Apache to permit the execution of
CGI files. You will also need to tell the server what files are
CGI files. The following AddHandler directive
tells the server to treat all files with the cgi or pl extension as CGI programs:
<font color="#ff0000">AddHandler cgi-script cgi p</font>
.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 in
your main server configuration:
<font color="#ff0000">AllowOverride Options</font>
In the .htaccess file, you'll need the
following directive:
<font color="#ff0000"> Options +ExecCGI</font>
which tells Apache that execution of CGI programs is
permitted in this directory.