MySQL and Perl for the Web: Chapter 3 Section 2 (1/5)
Improving Performance with mod_perl
Apache uses a standard protocol, the Common Gateway Interface (CGI), to communicate with externally executed scripts such as those we put in the cgi-bin directory in the preceding chapter. When a CGI script begins executing, it can assume that Apache has set up certain environment variables. For example,
REQUEST_URI indicate the client's host IP number and the request path. When Apache uses
mod_perl to execute a script directly, it doesn't set up the CGI environment. In principle, there is no need, because a script that has direct access to Apache's internals obviously can extract that information itself if it wants. But the practical implication of this is that Perl CGI scripts won't function properly under
mod_perl unless they are rewritten to use the Apache API or unless something else sets up the CGI environment for them.
Obviously, the latter alternative is preferable. If you already have a bunch of CGI scripts, you don't want to rewrite them all specifically for
mod_perl. Fortunately, there is an easy solution to this problem.
mod_perl includes an
Apache::Registry module that sets up the CGI environment for you. If we use it to run our CGI scripts,
mod_perl becomes transparent to them so that (for the most part) they don't need to know or care whether they're being run by a standalone Perl process or by
mod_perl. This enables you to move your scripts between the standalone and
mod_perl execution environments easily.1
Oh, you noticed that "for the most part" in the preceding paragraph, did you? That disclaimer was necessary because scripts containing certain constructs need modification for
mod_perl. We'll get to this in the section titled "Writing
The rest of this section describes how to configure Apache to use
Apache::Registry for running Perl scripts. The steps are as follows:
- Create a directory for
- Verify that
httpd.confto tell Apache how to execute
- Test your configuration.
- Set up a
mod_perlstartup file (optional, but useful).
Before following these instructions, verify that you have recent enough versions of Perl and CGI.pm. You should have Perl 5.005 or higher. You should also have CGI.pm 2.36 or higher, because earlier versions don't work with
mod_perl. If your versions aren't recent enough, you'll need to upgrade. See Appendix A, "Obtaining Software," for instructions.
1. Clearly, I'm making an argument for being able to write scripts that run whether or not you have
mod_perl installed, so that readers who can't install
mod_perl or who elect not to will be able to run most of the scripts in this book without modification. There is a counterargument, which is that if you know you're going to use
mod_perl, you can get even better performance by dispensing with
Apache::Registry and interacting more directly with Apache.
Created: July 2, 2001
Revised: July 2, 2001