Sunday 11 May 2014

Run A Simple Perl Script on Apache2

Step 1: Preparation
Install Apache2 in Ubuntu
sudo aptitude install apache2
This will complete the installation.
After installation Type the server’s IP address (or alias if you added the server to your /etc/hosts file) in your browser’s address bar or, if you are browsing on the server itself, type 127.0.0.1 or localhost. If an error occurs, then you will have to edit the apache2.conf file to ensure that Apache can fully resolve the server’s name. If you have any problem then you have to edit the apache2 configuration file using the following command
sudo nano /etc/apache2/apache2.conf
Add the following line somewhere
ServerName 10.1.1.64
Change default document root in Apache2
The main configuration file located at /etc/apache2/apche2.conf.If you want to change the default document root you need to edit the /etc/apache2/sites-available/default file and look for this line “DocumentRoot /var/www/” here you can change where ever you want to change.For example if you want to change /home/perl_test/form/the above line looks like this “DocumentRoot /home/perl_test/form/”.
Save and exit the file
Now you need to  restart Apache server using the following command.
sudo apache2ctl restart

Enable CGI and perl support for apache2 server
You need to install the following package

sudo aptitude install libapache2-mod-perl2

Configure a cgi-bin directory

You need to create a cgi-bin directory using the following command

sudo mkdir /home/perl_test/form/cgi-bin

Configuring Apache to allow CGI program execution is pretty easy. Create a directory to be used for CGI programs and add the following to the site configuration file (again between the <VirtualHost> tags).
       ScriptAlias /cgi-bin/ /home/perl_test/form/cgi-bin/
       <Directory "/home/perl_test/form/cgi-bin">
               AllowOverride None
               AddHandler cgi-script cgi pl
               Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
               Order allow,deny
               Allow from all
       </Directory>

# enable mod_perl
<Files ~ "\.(pl|cgi)$">
      SetHandler perl-script
      PerlResponseHandler ModPerl::Registry
      Options +ExecCGI
      PerlSendHeader On
</Files>
The first line creates an alias that points to the directory in which CGI scripts are stored. The final line tells Apache that only files that end with the *.cgi and *.pl extensions should be considered CGI programs and executed.
Test your Perl Program

cd /home/perl_test/form/cgi-bin
sudo nano perltest.pl
#!/usr/bin/perl -w
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";
for ($i=0; $i<10; $i++)

{
print $i."<br />";
}

Copy and paste the following section save and exit the file.

Finish basic config




Step 2:
Build a form for post values to perl script
<head></head>
<body>
<b>Please create your order!</b><br>
<form action = "http://10.1.1.64/script.cgi" method =post>
Name: <input type=text name=name size=25 maxlength=100>
<br>
Menu:<br>
<input type=checkbox name=burger value=yes>Burger
<input type=checkbox name=fries value=yes>Coke
<input type=checkbox name=coke value=yes>Coke
<input type=checkbox name=coffee value=yes>Coffee
<input type=checkbox name=tea value=yes>Tea
<br>
Payment Method:<br> <select name=payment>
<option> ---- Select ----</option>
<option value="Account"> Account </option>
<option value="Cash"> Cash </option>
<option value="Cheque"> Cheque </option>
<option value="Credit Card"> Credit Card </option>
</select><br>
First Time Customer?
<input type=radio name=firsttime value="yes">Yes
<input type=radio name=firsttime value="no">no<br>
<br>
Comments:<br>
<textarea wrap=virtual name="comments" cols=25 rows=3></textarea><br>
<input type=hidden name=store value="Smith Street">
<input type=submit value="Place Order">


</body>
</html>

Built a perl script to process posted value.
#!/usr/bin/perl -w

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

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $FORM{$name} = $value;
}

#http://www.scriptsocket.com/code.php

$Name = $FORM{'name'};
$Burger = $FORM{'burger'};
$Fries = $FORM{'fries'};
$Coke = $FORM{'coke'};
$Coffee = $FORM{'coffee'};
$Tea = $FORM{'tea'};
$Payment = $FORM{'payment'};
$FirstTime = $FORM{'firsttime'};
$Comments = $FORM{'comments'};
$Store = $FORM{'store'};


print <<EOF;
Thankyou $Name!<br>
Here is your order.<br>
<br>
Burger: $Burger<br>
Fries: $Fries<br>
Coke: $Coke<br>
Coffee: $Coffee<br>
Tea: $Tea<br>
<br>
Payment: $Payment<br>
<br>
First Time Customer: $FirstTime<br>
Comments: $Comments<br>
<br>
$Store
EOF


exit;

Verify simple script
   

No comments:

Post a Comment