First Step With mod_perl

Feeling nostalgia about HTTP (RFC2616) and CGI (RFC3875), I was thinking of looking into mod_python, which, unfortunately not updated since 2013. Instead of mod_php, I went for mod_perl. Not something to fancy about (well, it's a legacy technology anyway despite Perl 6 was recently released) but nevertheless, better than revisit mod_php again.

What is all these mod_<insert language>? Well, these are Apache module for a particular programming language. In other words, mod_<insert language> allows a programming language interpreter to be embedded into Apache web server.

Such approach have one significant benefit. Speed.

Since the Perl interpreter is embedded into Apache, there is no need to forward each HTTP request to an external program, thus reducing the overhead. Furthermore, upon starting the web server, all Perl code are compiled and loaded once. Unless there are changes to the code, all HTTP requests are handled by pre-compiled cached code. Furthermore, integration with web server allows Perl code to access and modify the HTTP requests.

We proceed with the installation. Note that this was done in Ubuntu 14.04 using LXD 2.0.
$ sudo apt-get install apache2 libapache2-mod-perl2
$ sudo a2enmod perl
$ sudo service apache2 restart

To check that mod_perl has been installed successfully. The quickest way is to check the Apache's server signature in the error log file.
$ grep mod_perl /var/log/apache2/error.log | tail -1
[Sat Apr 09 05:27:11.726824 2016] [mpm_event:notice] [pid 2057:tid 140441217996672] 
AH00489: Apache/2.4.7 (Ubuntu) mod_perl/2.0.8 Perl/v5.18.2 configured -- resuming normal operations

Another way, where we want the more configuration details of the installed mod_perl (similar to PHP's phpinfo) is to use Apache2::Status.

First, create the required configuration file.
$ cat /etc/apache2/conf-available/perl-status.conf 
<Location /perl-status>
    SetHandler perl-script
    PerlResponseHandler Apache2::Status

    # disallow public access
    Order Deny,Allow
    Deny from all

    # change to 'Allow from all' if you want to load this page other than localhost
    # not advisable for production machine
    Allow from 127.0.0.1
</Location>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Then, enable the configuration and restart Apache web server.
$ sudo a2enconf perl-status
$ sudo service apache2 restart

Quick check using the w3m text-based browser. Note that w3m is using Vi-like key bindings. To go back to previous page, press 'Shift+b'.
$ w3m localhost/perl-status
Embedded Perl version v5.18.2 for Apache/2.4.7 (Ubuntu) process 2285,
running since Sat Apr 9 05:27:11 2016

Environment
Loaded Modules
Inheritance Tree
ISA Tree
Perl Configuration
Compiled Registry Scripts
PerlRequire'd Files
Signal Handlers
Symbol Table Dump

No comments:

Post a Comment