22
Building dynamic websites with Mod_Perl Kamal Nayan http:// tipstricksandhacking.blogspot

Building dynamic websites with Mod perl and apache

Embed Size (px)

Citation preview

Building dynamic websites with Mod_Perl

Kamal Nayanhttp://tipstricksandhacking.blogspot.in

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

Question & AnswersHTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

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 andruns on a number of platforms. Although initially designed for the UNIX operating system.

Perl is interpreted language which is parsed and executed at run timeInstead of being compiled into binary form and then run.

HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

• 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

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

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/html\n\n"; print "Hello, I am kamal nayan";

HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

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

Why Use Mod_Perl As sites grow, their content usually becomesmore dynamic Sites ported to mod_perl show requestreturn rates 200% to 2000% higher Generally the more processing of content, theslower the page return

HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

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

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 substitutes HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

Evolution of a Web-sitePerlRun

How to execute the Perlinterpreter

Perl Registry

This module creates an object oriented interface to registry. Itallows you to read & update local as well as remote registry.

The Apache request cycle

HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

Web Server working diagram

/home/sulabh/Desktop/webserver.jpg

HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

Quick way to install Mod_Perl:

For CentOS :yum install mod_perl -yrpm -qa|grep -i mod_perl

For Ubuntu :apt-get install libapache2-mod-perl2dpkg -l|grep mod-perl2

How to install Mod_Perl

HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

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

Mod_Perl ConfigurationSpecific File type :

<Files *.mp>

SetHandler perl-script

PerlResponseHandler ModPerl::Registry

PerlOptions +ParseHeaders

Options +ExecCGI

</Files>

Specific File :

<Files "^/cgi/get_captcha.cgi">

SetHandler perl-script

PerlResponseHandler ModPerl::Registry

PerlOptions +ParseHeaders

Options +ExecCGI

</Files> HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

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

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

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

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

Further ReadingCPAN

http://www.cpan.org/

Mod_Perl Guide

http://perl.apache.org/guideStas Bekman

FAQhttp://perl.apache.org/faq Frank Cringle

HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN

HTTP://TIPSTRICKSANDHACKING.BLOGSPOT.IN