Tuesday, February 1, 2011

Configuring apache2 to handle perl and cgi scripts

Hi All,

This post explains how to handle perl and cgi scripts in your apache server.

Refer to my older posts to see how to configure apache server.

Now getting back,

Follow these steps:

1. cd /var/ww/
2. sudo mkdir public_html

Refer back to my older post and copy the perl script from there.

3. sudo cp a.perl public_html

Now open  your browser and type

This should open your perl file as is.

Next to configure your cgi script

1. cd /var/www/public_html
2. sudo mkdir cgi-bin

Copy this cgi script below:


# hello.pl -- my first perl script!

print "Content-type: text/html\n\n";

print "Hello, world!\n";
Paste this above to a file a.cgi and

3. sudo cp a.cgi cgi-bin

4. Next issue this in your browser:


If you are able to see your cgi rendered html - then you are lucky. 

Or else you need to peek around...


1. Apache provides 2 commands: a2ensite and a2dissite
2. See the manual pages for the same - which would apache 2 enable site and apache 2 disable a site
3. See the log files from /var/log/apache/error.log
4. See the /etc/apache2/sites-available/default file
5. You need to change that file to enable cgi scripting
6. See for this line:
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
7. This particular ScriptAlias line tells us that it has a fake name and a real name. 
And above cgi-bin for the server means the path /usr/lib/cgi-bin
The server would search for any cgi scripts only in that path. 
So now if you copy paste your a.cgi to /usr/lib/cgi-bin - then it would work 
8. But you can change the same line to render your cgi scripts from your public_html path as follows:
ScriptAlias /cgi-bin/ /var/www/public_html/cgi-bin/

After this - re-enable your site by giving the command

sudo a2ensite default

next re load or restart apache 

sudo apache2ctl restart

Now if you give the path http://localhost/cgi-bin/a.cgi 

You will find the rendered html page. 

Ananth S Gouri

No comments:

Post a Comment