CGI

From DreamHost
Jump to: navigation, search

Common Gateway Interface (CGI) is an important World Wide Web technology that enables a client web browser to request data from a program executed on the web server. CGI specifies a standard for passing request data between a web server and the program used to service that request. CGI scripts are often preferred by web developers because of their speed and their minimal resource requirements. CGI scripts on DreamHost can be written in all of the languages commonly used with CGI (Perl, Python, Ruby, even shell scripts). Since DreamHost gives you shell access, CGI scripts can be written in almost any language given you can get an interpreter for that language to compile.

Running a CGI Script on DreamHost

Copy the script to a web-accessible directory (e.g. /home/username/yourdomain.com/cgi-bin). You don't have to put your CGI scripts in a cgi-bin. On DreamHost CGI scripts can run from any directory within your domain. The server understands several file extensions (e.g. .pl), but if you get the source code of your script instead of the result of its execution, try using the generic extension .cgi. If you get a "500 Internal Server Error," you probably need to change the permissions of the file.

$ chmod 755 script.cgi

Running a CGI script without a .cgi extension

If you wish to install a CGI script without the .cgi extension, simply use a <Files> directive in your .htaccess. For example, if you wish to have a cgi-bin directory, create a file named .htaccess inside of it, and insert the following:

 <Files *>
   SetHandler cgi-script
 </Files>

Once this is done, all files within the directory will be run as CGI scripts.

Examples of CGI Scripts

Perl

#!/usr/bin/perl

use strict;
use CGI;

my $q = CGI->new;
print $q->header();
print "Hello, world!";

Python

#!/usr/bin/python

print "Content-type: text/html"
print
print "Hello, world!"

Ruby

#!/usr/bin/ruby

require 'cgi'

cgi = CGI.new
cgi.out{ 'Hello, world!' }

Shell Script

#!/bin/sh

echo "Content-type: text/html"
echo
echo "Hello, world!"

Examples in Languages Less Commonly Used for CGI Scripting

suEXEC

suEXEC is enabled for all DreamHost users and cannot be disabled. Because of this, some features, such as the SetEnv directive in .htaccess files, won't work as expected. See the suEXEC page for workarounds and more details.

See Also

(Old KB Article on CGI)

External Links