Apache/CGI

< Apache

CGI scripts

The CGI (Common Gateway Interface) is a norm permitting Apache to execute some programs, which can be written in any programming language (Bash, C, Java, Perl, PHP, Python...), from the moment it's executable and it respects certain in/out constraints.

Configure the CGI scripts access

To make Apache interpret the scripts, it's necessary to do a minimum of settings in the site configuration.

ScriptAlias

The directive (from httpd.conf):

ScriptAlias /cgi-bin/ /scripts path/

precise the folder name where Apache is authorized to executer the CGI scripts[1].

Unix example:

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

Windows example, use the URL format (no backslash):

ScriptAlias /cgi-bin/ "C:/wamp/bin/apache/apache2.2.27/cgi-bin/"

Actually the path /cgi-bin/ doesn't really exist, it's redirected to the scripts path, set by the directive, and it allows to write some URL like http://server/cgi-bin/my_script.

ExecCGI

The following clause activates the option ExecCGI in /var/www/cgi-bin, which authorize Apache to execute some scripts on the server:

<Directory /var/www/cgi-bin>
  Options ExecCGI
</Directory>

For example, if a script is called essai.cgi into /home/httpd/cgi-bin:

<Directory /home/httpd/cgi-bin>
  Options ExecCGI
</Directory>

Then, call the URL: http://serveur/cgi-bin/essai.cgi

AddHandler

This clause permits to choose the files extensions which will be authorized, eg:

AddHandler cgi-script .cgi .exe .pl .py .vbs

Recapitulation

Full example on Windows, in the Apache configuration:

ScriptAlias /cgi-bin/ "E:/www/cgi-bin/"
<Directory "E:/www/cgi-bin/">
  Options FollowSymLinks Indexes
  AllowOverride All
  Order deny,allow
  Allow from all
  Require all granted		
</Directory>

In E:/www/cgi-bin/.htaccess :

AddHandler cgi-script .cgi .exe .pl .py .vbs

Write a CGI program

The main constraint concerns the program outputs. If a CGI script generates some data on its standard output, he must display an HTTP header before, allowing to identify them.

Bash

#!/bin/bash

# Header
echo "Content-type: text/html"

# Header end
echo ""

# Content to display in the navigator
echo "<html><body>Hello World!</body></html>"

This script generates an HTML page.

Perl

#!c:/perl/perl/bin/perl.exe -w
use CGI;
my $query = new CGI;
my $Name = $query->param('Name');
print $query->header();
print "Hello World!"

Python

#!C:\Program Files (x86)\Python\python.exe
# -*- coding: UTF-8 -*-
print "Content-Type: text/plain;charset=utf-8"
print
print "Hello World!"

VBS

For Windows[2].

'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF
WScript.Echo "Hello World!"
Wscript.Quit 0

Known errors

or

# setsebool -P httpd_enable_cgi 1
# chcon -R -t httpd_sys_script_exec_t cgi-bin/your_script.cgi

Otherwise consult the Apache logs...

References

  1. http://httpd.apache.org/docs/current/en/howto/cgi.html
  2. http://wiki.uniformserver.com/index.php/CGI:_VBScript_CGI
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.