MySQL and Perl for the Web: Chapter 3 Section 2 (2/5) | WebReference

MySQL and Perl for the Web: Chapter 3 Section 2 (2/5)

To page 1current pageTo page 3To page 4To page 5
[previous][next]

Improving Performance with mod_perl

Create a Directory for mod_perl Scripts

Chapter 2 covered general Apache directory layout issues, including how to configure Apache to execute scripts found in the server's cgi-bin directory. Here we'll use a different directory for scripts that we intend to be executed by mod_perl. The examples in this chapter assume the use of cgi-perl under your Apache server's root, so the first thing you need to do is create that directory:

% cd /usr/local/apache
% mkdir cgi-perl

If you're using a different layout, make the appropriate substitutions in these commands and in the configuration instructions throughout the rest of this section.

Verify That mod_perl Is Installed

To use mod_perl, it needs to be compiled into the Apache httpd binary or loaded as an Apache dynamic shared object (DSO). Try running httpd –l to get a list of compiled-in modules. If mod_perl is among them, it's already installed. If mod_perl isn't compiled in, check whether it's available as a DSO. (Look in the modules directory under the server root directory to see whether there's a file with mod_perl in its name.) If not, you'll need to install mod_perl before proceeding to the next step. For instructions, refer to Appendix A.

Configure httpd.conf

If you're using a DSO version of mod_perl, you need to tell Apache where to find it by adding a LoadModule directive to your httpd.conf file. (If the name of the file is different from mod_perl.so on your system, make the appropriate adjustment to the line shown here.)

LoadModule perl_module modules/mod_perl.so

Next (for both compiled-in and DSO installations), tell Apache to associate mod_perl with scripts located in the cgi-perl directory by adding the following lines to httpd.conf:

Alias /cgi-perl/ /usr/local/apache/cgi-perl/
<Location /cgi-perl>
    SetHandler perl-script
    PerlHandler Apache::Registry
    PerlSendHeader on
    Options ExecCGI
</Location>

The Alias line specifies that when a URL begins with /cgi-perl/ after the host name part (for example, https://www.snake.net/cgi-perl/myscript.pl), Apache should look in the /usr/local/apache/cgi-perl/ directory to find the script. (You must use Alias; don't use ScriptAlias, because it won't work with mod_perl.)

The <Location> block provides the specifics about how to handle scripts found in the cgi-perl directory. The SetHandler and PerlHandler directives specify that we want to run them using Apache::Registry so that a CGI environment gets set up before they start executing. PerlSendHeader tells Apache that we want it to generate for us the HTTP header that precedes script output sent to the client. The Options line turns on CGI script-execution capability for the cgi-perl directory.

It's also possible to associate mod_perl with scripts based on their filenames rather than on their location. (You might do this if you want to put scripts in the document tree rather than in the cgi-perl directory.) To execute scripts having names ending in .pl as mod_perl CGI scripts, for example, add these lines to httpd.conf:

<Files *.pl>
    SetHandler perl-script
    PerlHandler Apache::Registry
    PerlSendHeader on
    Options ExecCGI
&lt/Files>

One drawback to associating .pl scripts with mod_perl this way is that the association will apply not only to new scripts that you write, but also to scripts that are already present in your document tree--scripts that may not have been written with mod_perl in mind. If that's a problem, you might want to consider using a mod_perl-specific extension such as .mpl instead. (Don't forget to change the <Files> line from *.pl to *.mpl if you do this.)

As long as you're modifying httpd.conf, add the following lines, too. They provide access to Apache::Status, a handler that displays all kinds of diagnostic information about your mod_perl setup when you send a perl-status request to Apache. Add the lines as shown, except that you should change the IP number on the allow from line to the name or IP number of the host on which you run your Web browser:

<Location /perl-status>
    SetHandler perl-script
    PerlHandler Apache::Status
    order deny,allow
    deny from all
    allow from 192.168.1.15
</Location>
To page 1current pageTo page 3To page 4To page 5
[previous][next]


Created: July 2, 2001
Revised: July 2, 2001

URL: https://webreference.com/programming/perl/mysqlperl/chap3/2/2.html