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.
![]() | CGI Security |
|---|---|
Allowing the server to execute CGI scripts is a potential security hole. Refer to Section 28.7, “Avoiding Security Problems” for additional information. | |
In openSUSE, 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 configuration (see
Section 28.2.1.2, “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/"<Directory "/srv/www/www.example.com/cgi-bin/"> Options +ExecCGI
AddHandler cgi-script .cgi .pl
Order allow,deny
Allow from all </Directory>
Tells Apache to handle all files within this directory as CGI scripts. | |
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 |
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
example.
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
it test.cgi.
Files accessible by the Web server should be owned by to the user
root (see
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
proper permissions.
Now call http://localhost/cgi-bin/test.cgi or
http://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:
CGI Troubleshooting
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
http://localhost/cgi-bin/test.cgi.
Are the file permissions correct? Change into the CGI directory and execute the ls -l test.cgi. Its output should start with
-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 programming errors.