Developing CGI Programs
Summary
If you are thinking about writing or installing a CGI program, this article provides the technical information you need to know about the local CGI programming environment. If you are unfamiliar with CGI programming, you might begin with An Introduction to CGI then return here for details.
Included on this page:
- Can I use CGI programs?
- Where should I put them?
- What filename extension should I use?
- What file permissions should I use?
- Do my CGI programs run under my userid?
- What security issues should I be aware of?
- Where should I develop my CGI programs?
- What programming languages can I use?
- Can I use an index.cgi program as my home page?
- What is the path to my Web directory?
- What do server errors mean?
- Can I look at the error_log?
- How can I control my CGI program processes?
Can I use CGI programs?
Yes, so long as you read this entire article.
Where should I put them?
Put them anywhere in your Web directory. There is no central "cgi-bin" directory.
What filename extension should I use?
Use a .cgi filename extension to designate a CGI program. Only files named with this filename extension are treated as a CGI programs.
What file permissions should I use?
If you can execute your CGI program, so can the Web server. The following command applies the recommended file permissions:
chmod 700 <filename.cgi>
Do my CGI programs run under my userid?
Yes, they run under your userid and count against your account's assets. Since they run as you, you do not have to loosen permissions in order to read and write to files through your CGI programs.
What security issues should I be aware of?
CGI programs provide a mechanism for passing information gathered from the Web into a program running under your userid. Be warned. If you use CGI programs within your Web directory, you may accidentally allow someone to snoop through, remove, or even modify the files in your Web directory. Be careful.
For information about writing secure CGI programs, see:
Note: for security reasons, a server error results if any portion of the path to a CGI program includes a symbolic link or is not owned by your account. Also, the directory containing a CGI program cannot be world writable.
Send email to help@u.washington.edu if you have any questions about writing secure CGI programs.
Where should I develop my CGI programs?
Use the Web development environment provided by UW Technology. To do so, log in to your Homer, Dante, or Shell.MyUW.net account, press the O key for "Other", then press the W key for "WebDev". This drops you into the Web development environment.
What programming languages can I use?
CGI programs can be written in any language supported within the Web development environment. This includes compiled programming languages, such as C and C++; interpreted languages, such as Perl, Python, Ruby, and the Bourne shell; and languages, such as Java, that lie somewhere in between. Here are some local nuances:
Perl (/usr/local/bin/perl): The CGI.pm and cgi-lib.pl libraries are included in the standard Perl library and can be used within CGI programs written in Perl. For specific information about Perl, refer to the Perl entry in the Software Guide.
C and C++: Compile CGI programs written in C and C++ while logged in to one of the Web development environments. They have the same operating system, Linux, as the corresponding Web servers. For specific information about compilers, refer to the C and C++ entries in the Software Guide.
Can I use an index.cgi program as my home page?
Yes. If the Web server doesn't find the regular home page files (index.html or index.htm) in a given directory, it looks for an index.cgi program to run instead.
What is the path to my Web directory?
The wwwhome command returns the full path to your Web directory and can be used within CGI programs to refer to the location of your Web directory. The need to do so often arises when a CGI program must read or write to other files in your Web directory.
Two examples follow, each demonstrating how the wwwhome command might be leveraged within a CGI program written in Perl. This first example uses wwwhome to determine the location of a data file:
#!/usr/local/bin/perl chop($wwwhome = `wwwhome`); open(DATA,"$wwwhome/data.txt"); ...
The next example uses wwwhome to add a new directory to the location where Perl libraries are found:
#!/usr/local/bin/perl
BEGIN { chop($wwwhome = `wwwhome`); }
use lib "$wwwhome/mypackage";
use MyPackage;
...
Note: You can hard code the location of your Web directory into a CGI program, but you run the risk of it breaking if your Web directory ever moves.
What do server errors mean?
If your CGI program produces a server error when it runs, it usually means something is wrong with it or the way it was installed in your web directory. For further ideas, see Debugging CGI Errors.
Can I look at the error_log?
Recent error_log entries can be gathered via a Web browser. Choose the appropriate link below and log in using the login name for which you want to gather error_log information.
- Get error_log entries for students.washington.edu
- Get error_log entries for faculty.washington.edu
- Get error_log entries for staff.washington.edu
- Get error_log entries for depts.washington.edu
- Get error_log entries for courses.washington.edu
To conserve disk space, UW Technology does not save error_log entries very long. In fact, all error_log entries are erased each night.
How can I control my CGI program processes?
The rps and rkill commands help you observe and control your CGI programs, particularly those that may have run away from you. These commands are like the ps and kill unix commands, only they allow you to observe and control processes on the the Web development environment and the applicable Web servers all at once.
Use rps to find a runaway process. For example:
> rps vergil01 UID PID TTY TIME CMD vergil01 54759 49380 pts/57 0:00 tcsh vergil01 54759 60192 pts/57 0:00 ps vergil01 54759 64656 pts/57 0:00 sed vergil01 54759 64690 pts/57 0:00 ksh boca01 UID PID TTY TIME CMD boca01 54759 18554 - 0:00 ps boca01 54759 23876 - 52:47 needswork.cgi boca02 UID PID TTY TIME CMD boca02 54759 37946 - 0:00 ps boca03 UID PID TTY TIME CMD boca03 54759 48642 - 0:00 ps
Notice that the needswork.cgi process has been running for 52 minutes. Anything more than 5 minutes here for a CGI program likely means that it has run away.
Use rkill to kill the runaway process. For example:
> rkill boca01 23876
See man ps and man kill for more information.
