Redhat: Apache CGI scripts

From Define Wiki
Jump to navigation Jump to search

Deploy a CGI script

This is easier than it sounds as no programming experience is required and all of the instructions can be found in the httpd manuals.

Apache config for CGI

LoadModule cgi_module
ScriptAlias /cgi-bin/ "/var/www/cgi-bin"

First the cgi module must be loaded. The second directive points apache to look for scripts in <hostname>/cgi-bin/

By default the directory container does not allow for executable scripts:

<directory /var/www/cgi-bin>
        AllowOverride None
        Options None
        Order allow,deny
        Allow from all
</directory>

Some changes must be made to allow CGI scripts to be exectuted:

<directory /var/www/cgi-bin>
        AllowOverride None
        Options ExecCGI
        AddHandler cgi-script .pl
        Order allow,deny
        Allow from all
</directory>

A simple CGI script

This is the example script hello.pl from the httpd-manuals

#!/usr/bin/perl
print "Content-type: "text/html\n\n";
print "Hello World"

Set the permissions to 755 using chmod. The SELinux context should be inherited from the /var/www/cgi-bin directory.

Once this is all set up, you can point your browser towards the cgi script you should see the output of the script.

http://boston1.example.com/cgi-bin/hello.pl