Main navigation | Main content
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.
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.
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.
#!/soft/python-2.5-bin/python import cgi import cgitb cgitb.enable() # for troubleshooting #print header print "Content-type: text/html" print print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict //EN \" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" print "<html>" print "<head>" print "<title>Python CGI test</title>" print "</head>" print "<body>" print "<p>Hello, world!</p>" print "</body>" print "</html>"
#!/soft/perl5.8.7-bin/perl -w
use CGI;
$cgi = new CGI();
print $cgi->header();
print '<?xml version="1.0" encoding="UTF-8"?>';
print '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict
//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Perl CGI test</title>
</head>
<body>';
print '
<p>
Hello world!
</p>';
print '
print '
</body>
</html>
';
#!/soft/ruby/1.9.1/SunOS5.8/bin/ruby puts "Content-type: text/html" puts puts "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" puts "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" puts "<html>" puts "<head>" puts "<title>Ruby CGI test</title>" puts "</head>" puts "<body>" puts "<p>Hello, world!</p>" puts "</body>" puts "</html>"
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html>
<head>
<title>Bash CGI test</title>
</head>
<body>
<p>
Hello World!
</p>
</body>
</html>"
Once the code has been copied to your text editor, save the file and exit.
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
Now that you have created your CGI scripts, it is time to test them. Point your favorite web browser at the following URL:
http://www-users.cselabs.umn.edu/~<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.
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."