Home > Perl > Web programming > CGI Programming - Part 1
CGI Programming - Part 1
Written by Philip L Yuson   
Who is this for
This is for those who would like to have basic knowledge on CGI programming and requirements on running CGI scripts.

What you need to know
Perl programming
Basic Windows or Linux operation

CGI is an acronym for Common Gateway Interface. It is a method of passing data from a web browser to a database, document or other processes for processing. The result will also be displayed on the web browser. A CGI script is written in any language and Perl is used more often when coding CGI scripts.

Components needed to Make CGI work

CGI scripts reside in a server. A server is a program that manages incoming requests from clients. A client is another program that sends requests to the server for processing. The client can be on the same machine or in another machine over the network. The most popular server is the Apache Web Server. Apache has versions for Linux and Windows (Yes there is a Windows version). For Windows 98, you have the Personal Web Server (PWS). You can also run your CGI scripts in the PWS.

Setting up an Apache Server in Linux
If you are running Linux, you probably have an Apache distribution in your CD. If not, you can download it from the Apache Web site (http://www.apache.org/dist).Once you have downloaded the distribution, proceed to install it. To install, you have to configure the compiler options and then compile the source code. Once installed, you can configure it to process CGI Scripts.

Installing Apache in Windows
Like the Linux distribution, you can download the Apache Web server from their website (http://httpd.apache.org/dist/binaries/wi... Once downloaded, you can just run the downloaded file and it will install the Web Server for you. You do not have to change any of the configuration to run CGI scripts. The installation program will create an Apache Web Server folder in your Start->Programs folder. The items in the Apache Web Server folder allows you to start and shutdown the installed Apache server. Try starting the Apache Server. It should start a DOS Window and display this message:

Apache/1.3.14 (Win32) running...

You CGI scripts would, by default, be read from the C:\Apace\CGI-BIN directory.

Setting up the Personal Web Server in Windows
If you do not want to use Apache, you can use the Microsoft Personal Web Server (PWS) in Windows. PWS is in the Windows 98 CD and you can install it from there. Once installed, you have to do some tweaking to run Perl CGI scripts. Click Start->Run and then type regedit. This starts the Registry Editor. Follow through these folders:

Script Map

Select the following on the Top Menu:

Edit->New->String Value

This defines an entry at the right Window of the Registry Editor. Change this value to .pl. Then double-click on the entry you edited. This will bring up the Edit String window. Type this on the Value data field:

C:\perl\bin\perl.exe %s %s

Make sure that you specify the location of Perl in your system and type the %s as is. This is case sensitive. Create another entry for the .cgi extension.

Start the Personal Web Server and select the Advanced icon. This should display the Advanced options with a list of Virtual Directories.
Check if there is a /cgi-bin directory. if there is none, create one. If there is, double-click on it.
This displays an Edit Directory Window. Change the Directory to where you want to put your CGI scripts.
Recommended is C:\Inetpub\wwwroot\cgi-bin. Also check the Read, Execute and Scripts options. Then click the OK button.

Testing your first CGI Script
Create a file (call it test.cgi) in your CGI directory (wherever you specified it in PWS or Apache).


print "Content-type: text/html\n\n"; # should be as
print "Hello, world!";
print "<HR/>\n";

Note that the last two lines can be written as one line. I separated them because it looked like two separate lines when displayed in this article.

Start a browser and type this on the URL:


You should see

Hello, world!

on your browser.

Copyright: © 2017 Philip Yuson