CGI

From Apis Networks Wiki

Jump to: navigation, search

Contents

Configuration

Running unrecognized extensions as CGI

By default the Web server will run files ending in .cgi, .pl, and .rb as CGI scripts. To treat other extensions, like .tcl or .py, as CGI scripts create a file named .htaccess within the directory of the script and use the AddHandler directive,

Adding .py as a CGI script
AddHandler cgi-script .py

Running CGI Applications

Running FastCGI Applications

Restarting a FastCGI application

A FastCGI application may be restarted by modifying the last modified time of the script. The recommended approach is to use touch to alter the file's timestamp within the shell, e.g. touch index.fcgi; however, not everyone has access to a SSH-capable client. Alternatively the following methods will work too:

  • Upload a copy of the .fcgi file overwriting the original. Usually these scripts are small bootstraps designed to run the entire application. A 4 KB file would take a few seconds to upload. Ensure that you change the permissions to 755 after uploading.
  • Visit the File Manager and run dos2unix on the file by clicking on the Windows -> Unix button. This should have no effect on the integrity of the file, but will update its last modified timestamp.

FastCGI will pick up the change immediately following a timestamp change.

CGI/FastCGI Errors

Application won't start; 550 Internal Server Error

Applications with incorrect permissions generally are the cause of a 550 Internal Server Error ("ISE" for short). suexec is a wrapper used to execute CGI scripts and enforces very stringent permissions. Ensure that both the CGI script and parent directory have the permissions 755, owner: rwx, group: r-x, other: r-x. The following snippet illustrates a file listing from the shell. First the incorrect, common permissions are listed followed by the correct permissions.

Incorrect

[admin@server html]# ls -la trac.fcgi 
-rwxrwxr-x 1 admin admin 1083 Sep 11 14:58 trac.fcgi
Correct

[admin@server html]# ls -la trac.fcgi 
-rwxr-xr-x 1 admin admin 1083 Sep 11 14:58 trac.fcgi

Permissions may be changed from within the File Manager, FTP, or shell via the chmod command.

Other Non-suexec Errors

Two other common problems exist with CGI scripts, which are not directly related to the suexec wrapper.

Script was edited on Windows/Mac, forcing incorrect EOL markers
At the top of each script is a shebang operator. A CGI script uses this first line, which is required, as the shell in which to execute the code. Most shebangs look like #!/usr/bin/perl, #!/usr/bin/ruby, or #!/usr/bin/python for a Perl, Ruby, or Python script respectively. Files edited under basic text editors like Notepad on Windows will change line endings from \n to \r\n. On a Mac, the EOL marker is \r. Linux expects \n to separate each line; this style is common referred to as Unix-style. Both \r\n and \r will result in a shebang that is unparseable on Linux.
There are a few possible fixes:
  1. Visit the File Manager to convert end-of-line styles from Windows to Unix.
  2. Use a text editor that supports Unix-style EOL markers, such as Notepad++ or SciTE
  3. Use dos2unix or mac2unix from the shell to convert a script edited under Windows/Mac to its proper Linux-native form.
Programming error
There is no universal solution for a programming error. Check /var/log/httpd/error_log for any errors logged during the execution of the script. Things range from syntax errors, missing libraries, garbled uploads, and even those pesky die() statements you never saw coming.
Personal tools