Monday through Friday, 8 a.m. to 8 p.m.
612-625-0876 or use the red phone in any CSE Lab
CGI Tutorial and FAQ
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 .www Directory to do this.
Create your CGI script
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.
cgitb.enable() # for troubleshooting
print "Content-type: text/html"
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
print "<!DOCTYPE html>"
print "<title>Python CGI test</title>"
print "<p>Hello, world!</p>"
$cgi = new CGI();
print '<?xml version="1.0" encoding="UTF-8"?>';
print '<!DOCTYPE html>
<title>Perl CGI test</title>
puts "Content-type: text/html"
puts "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
puts "<!DOCTYPE html>"
puts "<title>Ruby CGI test</title>"
puts "<p>Hello, world!</p>"
echo "Content-type: text/html"
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<title>Bash CGI test</title>
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
Now that you have created your CGI scripts, it is time to test them. Point your favorite web browser at the following URL:
If you get an Internal Server Error, double-check that you followed all the steps above.
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."
What Perl path should I have at the top of my script?
Your Perl CGI script should begin with:
What Python path should I have at the top of my script?
Your Python CGI script should begin with:
Why won't my Python .py file execute?
The way our server is configured, you need to name your executable CGI scripts with the
How should permissions be set on my CGI script?
- Your home directory should have permissions of 701
- Your .www (and any sub-directories containing your scripts) should have permissions of 701
- Your CGI scripts should have permissions of 701
- Files that your cgi script needs to read (for example an image file) should have permissions of 604
Note: Make sure that your CGI scripts and the directories that your CGI scripts are in are not group or world writable, otherwise our server will not execute them.
For security reasons you should never make your files or directories world writable and you should have a good understanding of the security implications before you make any files or directories group writable. This is especially true of web-accessible files and directories.
Do the owner and group of my CGI script matter?
- The CGI script and the enclosing directory must be owned by you.
- The group of the file and the enclosing directory must be the same as your default group.
To find your default group use the
id command. The value returned for gid will be your default group. (number and name)
Failing to set the owner and group correctly will result in an Internal Server Error.
How can I tell if my CGI script is producing the correct output?
Run your script from the command line. Simply
cd to the directory containing the script, and type
<scriptname.cgi> is the name of your CGI script. The first line of output returned should be:
followed by a newline and then the output of your script. If the above line is not produced, or it's not followed by a blank line and then some content, an Internal Server Error will be displayed. Also, you might find that the script produces the following prompt when you run it:
(offline mode: enter name=value pairs on standard input)
If this happens, type control-D and the output of the script will be displayed.
Where can I find the Apache error logs?
Check the Apache Error Logs Page for information on where to find and how to access error logs.
What else can I do if I'm still getting Internal Server Errors?
Most CGI-related problems are caused by one of the above requirements not being met. Also, be sure to check the Apache Error Log to see what error message is being generated. If you have double-checked this page and you are still experiencing problems getting your scripts to run, contact Systems Staff.
What do I do if I get an error when I try to run a script on my Windows computer?
If you are trying to run a script on a Windows computer, you may get an error that looks similar to this:
[date][error][client] suexec failure: could not open log file
[date][error][client] fopen: Permission denied
[date][error][client] Premature end of script headers: ruby.cgi
First, Check to make sure you don't have any strange newlines. It could be that you have Windows newline characters that are causing problems. SSH to one of our Solaris machines and type the following command:
% dos2unix <<file>> <<file>>
This command will convert Windows newline characters to UNIX ones and clear up the error.