home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


CGI Programming on the World Wide Web

Previous Appendix A
Perl CGI Programming FAQ
Next
 

A.3 CGI and the WWW Server

Where does my Perl CGI program have to live to execute? What is the cgi-bin directory for?

The server is generally configured so that it executes CGI scripts that are located in the cgi-bin directory. However, the server administrator can set up aliases in the server configuration files, so that scripts with certain extensions (i.e., .cgi, .pl) can also be executed.

What are file access permissions? How do I change them?

File permissions allow read, write, and execute access to users based on their user identification (also known as UID), and their membership in certain groups. You can use the command chmod to change a file's permissions. Here is an example:

% ls -ls form.cgi
  1 -rwx------  1 shishir       974 Oct 31 22:15 form.cgi*

This has a permission of 0700 (octal), which means that no one (besides the owner) can read to, write from, or execute this file. Let's use the chmod command to change the permissions:

% chmod 755 form.cgi
% ls -ls form.cgi
  1 -rwxr-xr-x  1 shishir       974 Oct 31 22:15 form.cgi*

This changes the permissions so that users in the same group as "shishir," as well as all other users, have the permission to read from and execute this file.

See the manpages for the chmod command for a full explanation of the various octal codes.

Where should Perl be installed so I can execute it?

Perl can be installed anywhere on the system! The only thing you have to ensure is that the server is not running in a chroot-ed environment, and that it can access the interpreter. In other words, system administrators can change the root directory, so that "/" does not point to the actual root ("/"), but to another directory.

What should I do when I get a "Server: Error 500" message?

You can get a server error for the following reasons:

  • If the script does not contain the "#!/usr/local/bin/perl" header line that points to the Perl interpreter, or if the path to the interpreter is invalid.

  • If the first line output from the script is not a valid HTTP header (i.e., "Content-type: text/html"), or if there is no blank line after the header data.

I try to open a file for writing so I can save my data, but the open ( ) command fails. What's going on?

Generally, the HTTP server will be running as user "nobody," or "www," or some other user ID that has minimal privileges. As a result, the directory (where you intend to create the file) must be writeable by this process ID.

To be on the safe side, always check the return status from the open ( ) command to see if it was a success:

open (FILE, "/abc/data.txt") ||
     &error ("Could not open file /abc/data.txt");
.
.
.
sub error {
    local ($message) = @_;
    print "Content-type: text/html", "\n";
    print "Status: 500 CGI Error", "\n\n";
    print "<TITLE>CGI Error </TITLE>", "\n";
    print "< H1>Oops! Error </H1>", "\n";
    print "< HR>", $message, "< HR>", "\n";
}


Previous Home Next
Modules Book Index Specific Programming Questions

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell