Introduction to CGI

CGI stands for "Common Gateway Interface," a fancy name meaning computer programs running on the web server that can be invoked from a www page at the browser. The "bin" part alludes to the binary executables that result from compiled or assembled programs. It is a bit misleading because cgi's can also be Unix shell scripts or interpreted languages like Perl. CGI scripts need to be saved in ASCII format and uploaded to your server's cgi-bin in ASCII or text format. This is very important.


Where to Put CGI Scripts
Put your cgi-bin scripts in the public_html subdirectory named "cgi-bin".


Paths to Date, Mail, Perl, etc.

Here are your paths to the common server resources that CGI scripts often require:


Permissions - Definitions and Overview

In order for your CGI program to run, it must be executable by the server. Therefore, you must set the permissions so the server can read or execute your files, which usually means making your CGI programs world executable. The way you set your file permissions is by using the CHMOD commands.


The CHMOD's permission mask is a three-digit number. Each digit of the number defines the permission for a different user of the file. The first digit defines the permission for the owner, the second digit defines the permissions for the group and the third digit defines the permissions for everyone else. Each digit works the same for each group of users: the owner, group, and public. What you set for one digit has no effect on the other two digits.

Owner = the files users (you)
Group = the files group
Others = others


Permissions Definitions:

r = read access
x = execute access
w = write access


Numerical Definitions:

r = 4
x = 2
w = 1


You will come to recognize, if you do not already, Chmod as a word used for changing Permissions from within SSH or your FTP client.


Some scripts will tell you to chmod 775 (for example). When using the numeric system, the code for permissions is as follows:

4 + 2 + 1 (rwx) = 7


The first number applies to Owner, the second number applies to Group, and the third number applies to Others. Therefore the first 7 of the chmod 775 tells Unix to change the Owner's permissions to rwx (because r=4 + w=2 + x=1 adds up to 7, this giving the Owner Read, Write, and Execute Permission. The second 7 applies to the group, this giving the Group Read, Write, and Execute Permission, and the last number 5, refers to Others (4 + 1= 5), giving Others only Read and Execute Permission. The permissions for chmod 775 look like this: rwx rwx -rx.


Permissions are always broken up into three groups of letters, however if there is a dash, this dash simply means that Permission wasn't given for that particular function, for example in the chmod 775, Permission to Write was not given to Others.


Remember: the first 3 letters always apply to Owner, the second 3 apply to Group, and the third 3 apply to Others.


Setting Permissions

How do I set permissions for my cgi scripts?

There are three different ways to set permissions for your files and directories within your account.



Setting Permissions Using Your File Manager:

Log into your cPanel and then click on File Manager. You will now see a list of directories and files within the root of your account.

CGI-BIN

Right click on the 0755 or whatever permission is set, and select Change Permissions.

Set your permissions and save changes.


Other FTP programs all have ways to change your permissions for your directories and files once you are in the public_html directory.


Troubleshooting CGI-bin Problems

Below are solutions to some of the more common CGI script problems.


When I activate my CGI program, I get back a page that says "Internal Server Error. The server encountered an internal error or mis-configuration and was unable to complete your request."

This is generally caused by a problem within the script. Check your script settings again to see that you have entered the correct server information and have set the correct permissions for the script. If this information is correct, you'll need to contact whoever wrote or is distributing the script for further assistance.


I am being told "File Not Found," or "No Such File or Directory."

Upload your Perl or CGI scripts in ASCII mode, not binary mode.


CGI Related Sites

The Common Gateway Interface
NCSA provides much helpful information about CGI here (along with a good introduction to forms.)

Matt's Script Archive
If you just want good scripts without having to write them, try here. Some modifications are usually necessary, which Matt explains in his readme files.

The CGI Resource Index
Plenty of free and fee based based scripts. Find everything from programmers to script ratings, good site.