DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 2.4 Installing mod_perl on a Unixish System

Problem

You want to install the mod_perl scripting module to allow better Perl script performance and easy integration with the web server.

Solution

Download and unpack the mod_perl source package from http://perl.apache.org/. Then use the following command:

% perl Makefile.PL \ 
>     USE_APXS=1 \ 
>     WITH_APXS= /usr/local/apache/bin/apxs \ 
>     EVERYTHING=1 \ 
>     PERL_USELARGEFILES=0 
% make 
% make install 

Restart your server.

Discussion

mod_perl is quite a complex module, and there are several different ways to add it to your server. This recipe is the fastest and lowest-impact one; if it doesn't suit your needs, check the various README.* files in the package directory after unpacking. Because its primary language is Perl rather than C, the installation instructions are significantly different from those for most other modules.

Once you have restarted your server successfully, mod_perl should be available and configured as part of it. You can test it by making some changes to the httpd.conf file, adding a few scripts, and seeing whether the server processes them correctly. Here is a sample set of steps to test mod_perl's operation.

  1. Create a directory where your mod_perl scripts can live:

    # cd  ServerRoot 
    # mkdir lib lib/perl lib/perl/Apache 
  2. Create a file named startup.pl in your server's conf/ directory that will give mod_perl some startup instructions:

    #! /usr/bin/perl
    BEGIN {
        use Apache (  );
        use lib Apache->server_root_relative('lib/perl');
    }
    use Apache::Registry (  );
    use Apache::Constants (  );
    use CGI qw(-compile :all);
    use CGI::Carp (  );
    1;
  3. Next, create the lib/perl/Apache/HelloWorld.pm file that will be used for our test:

    package Apache::HelloWorld;
    use strict;
    use Apache::Constants qw(:common);
    sub handler {
        my $r = shift;
        $r->content_type('text/plain; charset=ISO-8859-1');
        $r->send_http_header;
        $r->print("Hello, world!  Love, mod_perl.\n");
        return OK;
    }
    1;
  4. Next, edit the server's configuration file to add the directives that will enable mod_perl to locate all the pieces it needs, and tell it when to invoke the test script. Add the following lines to the httpd.conf file:

    <IfModule mod_perl.c>
        PerlRequire conf/startup.pl
        <Location /mod_perl/howdy>
            SetHandler perl-script
            PerlHandler Apache::HelloWorld
        </Location>
    </IfModule>
  5. Now restart your server and request the script using http://localhost/mod_perl/howdy.

If your configuration is valid, the response should be a page containing simply the words, "Hello, world! Love, mod_perl."

See Also

  • http://perl.apache.org/

  • Writing Apache Modules with Perl and C by Doug MacEachern and Lincoln Stein (O'Reilly)

  • mod_perl Developer's Cookbook by Simon Cozens (O'Reilly)

    [ Team LiB ] Previous Section Next Section