Fedora 15

Configuring Apache to permit CGI

method 1: ScriptAlias directive

The ScriptAlias directive tells Apache that a particular directory is set aside for CGI programs. Apache will assume that every file in this directory is a CGI program and will attempt to execute it.


Create a perl script: test.pl

!/usr/bin/perl
print "Content-type: text/plain\n\n";
foreach $var (sort(keys(%ENV))) {
$val = $ENV{$var};
$val =~ s|\n|\\n|g;
$val =~ s|"|\\"|g;
print "${var}=\"${val}\"\n";
}

put it in /var/www/cgi-bin/ directory


vi /etc/httpd/conf/httpd.conf

ServerName www.iamtraining.com:80
ServerAdmin root@iamtraing.com
DocumentRoot "/var/www/html"

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


Put all cgi scripts under /var/www/cgi-bin/ directory and make sure they are executable.


/sbin/service httpd restart


http://www.iamtraining.com/cgi-bin/test.pl

"Internal Server Error" shows in the web browser.

Solution:

chmod a+x test.pl

http://www.iamtraining.com/cgi-bin/test.pl


Another Example

Create a HTML file login.html and put it in /var/www/html/

<HTML>
<BODY>
<FORM METHOD="POST" ACTION="/cgi-bin/myscript.cgi">
<PRE>
First Name <INPUT TYPE="text" NAME="fname" MAXLENGTH=15 SIZE=15>
Last Name <INPUT TYPE="text" NAME="lname" MAXLENGTH=20 SIZE=20>
E-Mail Addr <INPUT TYPE="text" NAME="email" MAXLENGTH=35 SIZE=35>
<INPUT TYPE="submit" VALUE="Send Mail!">
<INPUT TYPE="reset" value=" Clear-Form">
</PRE>
</FORM>
</BODY>
</HTML>

Create a cgi script file myscript.cgi

#!/usr/local/bin/perl
read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
($key,$content)=split(/=/,$item,2);
$content=~tr/+/ /;
$content=~s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<BODY BGCOLOR=#FFFFFF>\n";
print "<CENTER>\n";
print "THANK YOU<BR>\n";
print "$fields{fname} $fields{lname}</BR>";
print "I will write<BR>\n";
print "you at<BR>\n";
print "$fields{email}<BR>\n";
print "</CENTER>\n";
print "</BODY></HTML>";

chmod a+x /var/www/cgi-bin/myscript.cgi


method 2:

You could explicitly use the Options directive to sepcify that CGI execution is permitted in a particular directory.

vi /etc/httpd/conf/httpd.conf

<Directory /var/test>
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>

Alias /test /var/test

AddHandler cgi-script .cgi .pl

Modify the login.html file

<HTML>
<BODY>
<FORM METHOD="POST" ACTION="/test/myscript.cgi">
<PRE>
First Name <INPUT TYPE="text" NAME="fname" MAXLENGTH=15 SIZE=15>
Last Name <INPUT TYPE="text" NAME="lname" MAXLENGTH=20 SIZE=20>
E-Mail Addr <INPUT TYPE="text" NAME="email" MAXLENGTH=35 SIZE=35>
<INPUT TYPE="submit" VALUE="Send Mail!">
<INPUT TYPE="reset" value=" Clear-Form">
</PRE>
</FORM>
</BODY>
</HTML>

http://www.iamtraining.com/test/login.html

Forbidden

You don't have permission to access /test/myscript.cgi on this server.

Solution:

system-config-selinux

File Labeling:

Add

File Specification: /var/test(/.*)?
File Type: all files
SELinux Type: httpd_sys_script_exec_t
MLS:s0

Status: "Relabel on next reboot" checked

Restart

cgi

Pay attention to CGI programs with default access.