Apache/CGI

CGI scripts
The CGI 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): precise the folder name where Apache is authorized to executer the CGI scripts.

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

Actually the path  doesn't really exist, it's redirected to the scripts path, set by the directive, and it allows to write some URL like.

ExecCGI
The following clause activates the option  in , which authorize Apache to execute some scripts on the server:

For example, if a script is called  into  : Then, call the URL:

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

Recapitulation
Full example on Windows, in the Apache configuration:

In  :

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
This script generates an HTML page.

VBS
For Windows.

Known errors
or
 * Error 500 Server error! : replace a  by a.
 * 1) setsebool -P httpd_enable_cgi 1
 * 2) chcon -R -t httpd_sys_script_exec_t cgi-bin/your_script.cgi
 * Error 403 Forbidden access : impossible to list this folder, so call directly its files.
 * If the file source code is appearing in the navigator: the .htaccess is not properly set.
 * couldn't create child process : replace the path after shebang. For example:
 * par.
 * par.
 * End of script output before headers : missing header (eg: move the importation before ). But it can also be the symptom of a compilation error in the script language.
 * malformed header from script: Bad header: : the header is not adapted (eg: replace  by   if there is a   after).

Otherwise consult the Apache logs...