Skip

Some CGI basics

The aim of this article is to set out some basic information on what you need, and what you typically need to do, to set up CGI programs on your web site. Some requirements will depend on your particular circumstances, so I can’t provide a “one size fits all” answer. But I can tell you what information you will need, and suggest where to get it.

I’m assuming you already have a web site — or you probably wouldn’t be reading this — and therefore you have access to a web server: maybe your own, maybe your employer’s, or more likely through an account with a web hosting company or an Internet Service Provider (ISP). You will also need:

A CGI directory

You need to be able to run CGI programs on your server. If you aren’t sure about this, consult your web host or ISP. CGI programs often need to be kept in a special directory in order to run, though this depends on the type of server and how it has been configured — some servers allow CGI programs to be run from any directory as long as the filename has a particular extension, such as .cgi. For a number of reasons, though, it’s better to keep all your CGI programs in one place.

The “special directory” is often named cgi-bin (especially on servers running some flavour of the Unix operating system), but it could be called anything, as long as it has been set up to run CGI programs. This is an important point: you can’t just create a directory on your server and call it cgi-bin, then expect any CGI programs you put in there to work. The server needs to be configured by the server administrator (your web host or ISP) so that it will permit CGI programs to be run from that directory.

The Perl interpreter

The programs in this section are written in Perl, the commonest language used for CGI programs. In order to run Perl programs, the Perl interpreter must be installed on your web server. If your server is a Unix-type beast, then the likelihood is that Perl is already installed; however you’ll find that many if not most Windows servers these days also have Perl installed. If in doubt, consult your web host or ISP.

If your web site is on a Unix-type server, you also need to know the path to Perl — in other words, where the Perl interpreter is actually located on the server. You will put this information into the CGI program itself, to tell the server where to find Perl. Often the path is /usr/bin/perl. Your web host or ISP will be able to give you this information.

If your site is on a Windows server, you probably don’t need to know the path to Perl. Most windows server software, such as Microsoft’s Internet Information Server, recognise programs with the extension .pl as Perl programs and will invoke the Perl interpreter automatically, without being specifically told where it is by the program. However, Apache for Windows server software needs to be told the path to Perl, just as it does in the Unix versison.

A way to install programs

Of course, you need to be able to upload programs to your web server. Presumably you are used to uploading files if you have a web site. Your are probably doing this by File Transfer Protocol (FTP) using an application like WS_FTP or CuteFTP. That will do just fine for uploading your Perl programs. There are a couple of points to note:

  1. Tricky things can happen with line endings in your program scripts when uploading files to your server, which can then give rise to problems when your program executes. Make sure you upload files in ASCII and not Binary mode. This is usually a very obvious option in FTP programs; consult the documentation or help file if you can’t see it.
  2. Depending on how your web server is configured, you may need to give your program filenames a particular extension. Generally Windows servers recognise files with an extension of .pl as a Perl program and will invoke the Perl interpreter. Unix servers may prefer the file to have the extension .cgi. This is how my server is set up, so all of the files in this section are supplied with the extension .cgi. Just rename the file if you have to; again, consult your web host or ISP about what is needed.
  3. Make sure you upload program files to the special CGI directory, if your server requires it.

Finally, if you are on a Unix-type host, then you will need to set (or “chmod”) the file permissions for your program files — consult your FTP program’s documentation for how to do this. File permissions are set for the owner (the person who wrote or controls the program, i.e. you), for the group to which the owner belongs, and finally for the user (everyone else who may access your web site and use the program). Each of these may be allowed to read, write, or execute the file. Normally you would only want the owner of a program file to be able to do all three, i.e. read, write and execute. Typically the group permissions will be the same as the user permissions; more often than not these are read and execute. Your FTP program may allow you to set permissions according to these descriptions of read, write and execute; alternatively it may need you to set them using a number. Here’s how it works:

  1. “Read” is represented by 4, “write” by 2, and “execute” by 1.
  2. To specify a permission, you add the numbers. For example: read, write and execute permission is 7 (4 + 2 + 1). Read and execute permission is 5 (4 + 1). Read and write permission is 6 (4 + 2).
  3. The overall file permissions are stated as a triplet of numbers in the order owner-group-user. So a file with permissions of 755 means the owner has read, write and execute permissions, while both the group and users have read and execute permissions.

This all sounds more complicated than it is in practice. The information for individual programs and any related files will tell you what permissions need to be set.

On Windows servers, you don’t need to set file permissions.

Of course, you might upload files and set permissions using Telnet. If so, I doubt you need any further explanation from me.

This page was last modified on Thursday, February 26, 2009

Top