Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 

What's inside.

Account Management

CSE Wiki

E-mail

Hardware and Media

Network and Wireless

Offsite Access

Printing

Processes

Security and Passwords

Software and Tools

Systems Staff

Web Development

 

Help Home

CSE Home

 
 
 
 
 

 
  Home > Web > CGI Scripting Tutorial

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

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

Create your CGI script

Python

Now that your .www directory has been created and you have set the permissions correctly, it’s time to create your first Python CGI script. To do this, enter the following commands at a UNIX prompt:

% cd ~/.www 
% pico test-python.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.

Copy and paste the following into pico:


#!/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>"

Save this file and exit pico. Then, execute the following commands to ensure that the file permissions are correct.

% chmod a+x test-python.cgi
% chmod go-w test-python.cgi

Perl

Now that your .www directory has been created and you have set the permissions correctly, it’s time to create your first Perl CGI script. To do this, enter the following commands at the UNIX prompt:

% cd ~/.www
% pico test-perl.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.

Copy and paste the following into pico:


#!/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>
';

Save this file and exit pico. Then, execute the following commands to ensure that the file permissions are correct.

% chmod a+x test-perl.cgi
% chmod go-w test-perl.cgi

Test your CGI scripts

Now that you have created your CGI scripts, it is time to test them. Point your favorite web browser at the following URLs:

http://www-users.cs.umn.edu/~<your_username>/test-python.cgi
http://www-users.cs.umn.edu/~<your_username>/test-perl.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

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

Related Links

University of Minnesota Policies

Helpful CGI links

The following list contains helpful resources on CGI scripts.

 
The University of Minnesota is an equal opportunity educator and employer.
CSE Online Help