Mod_perl 2, apache 2.2, perl handler example

  1. Make sure that mod_perl.pm is being loaded on server start-up! In httpd.conf, have a line like:
    LoadModule perl_module modules/mod_perl.so
  2. The perl that runs with the apache server needs to know where you are putting your stuff, along with any other perl-isms at startup, so we set a perl file that gets sourced by apache/perl when loading. In httpd.conf, add:
    PerlRequire /path/to/your/perl_startup.pl

    This file itself, can set several things, but at a minimum adds the module path for your perl modules to be referenced in:

    use lib qw(/path/to/your/perl_modules);
    1;
  3. Now set up code for each specific handler! For each mod_perl app you put out there, you need to map a location to the perl module, in httpd.conf, like so:
    <Location /modperl_rocks>
           SetHandler perl-script
           PerlResponseHandler MyApache2::Rocks
    </Location>
  4. Next the perl code itself! In the perl lib directory defined above, /path/to/your/perl_modules, create a file MyApache2/Rocks.pmand have it look something like:
    package MyApache2::Rocks;
    
    use strict;
    use warnings;
    
    use Apache2::RequestRec();
    use Apache2::RequestIO();
    use Apache2::Const -compile => qw(OK);
    
    sub handler {
        my ($r) = @_;
        $r->content_type('text/html');
        print "mod_perl 2.0.2 on apache 2.2.2 rocks!n";
        return Apache2::Const::OK;
    }
    
    1;
  5. Finally, stop and restart apache, and for this example surf to /modperl_rocks. All should work well…
Share and Enjoy:
  • Print
  • Facebook
  • Twitter
  • PDF

Leave a Reply

Your email address will not be published. Required fields are marked *