Building dynamic websites with Mod perl and apache
1. Building dynamic websites with
Mod_Perl
Building dynamic websites with
Mod_Perl
Kamal Nayan
http://tipstricksandhacking.blogspot.in
2. Topics covered
What is Perl
What Web Server do
What is CGI
Perl CGI
What is Mod_Perl
Why use Mod_Perl
Difference between CGI and Mod_Perl
Evolution of website
How Mod_Perl works with Apache
The Apache request cycle
Web Server working diagram
How to install Mod_Perl
Mod_Perl Configuration
How to Check Version
Mod_Perl Handler
Further Reading
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
3. What is Perl
Perl is an all-purpose, open source (free software) interpreted
language maintained and enhanced by a core development team
called the Perl Porters. It is used primarily as a scripting language and
runs on a number of platforms. Although initially designed for the UNIX
operating system.
Perl is interpreted language which is parsed and executed at run time
Instead of being compiled into binary form and then run.
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
4. • In response to a Web client request
(e.g., http://google.com) a Web server:
Accepts network connection
Parses the request (index.html)
Reads file from disk or runs a dynamic content
generator
Sends content (headers and body) back
What Web Servers Do
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
5. What is CGI
Common Gateway Interface, which is just an interface
between browsers and servers
CGI provides a standard way a browser can call, pass data to
and receive response from programs on server
CGI is not a Perl-specific concept. Almost any language can
produce CGI programs but Perl has a very nice interface to
creating CGI methods
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
6. Perl CGI
Perl is a simple programming language. it's popular for use
on the web. When it's used on the web the programs are
called Perl CGI, because CGI is the way that Perl talks to
your web browser.
e.g.
#!/usr/bin/perl -w
print "Content-type: text/htmlnn";
print "Hello, I am kamal nayan";
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
7. What is Mod_Perl?
The fast way to create server side Perl
scripts that replace the need for the
Common Gateway Interface.
Open-Source interface between Apache
and Perl
Apache handlers can be written in Perl
One-time parsing/compilation
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
8. Why Use Mod_Perl
As sites grow, their content usually becomes
more dynamic
Sites ported to mod_perl show request
return rates 200% to 2000% higher
Generally the more processing of content, the
slower the page return
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
9. The main difference between Mod_Perl and Perl scripts
running as CGIs is that Mod_Perl compiles Perl modules
and scripts the first time they are run/used, and keeps them
in memory, ready to be executed in the next request
A Perl script run as CGI is compiled every time it is run
Mod_Perl can be used also to create Apache modules in
Perl, that will completely handle the requests as if a normal
Apache module
Difference between CGI and Mod_Perl
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
10. How Mod_Perl works with Apache
Mod_Perl allows you to interact with server and
directly alter server behavior
Gives you the ability to "program within
Apache's
framework instead of around it"
Allows you to intercept basic Apache functions
and replace them with your own Perl
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
11. Evolution of a Web-site
PerlRun
How to execute the Perl
interpreter
Perl Registry
This module creates an object
oriented interface to registry. It
allows you to read & update
local as well as remote registry.
13. Web Server working diagram
/home/sulabh/Desktop/webserver.jpg
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
14. Quick way to install Mod_Perl:
For CentOS :
yum install mod_perl -y
rpm -qa|grep -i mod_perl
For Ubuntu :
apt-get install libapache2-mod-perl2
dpkg -l|grep mod-perl2
How to install Mod_Perl
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
15. How to install mod_perl
Basic steps to install Mod_Perl:
$ lwp-download http://www.apache.org/dist/httpd/httpd-2.4.9.tar.gz
$ lwp-download http://apache.org/dist/perl/mod_perl-2.0.8.tar.gz
$ tar xzvf httpd-2.4.9.tar.gz
$ tar xzvf mod_perl-2.0.8.tar.gz
$ cd mod_perl-2.0.8
$ perl Makefile.PL –with-apxs=<Path of APXS>
e.g. /usr/local/apache_back/bin/apxs
$ make && make test && make install
$ cd ../httpd-2.4.9
$ make install
LoadModule perl_module modules/mod_perl.so
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
17. How to Check Version
To check Apache version
/usr/local/apache/bin/httpd -V
To check Perl version
$perl -v
To check Mod_Perl version
$perl -Mmod_perl 999 or perl -Mmod_perl2 999
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
18. Mod_Perl Handler
Handlers are simply perl subroutines called by the server at
various stages of the HTTP request cycle. Handlers, are
typically created as a perl modules stored in a library, and
accessible via perl's @INC array.
For example, here's an example that returns a greeting and the
current local time.
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
19. File:My/Greeting.pm
package My::Greeting;
use strict;
use Apache::Constants qw(OK);
sub handler
{
my $r = shift;
my $now = scalar localtime;
my $server_name = $r->server->server_hostname;
$r->send_http_header('text/plain');
print "Thanks for visiting $server_name.n";
print "The local time is $now.";
return OK;
}
1;
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN
20. We have to save the above file in perl library (e.g. My/Greeting.pm).
Now we have to write below given code in httpd.conf, to return the
above greeting when the URL /hello is visited on your server.
<Location /hello>
SetHandler perl-script
PerlHandler My::Greeting
</Location>
HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN