This web page shows how to setup CGI (Common Gateway Interface) using free perl scripts. It does not teach users to write perl scripts.
| Introduction: [Top] |
Note: Most ISP's (Internet Service Providers) do not allow CGI access at a 'fair' price - if at all. Therefore, you may want to use a small commercial web hosting company where prices are usually $5 to $10/month.
| General: [Top] |
| Step x Step: [Top] |
Caution: the SSI instructions for some scripts will not work. They may show the use of the #exec command. In other words, you may see instructions that tell you to use something like this: <!--#exec cgi="/cgi-bin/programname.cgi" -->. But it's very uncommon for a hosting company to allow '#exec' commands because of security risk. They often require you to use a 'virtual' include like this: <!--#include virtual="cgi-bin/programname.cgi"--> Then I hate to tell you but that sometimes creates yet another 'problem' with the original instructions. If the script writes or appends to a file or directory, you will more than likely have to set chmod 777 (wide open) for the file and/or the directory Summary: replace any '#exec cgi' statements with 'include virtual' unless your host allows #exec (most Do Not). Then if your script writes or appends to a text file, set the permission (wide open) for the file (and it's directory in some cases). Beware: chmod 777 is not secure so do not use it unless it's essential.
Notes: According to Murphy's Law there will inevitably be some kind of internal server error. Don't panic!! That just means that you are not perfect (yet :) You need to double check once again from the beginning. Here is a check list that may help:
Check:
| Example: [Top] |
| Closing Tips: [Top] |
I picked the counter.cgi example because it had errors that make you think :) This is what really frustrates new users i.e. having poorly written installation instructions and/or errors that were overlooked (probably during updates / modification). But it's a good example for a newbie because it shows things that typically go wrong. It makes you think about what the files are doing - makes you realize that the script has to 'increment' the counts.txt file so it must be accessible to any visitor - therefore - a chmod 755 does not always work so you try a chmod 777. Caution: I would never use a chmod 777 on a script (.cgi / .pl) file unless the author requires it. (very unlikely). But using a chmod 777 on a .txt file that just stores the count / data is not very dangerous i.e a hacker is not going to care about you count but one may like to modify a script running on your site. I'm beating a dead horse but be careful with chmod 777.
BTW: After you install this counter, you can add a text counter to all your pages (with an include statement). The counts.txt file will keep the counts for each individual page (cool).
Note: Just be sure that you pay attention to the path in the include statement i.e. the <!--#include virtual="/cgi-bin/counter.cgi"--> will work for all .shtml files (web pages) in the top / domain directory (www.yourdomain.com) but if you put a .shtml file in a subdirectory you will likely have to make the path statement 'backup' with something like this: <!--#include virtual="../cgi-bin/counter.cgi"--> Again - understanding pathnames and how they work is critical (and often a 'real killer' for newbies :( It just takes practice (and a lot of hair to pull out :)
Now for the bad news, counter.cgi was a simple script to set up. Most good scripts require you to set actual path information, including the path to your server directory, the path to the date, the path to sendmail. These are the things that drive most newbies crazy.
There are so many scripts (1000's) that have different requirements that it's impossible to cover all the path's and/or variables you will have to set. But once you learn how these things work then you'll just have to use the instructions from the 'author'
As an example of typical path settings - here are the essential settings from a flat file forum that I use on one host / server. Just so you can see the syntax / typical path info required by a big script.
(1) Specify the locations of your files:
$scripts_dir = "/home/username/www/webbbs";
$webbbs_basic = "$scripts_dir/webbbs_basic.pl";
.
.
etc.
## (2) Define your e-mail notification features:
$mailprog = '/usr/sbin/sendmail';
$admin_name = "Prince";
$maillist_address = "prince\@ibdprince.com";
$notification_address = "prince\@ibdprince.com";
.etc
Another set of examples:
#required changes:
$mailprog = '/usr/sbin/sendmail';
$my_name = 'Prince';
$my_email = 'prince@ibdprince.com';
$date = '/bin/date';
$logfile = "/home/username/public_html/accesslog.html";
Note: both of these examples have 'username' in place of a real username i.e. the username is very simple but some people forget to replace it with the user name that they use for their account. Silly little 'mental lapses' like this (or forgetting to upload files as ASCII (text files) or using a text editor that inserts a carriage return (enter) or not setting the correct permission for the 'config' script (the script that must execute) or not setting the correct permission for a file that must be 'written' to (such as the counts.txt). These various errors can drive a 'newbie' crazy so you often have to just recite a few choice word and/or throw a pillow at the wall to maintain your sanity :)
| Glossary: [Top] |
| CGI: [Top] |
An acronym for Common Gateway Interface, CGI is an interface that enables an Internet server to run external programs to perform a specific function. A plain HTML document that the Web daemon retrieves is static, which means it exists in a constant state: a text file that doesn't change. A CGI program, on the other hand, is executed in real-time, so that it can output dynamic information.
The actual programs generally consist of a set of instructions written in a programming language like C or PERL that process requests from a browser, execute a program and format the results in HTML so they can be displayed in the browser.
Summary: CGI, Common Gateway Interface is a way of interfacing computer programs with HTTP or WWW servers, so that a server can offer interactive sites instead of just static text and images
Note: While you hear many people talk about Perl and CGI almost interchangeably, they are not at all the same thing. The Common Gateway Interface (CGI) allows information to pass between a Web site's server and a program (written in Perl or another language) that lives in the "CGI bin" on that server. You send information from your browser to the server; the server passes the information through the CGI gateway to the Perl program; the Perl program does its thing to the information (perhaps conducting a query of a database and creating an HTML page that lists the results), then sends the information back through the CGI gateway to the server, and the server returns the product to your browser. And it all happens in a matter of milliseconds, if everything is running smoothly. Whew.
| CHMOD: [Top] |
Change Mode (chmod) is the Unix command and system call to change the access permissions of a file. In other words, change Mode (chmod) means to set permission.
The most common chmod command (for scripts) is 755 (rwx r-x r-x)See Table. It's not essential that you completely understand the meaning of the permissions. However, you must have a program / control panel / telnet client that will let you set the required permissions. The following table will help if your FTP programs does not allow numerical values.
| Owner | Group | Public | |
| read=4 | X | X | X |
| write=2 | X | ||
| execute=1 | X | X | X |
| Totals | (4+2+1)=7 | (4+1)= 5 | (4+1)=5 |
| config.pl [Top] |
Configuration (config) relates to the configuration files that normally set the variables in a perl script. As mentioned in the 'perl' section, the files are text files (ASCII / Dos text) files that must be edited in a text editor. It's just common practice for a perl programmers to name their configuration file as config.pl (or config.cgi). However, these names can be changed to better describe the 'script' e.g. the config.pl file for a calendar script is likely to be renamed to calendar.pl (or calendar.cgi).
| FTP [Top] |
This is an acronym for File Transfer Protocol -- a very common method of transferring one or more files from one computer to another (usually between a client and a host / server). It's an old standard / protocol with out dated syntax /commands. However, there are now many FTP programs that make it easy to transfer files without having to use these commands. WSFTP-LE is a popular free FTP program that will also allow you to set permissions (chmod).
| Host: [Top] |
The host is any computer directly connected to a network that provides such services as e-mail, newsgroups, FTP or World Wide Web. Host computer usually references the main computer that shares resources (data, files, images, programs, etc) with clients. While a client is the individual computer requesting the service from the host on the network.
A host and a server often mean the same thing in relation to a network (such as the Internet - the biggest network of them all :). A server is a computer that handles requests for data, e-mail, file transfers, and other network services from other computers (i.e., clients). The only real difference is that a host is typically a computer (or set of computers). While a server may reference a computer or it may reference (server) software running on a computer. In other words, you can have server software (such as an email server program) running on a computer with other software - but the email server functions like a email host (computer) to clients.
Sometimes a reference to 'the host' is really a reference to the 'hosting company' i.e. the company / owner of the host computer - the person(s) who must supply the path, setup information for the server / host computer. So a host can be a computer or the owner of the computer depending on how it's used in a sentence.
| Path [Top] |
The exact directions to a file, program, etc on a computer. These directions are usually described by means of the hierarchical filing system from the top down, stating the root (top) directory, directory, any sub-directories, the file itself, and its filename extension if it has one. The path is not the same thing as the URL (address / location to a browser). While the URL might be www.yourdomain.com, the path to the site on the host / server's hard-disk might be something like this: /home/username/www/
For perl/cgi scripts, a common requirement is that the path to your site must be specified. Therefore, the host (company) must provide you with the important path information.
A path is sometimes called a Pathname. Pathnames can be absolute or relative. An absolute pathname provides the full path (address) of a file, including the computer system, directories, and sub-directories (if any) it resides in e.g. C:\programs\hopscotch.html Relative pathnames are used to describe a file or directory location on the user's system relating to current position ./cgi-bin/easyscript.cgi
| Perl: [Top] |
Practical Extraction and Reporting Language (perl) is a scripting language. Perl, is a simple, free, text-processing programming language that pushes much of the Web's inter-activity.
Hint: The fact that PERL is text based means the method used to upload to the host/server is critical. You must upload perl/CGI scripts as text files (often referred to as ASCII or Dos text files). Most FTP programs allow you to set the options so all perl (pl) and CGI scripts are automatically uploaded as ASCII (otherwise expect to forget and unload some as binary files - then they will not execute. Believe me - it happens to best of us - but it sure is confusing to remember to use binary for images, etc but to change to ASCII for uploading CGI/perl scripts.
Note: While you hear many people talk about Perl and CGI almost interchangeably, they are not at all the same thing. The Common Gateway Interface (CGI) allows information to pass between a Web site's server and a program (written in Perl or another language) that lives in the "CGI bin" on that server. You send information from your browser to the server; the server passes the information through the CGI gateway to the Perl program; the Perl program does its thing to the information (perhaps conducting a query of a database and creating an HTML page that lists the results), then sends the information back through the CGI gateway to the server, and the server returns the product to your browser. And it all happens in a matter of milliseconds, if everything is running smoothly. Whew.
| shtml [Top] |
Server-Side Include Hypertext Markup Language (shtml) is the file extension for Web pages that contain server side includes (SSI). Servers have to be configured to use this extension or MIME type as a SSI. It is the filename extension that enables the Web server to differentiate those pages that need to be processed before they are sent to a Web browser. A Web file with the suffix of ".shtml" (rather than the usual ".html") indicates a file that includes some information that will be added "on the fly" by the server before it is sent to you.
| SSI [Top] |
Server Side Include (SSI) allows a HTML web page to include files from the server. Using server-side includes makes it unnecessary to include multiple copies of the same information in the HTML file, and make it easier to work with frequently-updated information.
A SSI is also a type of comment that directs the Web server to dynamically generate data for the Web page whenever it is requested. The basic format for SSIs are: <!--#command tag="value" --> (Where #command can be any of various commands).
The simplest command is #include, which inserts the contents of another file. This is especially useful for ensuring that components, such as a header (or footer, menu, etc) are the same on all pages throughout a Web site. For example, to change a header that appears on each page, you only need to modify the include file, instead of updating every individual Web page.
SSIs can also be used to execute programs and insert the results. In other words, you can use SSI to call CGI/perl scripts that display the results directly in the web page. One example of this would be using a SSI to call a counter.cgi script to increment/ display a visitor count.
| Text Editor [Top] |
A Text Editor is a program that enables you to create and edit text files. Much like any word processor, however not usually as powerful. The distinction between editors and word processors is not clear-cut, but in general, word processors provide many more formatting features. However, one important difference is how the 'files' are saved. A text editor should not insert a carriage return/enter at the end of each line i.e. the formatting options should be suppressed if you use a full featured word processor to edit config files for scripts. That is why it's best to use a pure ASCII text editor (such as Notepad, Wordpad) that allow you to save as a pure ASCII / Dos Text file. (Full word processors often have this option but it's difficult to remember to save as 'dos text' without ANY formatting commands.
BTW: The term HTML editor can refer to source code editors (such as Frontpage, Dreamweaver, Hot Dog, etc) that include many special features for writing and editing HTML source code.
| unzip [Top] |
To unzip is to return a zip file to its original size with a unzip program. Perl scripts are often zipped (compressed) to speed up the download. When zipped files reach their destination, they must be unzipped (uncompressed) before they can be used - changed in the text editor.
Updated: Wed, Jul 09, 2008
© 1995-2008