28.5 Getting CGI Scripts to Work
Apache's Common Gateway Interface (CGI) lets you create dynamic content
with programs or scripts usually referred to as CGI scripts. CGI scripts
can be written in any programming language. Usually, script languages
such as Perl or PHP are used.
To enable Apache to deliver content created by CGI scripts, mod_cgi needs
to be activated. mod_alias is also needed. Both modules are enabled by
default. Refer to Section 28.4.2, Activation and Deactivation
for details on activating modules.
28.5.1 Apache Configuration
the execution of CGI scripts is only allowed in the directory
/srv/www/cgi-bin/. This location is already
configured to execute CGI scripts. If you have created a virtual host
Virtual Host Configuration) and
want to place your scripts in a host-specific directory, you must unlock
and configure this directory.
Example 28-5 VirtualHost CGI Configuration
ScriptAlias /cgi-bin/ "/srv/www/www.example.com/cgi-bin/"
AddHandler cgi-script .cgi .pl
Allow from all
Tells Apache to handle all files within this directory as CGI
Enables CGI script execution
Tells the server to treat files with the extensions .pl and .cgi as
CGI scripts. Adjust according to your needs.
The Order and Allow
directives control the default access state and the order in which
Allow and Deny directives are evaluated. In this case
deny statements are evaluated before
allow statements and access from everywhere is
28.5.2 Running an Example Script
CGI programming differs from "regular" programming in that the CGI
programs and scripts must be preceded by a MIME-Type header such as
Content-type: text/html. This header is sent to the
client, so it understands what kind of content it receives. Secondly,
the script's output must be something the client, usually a Web browser,
understands—HTML in most cases or plain text or images, for
A simple test script available under
/usr/share/doc/packages/apache2/test-cgi is part of
the Apache package. It outputs the content of some environment variables
as plain text. Copy this script to either
/srv/www/cgi-bin/ or the script directory of your
virtual host (/srv/www/www.example.com/cgi-bin/) and name
Files accessible by the Web server should be owned by to the user
Section 28.7, Avoiding Security Problems for additional information).
Because the Web server runs with a different user, the CGI scripts must
be world-executable and world-readable. Change into the CGI directory
and use the command chmod 755 test.cgi to apply the
Now call https://localhost/cgi-bin/test.cgi or
https://www.example.com/cgi-bin/test.cgi. You should see the
CGI/1.0 test script report.
If you do not see the output of the test program but an error message
instead, check the following:
Have you reloaded the server after having changed the configuration?
Check with rcapache2 probe.
If you have configured your custom CGI directory, is it configured
properly? If in doubt, try the script within the default CGI directory
/srv/www/cgi-bin/ and call it with
Are the file permissions correct? Change into the CGI directory and
execute the ls -l test.cgi. Its output should start
-rwxr-xr-x 1 root root
Make sure that the script does not contain programming errors. If you
have not changed test.cgi, this should not be the case, but if you are
using your own programs, always make sure that they do not contain