- CS&E Wiki
- Course Resources
- Hardware and Media
- Network and Wireless
- Offsite Access
- Web Development
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.7/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>" 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.14.1/bin/perl -w use CGI; $cgi = new CGI(); print $cgi->header(); print '<?xml version="1.0" encoding="UTF-8"?>'; print '<!DOCTYPE html> <html> <head> <title>Perl CGI test</title> </head> <body>'; print ' <p> Hello world! </p>'; print ' </body> </html> ';
#!/soft/ruby/1.9.2/SunOS5.10/bin/ruby puts "Content-type: text/html" puts 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>"
#!/bin/bash echo "Content-type: text/html" echo "" echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE html>"; <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:
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."