University of Minnesota
Computer Science & Engineering Help Pages

CGI Scripting Tutorial

Setting up a CGI script can be extremely frustrating. While the basic concepts are simple, there are many steps, and missing any of them will cause your CGI script to fail. The following steps should be sufficient to get your first CGI script up and running in your home directory. Note that this tutorial only covers getting a CGI script working in our environment. It doesn’t say anything about how one writes CGI scripts that do anything useful.

Create your .www directory

(back to top)

The first step is to check the permissions on your home directory and create a .www directory within it. Read Create Your .www Directory to do this.

Create your CGI script

(back to top)

Now that your .www directory has been created and you have set the permissions correctly, it’s time to create your first CGI script. To do this, open a text editor (pico, vi, emacs, xemacs, etc.) and copy and paste the code found in one of the examples found below (use one of the examples in the language you plan to write your future CGI programs). For example:

% cd ~/.www 
% pico test-cgi.cgi

This will call up the pico text editor and allow you to enter your CGI script. Please note, you can use any text editor to create the test file, e.g. xemacs, emacs, vi, etc.

Example Code



import cgi
import cgitb
cgitb.enable()  # for troubleshooting

#print header
print "Content-type: text/html"
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
print "<!DOCTYPE html>"
print "<html>"
print "<head>"
print "<title>Python CGI test</title>"
print "</head>"
print "<body>"
print "<p>Hello, world!</p>"
print "</body>"
print "</html>"


#!/usr/bin/perl -w

use CGI;

$cgi = new CGI();

print $cgi->header();

print '<?xml version="1.0" encoding="UTF-8"?>';

print '<!DOCTYPE html>

      	<title>Perl CGI test</title>

print '
         Hello world!

print '




puts "Content-type: text/html"
puts "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
puts "<!DOCTYPE html>"
puts "<html>"
puts "<head>"
puts "<title>Ruby CGI test</title>"
puts "</head>"
puts "<body>"
puts "<p>Hello, world!</p>"
puts "</body>"
puts "</html>"


echo "Content-type: text/html"
echo ""
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE html>

      	<title>Bash CGI test</title>
          Hello World!


Once the code has been copied to your text editor, save the file and exit.

Set file permissions

Before you can run your cgi script, you need to set the permissions so that it is executable, otherwise you will get an error

% chmod 700 test-cgi.cgi

Test your CGI scripts

(back to top)

Now that you have created your CGI scripts, it is time to test them. Point your favorite web browser at the following URL:<your_username>/test-cgi.cgi

If you get an Internal Server Error, double-check that you followed all the steps above.

Please see the CGI FAQ page for answers to common problems related to CGI scripts.

If you still can’t get it to work, contact Systems Staff.

Disclaimer Policy

(back to top)

University of Minnesota policy requires that the following disclaimer appear on all personal pages and on all student organization pages. It will be automatically appended to pages that you create:
"The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota."

Contact: 1-201 Keller Hall, 200 Union St SE, Minneapolis, MN 55455   Phone: (612) 625-0876   Email: