41.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 41.4.2,
Activation and Deactivation for details on activating
modules.
41.5.1 Apache Configuration
In SUSE Linux Enterprise Server, 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
Virtual Host Configuration) and want to
place your scripts in a host-specific directory, you must unlock and
configure this directory.
41.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 41.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 https://localhost/cgi-bin/test.cgi or
https://example.com/cgi-bin/test.cgi. You should
see the CGI/1.0 test script report
.
41.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
https://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.