CGI
From DreamHost
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 are often preferred by web developers because of their speed and their minimal resource requirements. A CGI on DreamHost could be a java .jar, a shell script .sh, a perl .pl, a ruby .rb, python, etc. In fact, the PHP interpreter preferred and installed by default on DreamHost is a CGI version. /dh/cgi-system/php5.cgi
To install a cgi script on dreamhost the following seems to work:
- create a directory where you want to place the script. For example you could create a cgi-bin directory in your domain (yourdomain/cgi-bin).
- edit or create the .htaccess file in that directory and add Options +ExecCGI (this is not necessary as we automatically set this for all domains and it works for any folder within domains. --Dallas 12:10, 7 April 2008 (PDT))
- place the scripts in that directory
- because dreamhost runs suexec (which means that the scripts will be run with you as user and not the apache www user) you need to also do the following:
-
chmod -R 755 cgi-bin(See Suexec) - try
chmod u+rxon your security sensitive scripts to allow your user execute perms - make sure that you are listed as a user
- make sure that your default group is listed as group (else do a chgrp -R pg****** cgi-bin)
- if you set up permissions incorrectly, you'll probably get "500 Internal Server Error"
-
- take care of the script file name extension. The server understands several extensions, like ".pl", but if you get source code of your script instead of result of its execution, try to use universal extension ".cgi" (or ".fcgi" for FastCGI)
Contents |
CGI Examples Scripts
CGI Shell Script
Create this file as test-cgi.cgi in your /cgi-bin/ directory with permissions set as chmod u+rx or the least secure, 750.
#!/bin/sh # disable filename globbing set -f echo "Content-type: text/plain; charset=iso-8859-1" echo echo CGI/1.0 test script report: echo echo argc is $#. argv is "$*". echo echo SERVER_SOFTWARE = $SERVER_SOFTWARE echo SERVER_NAME = $SERVER_NAME echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE echo SERVER_PROTOCOL = $SERVER_PROTOCOL echo SERVER_PORT = $SERVER_PORT echo REQUEST_METHOD = $REQUEST_METHOD echo HTTP_ACCEPT = "$HTTP_ACCEPT" echo PATH_INFO = "$PATH_INFO" echo PATH_TRANSLATED = "$PATH_TRANSLATED" echo SCRIPT_NAME = "$SCRIPT_NAME" echo QUERY_STRING = "$QUERY_STRING" echo REMOTE_HOST = $REMOTE_HOST echo REMOTE_ADDR = $REMOTE_ADDR echo REMOTE_USER = $REMOTE_USER echo AUTH_TYPE = $AUTH_TYPE echo CONTENT_TYPE = $CONTENT_TYPE echo CONTENT_LENGTH = $CONTENT_LENGTH
PerlCGI Example
#!/usr/bin/perl
##
## printenv -- demo CGI program which just prints its environment
##
print "Content-type: text/plain; charset=iso-8859-1\n\n";
foreach $var (sort(keys(%ENV))) {
$val = $ENV{$var};
$val =~ s|\n|\\n|g;
$val =~ s|"|\\"|g;
print "${var}=\"${val}\"\n";
}
Suexec
The suEXEC feature provides DreamHost users the ability to run CGI programs that are different from the calling web server. Normally, when a CGI executes, it runs as the same user who is running the web server.
Suexec has very strict permission checking, and any failure in that checking will result in your CGI programs failing with
Premature end of script headers.
Also note that suexec wips out all environment variables that don't start with HTTP_. So in .htaccess:
SetEnv FOO "will not work" SetEnv HTTP_BAR "will work"
This is a bit hacky but it seems to be the only workaround.
FAQs
Question: Do I have to place a CGI script in the cgi-bin directory?
Answer: No, well... maybe you should try it out first. For example, the blosxom.com perl based CGI works fine in the domain home folder renamed index.cgi
External Links
- Apache Tutorial: Dynamic Content with CGI
- suEXEC Information
- Writing a CGI Program
- cgi Apache Module mod_cgi

