CGI Programming

The purpose of this CGI service is to allow CS users to learn CGI programming or add small flourishes to personal web sites.

Large off-the-shelf web applications, such as wikis, picture galleries, bulletin boards, publishing platforms, etc, are prohibited for security reasons.

To use our CS CGI server, you must first register.

Upon successful registration, you will be notified of where to put your code, and the URL to access it.

CGI programs may be written in many languages, including perl, python, php, shell, C, etc. Except for PHP programs (extension .php), the programs must be named with the filename extension .cgi

File Permissions

.cgi files run under the username that owns them. These program files, as well as datafiles they read, may be set to permissions 700 and 600, to protect passwords or code.

html files should be set to 644, and directories should be set to 755.

php code does not go through suexec (i.e. runs under a standard unpriviledged user, such as www), and can be 644.

Example 1: Hello World via Perl

  1. cd to the directory created for you at registration
    • cd /stage/some-cgi-server/your-username/public_html

  2. Now create a file named hello.cgi with three lines of perl code:
      #!/usr/bin/env perl
      print "Content-type: text/html\n\n";
      print "Hello World!\n";

  3. Set the permissions to be executable, then test it at the command line:
    • chmod +x hello.cgi
      ./hello.cgi

    • output:
    • Content-type: text/html

      Hello World!

    • The first line printed from any cgi program must be a Content-type: line, followed by a blank line. Anything else will cause an error.

    • All cgi programs must have the filename extension .cgi (an exception to this rule is extension .php for PHP programs)

    • Running the program from the command line is a good way to test your program before accessing it via the web.

  4. Now try to run the program from your browser. Assuming your CS username is fred, and the cgi server you registered for is cgi-general:
    • http://cgi-general.cs.uchicago.edu/~fred/hello

Example 2: Hello World via PHP

PHP is a language designed explicitly for web programming. A single line in the file public_html/hello.php will do. Remember, the file must be world-readable:

<? print("Hello World!\n"); ?>

You can run your php file from the command line:
php hello.php

Or from your browser:
http://cgi-general.cs.uchicago.edu/~fred/hello

Debugging

Debugging cgi programs can be tricky, because they run on a server you cannot log into, and many errors just produce the unhelpful message Internal Server Error.

  • Make sure the cgi program is executable, and all directories leading to it are world-executable.

  • Run your cgi program at the command line. The first line must be a Content-type: line, followed by a blank line, followed by valid html. You can validate your html at: http://validator.w3.org

  • You may be interested in the web server error log messages that correspond to your script: /stage/some-cgi-server-logs

  • The error message Premature end of script headers often means your program printed something before the Content-type: line. Look for other output or error messages.

For additional assistance with this service, email techstaff@cs.uchicago.edu.