User Tools

Site Tools


techstaff:how_to_use_cgi

This is an old revision of the document!


CGI Programming

The purpose of the CGI service is to allow CS users to learn CGI programming and make small, dynamic sites in their course work.

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

To use our CGI server, you must first be registered in a class that requires 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

change directory to the directory created for you at registration

cd /stage/some-cgi-server/your-username/public_html

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"; 

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.

Now try to run the program from your browser. Assuming your CS username is fred, and the cgi server you use is mpcs53001

http://mpcs53001.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://mpcs53001.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.

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

/var/lib/dokuwiki/data/attic/techstaff/how_to_use_cgi.1431979858.txt.gz · Last modified: 2015/05/18 15:10 by chudler

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki