CGI and Perl
Introduction to CGI
CGI is short for "Common Gateway Interface", a protocol
which allows programs which are run on a web server to
communicate with visitors' browsers. CGI programs allow you to
make your site more dynamic and interactive by including such
features as counters, guest books and discussion
boards.
Most CGI programs are written in a language called Perl, but
CGI programs can in theory be written in any programming
language. The Efficient Hosting servers support CGI scripts
written in Perl, Python, C, PHP and the common Linux
shells.
For more information about CGI and Perl, or to view archives
of third party CGI scripts, you may follow the links to
suggested CGI resources below.
Uploading CGI
Scripts
After uploading, the file permissions for any CGI scripts
should be set to 755, in order to make them
executable.
For many CGI scripts it is also important that the
permissions are set correctly for any files or folders which
they need to access. For example, if a CGI script needs to
write to a log file, then this log file will need to have write
access enabled and the folder where it is located will need to
give "execute" permission. If a CGI script needs to create,
delete or rename files, then it will need "write" permission
for the folder in which this is to be done.
For third party scripts, the documentation provided by the
author usually gives specific details of the permissions
required. For instance, the instructions may direct you to
"chmod 755" a file.
Each number represents the sum of the values assigned for
Read, Write, and Execute permission:
Read - 4
Write - 2
Execute - 1
Adding the numbers for each of the three permission groups
gives you the setting.
In our example, the owner is granted read, write and execute
permission (7=4+2+1), whilst both the group and others are
granted read and execute permission (5=4+1).
The read, write and execute permission settings have
different meanings for folders than they do for files.
For a folder: Read permission is required in order to list the
files it contains.
Write permission is required in order to create, delete or
rename files within it.
Execute permission is required in order to open files within it
for reading and/or writing.
File permissions can be modified by most popular FTP
programs, including WS_FTP and CuteFTP.
You can also check and update file permissions using the
File Manager within your Control Panel. The permissions are
displayed alongside all files and folders and can be changed by
first clicking on the appropriate file name and then clicking
"Change Permissions".
Useful Paths
Path to your "CGI-Bin" folder
/home/username/public_html/cgi-bin
Web Address of your "CGI-Bin" folder
http://www.yourdomain.com/cgi-bin
Web address of global "CGI-Bin" folder
http://yourdomain.com/cgi-sys
Path to Sendmail: /usr/sbin/sendmail
Path to Perl: /usr/local/bin/perl (For
convenience, /usr/bin/perl may also be used, so all Perl
scripts should start either with the line #!/usr/local/bin/perl
or with the line #!/usr/bin/perl
Troubleshooting CGI Scripts
If your script doesn't work, please make sure that the
Permissions are set right for both the scripts themselves, and
any files or folders they need to access.
It is also worth double checking that all Paths are set
correctly, and that all CGI scripts were uploaded as the
correct File Type (usually "ASCII Text").
Another common problem is an output format error. Any output
from a CGI program intended for a visitor's browser must be
preceded by a valid "HTTP header". For example, if you are
generating an HTML page, the first print statement that your
program executes must be: print "Content-type:
text/html\n\n";
Also, it is best to ensure that the very last line of a CGI
script has a newline at the end. If not, it may not get
processed when you run the script.
It can be a good idea to check your CGI scripts for syntax
errors before uploading them to your site. To do this, you will
need to download Perl from www.perl.com and install it on your
local machine. You can then type perl -c your_program.cgi in a
DOS box to check your program for syntax errors.
You may also be able to learn more about why a CGI script is
failing to work correctly by checking the Error Log in the Web
Statistics section of your Control Panel.
Further Reading and Related Links
CGI RESOURCES
CGI DIRECTORY
The Perl Homepage
|