Installing Your Own CGI Scripts

These notes describe how to install Perl CGI scripts on the Oriole server.

You each have your own script directory under the server's cgi-bin directory, in which you can store and edit files. The full pathname is

        /var/www/cgi-bin/YOUR_LOGIN_ID
If you install a script file, the URL pointing to it would be
        http://oriole.acad.emich.edu/cgi-bin/YOUR_LOGIN_ID/SCRIPT_NAME
You can either create files on another machine and upload them to your directory using secure ftp or secure copy, or you can log in to the server interactively and use one of the available text editors (such as nano, vi, or emacs).

The first line of a Perl CGI must be

                #!/usr/bin/perl
This is known as the "shebang line" and is used by the operating system to tell what program to run to interpret the script - in this case, the Perl interpreter.

The Perl script must have read and execute permission. To accomplish this, use the chmod command:

                $ chmod a+rx SCRIPT_NAME
This sets read and execute permission for everybody. (Reminder: I've provided some notes on useful Linux file-management commands, including chmod.)

A CGI script must output a valid HTTP response to the standard output, consisting of headers and a body, separated by an empty line. Minimally a Content-type header must be present, consisting of the label Content-type: followed by a MIME type, typically text/html.

There are two ways to run a CGI script. For testing purposes, you can run it as a standalone program while logged into the server:

         ./SCRIPT_NAME
Or, you can type its URL in a browser:
         http://oriole.acad.emich.edu/cgi-bin/YOUR_LOGIN_ID/SCRIPT_NAME
These methods are not quite equivalent, especially if there is form data that is being passed by the get method. (Why?) Also, if the script creates any files, then when the script is run by you from the command prompt, the files will be owned by you, but when it is run from a browser via its URL, the files will be owned by the apache server account.

A CGI script generates a document to be returned to the client. Most scripts return html or xhtml documents. This means that the script must output a page with tags, typically via a series of Perl print statements. The document should be well-formed and valid html or xhtml.

If the script needs to process form data, then it needs to extract the form data (from the QUERY_STRING variable if the form specifies the get method, and from the standard input if it specifies the put method), decode the hex-encoded characters, and store the (name, value) pairs in an appropriate data structure, usually a Perl hash. This is called parsing the form data. The script normally does it before doing anything else. In Perl parsing is very simple; here is some parsing code that works for both the get and put methods.


Valid XHTML 1.1! Valid CSS!