26.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 26.4.2, “Activation and Deactivation” for details on activating modules.

[Warning]CGI Security

Allowing the server to execute CGI scripts is a potential security hole. Refer to Section 26.7, “Avoiding Security Problems” for additional information.

26.5.1. Apache Configuration

In SUSE Linux, 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, “Virtual Host Configuration”) and want to place your scripts in a host-specific directory, you must unlock and configure this directory.

Example 26.5. VirtualHost CGI Configuration

ScriptAlias /cgi-bin/ "/srv/www/example.com_cgi-bin/"1

<Directory "/srv/www/example.com_cgi-bin/">
 Options +ExecCGI2
 AddHandler cgi-script .cgi .pl3
 Order allow,deny4
 Allow from all

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 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 enabled.

26.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 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/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 26.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://example.com/cgi-bin/test.cgi. You should see the “CGI/1.0 test script report”.

26.5.3. Troubleshooting

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.