My Tech Scrap


Sunday, 10 January 2010

Setting Up Perl and CGI For Wamp Server WAMP(P)

I work on Perl and CGI recently for my college project and so I decided to write a few steps on setting up Perl and CGI with Wamp Server. I hope this might be useful.
First you need to download Wamp Server from and install Wamp Server on your machine. The default installation directory is ‘C:\wamp” and here I am using the default options for installation. To complete the installation you have to set the host name for your mail server and your email address, here you can leave the default option again. That will do no harm.
The current Wamp Server will install Apache 2.2.11, PHP 5.2.9-2 + PECL, MySQL 5.1.33, SQLitemanager and PhpMyadmin.
Now you have to download ActivePerl (currently 5.10.0) from and install it. The default installation directory is “C:\Perl“, but for simplicity and ease of use I use different directory. I create a new folder name “perl” inside “C:\wamp\bin“. So I install Active Perl in “C:\wamp\bin\perl” directory. The next thing you need to do is configure the Apache web server to execute Perl and CGI script.
This is the most important part here. You need to edit the Apache configuration file. Now go to “C:\wamp\bin\apache\Apache2.2.11\conf” directory and open “httpd.conf” file. Edit the httpd.conf file as below.
1. Inside httpd.conf, look for the line that says ““, just a few lines below this you’ll find the line that says “Options Indexes FollowSymLinks“. Add “Includes ExecCGI” in the line just next to FollowSymLinks, thus it look like this
  1. Options Indexes FollowSymLinks Includes ExecCGI  
This will enable CGI script inside your www folder.
2. Now look for the line “AddHandler cgi-script .cgi“, this line is commented out. You need to enable this by un-comment this line, to do that remove the # character at the beginning of this line. This will add handler for files with .cgi extension. If you want to use .pl file extension in your server add “AddHandler cgi-script .pl” just below the above line. Now you will be able to execute CGI and Perl script with .cgi and .pl, extension.
Lines to add
  1. AddHandler cgi-script .cgi  
  2. AddHandler cgi-script .pl  
3. To add directory index file, look for the line “DirectoryIndex index.php index.php3 index.html index.htm“. Add index.cgi and in this line.
Lines to add
  1. DirectoryIndex index.php index.php3 index.html index.htm index.cgi  
Alternative: If you do not want to waste your time doing the above 3 steps, you can download the edited configuration file httpd.conf here. Replace the one inside your apache directory with this one.
Your server is now configured and ready to run perl and cgi script. Next thing you might need to do is to configure perl to use mysql database. You need to download and install mysql driver to enable database connection through your perl script. You have to grab the driver from the ActivePerl package repository. However, mysql driver module is not available in the default ActivePerl Package Repository. So, you need to add additional repository and install from that repository. Follow the steps below:
1. Open command prompt [type cmd in run], then type “ppm repo add uwinnipeg” and press enter.
2. After the “uwinnipeg” repository is added successfully, you can install DBD-mysql by typing this command “ppm install DBD-mysql” and hit enter.
Note: The ActivePerl default package repository contains DBD-mysqlPP module. If you install that one, you will get an error in your SQL SELECT query and especially when you use the WHERE clause. Your localhost will hang if you run this kind of query with the WHERE clause, so to get it work you need to install the package that I mentioned above only.

For testing purpose , I have given some test codes below :
you need to create 2 files inside a new folder under www directory of wamp :
  • cgi_form.html
  • backatcha.cgi
then run the cgi_form.html under wamp server.

Example file :
Create a file named cgi_form.html

            What is your favorite color?                                
Create a file named backatcha.cgi

use 5.010;
use CGI;

use strict;
use warnings;

my $q = CGI->new();
say $q->header(), $q->start_html();

say "


"; for my $param ($q->param()) { my $safe_param = $q->escapeHTML($param); say "$safe_param: "; for my $value ($q->param($param)) { say $q->escapeHTML($value); } say ' '; } say $q->end_html();
then , run the wamp server ,
and open the cgi_form.html
(  for example :   http://localhost/cgi/cgi_form.html )