Previous Page Next Page

Appendix D. Power and Speed: CGI and mod_perl

What Is mod_perl?

The mod_perl Web Site

Installing mod_perl

Resources

D.1. What Is mod_perl?

Some people may tell you that CGI scripts are a thing of the past. You should be using PHP, ASP.NET, etc. Not true. The mod_perl module (originally written by Doug MacEachern and licensed under the Apache Software license) gives you a whole new way to create dynamic content by integrating the Apache server and Perl. mod_perl is described as the marriage between Apache and Perl. It is an open source module implemented as a Perl interface to the Apache API and allows existing CGI scripts to run much faster.

By embedding the Perl interpreter right in the Apache server, there is no need to start up (fork) a separate, external interpreter to run CGI programs. In addition, modules and scripts are loaded and compiled only once, and for the rest of the server's life they are served from the cache. Thus, the server spends its time running already loaded and compiled code, which is very fast.

Lincoln Stein, author of CGI.pm, describes mod_perl as follows:

"mod_perl is more than CGI scripting on steroids. It is a whole new way to create dynamic content by utilizing the full power of the Apache web server to create stateful sessions, customized user authentication systems, smart proxies and much more. Yet, magically, your old CGI scripts will continue to work and work very fast indeed."

Today, Apache is the most popular Web server on the Internet, and mod_perl is one of the most popular modules for extending it. With the mod_perl API, it is possible to write customized Apache modules in Perl rather than in C and to dynamically configure the server itself, making the administration of servers with complex configurations easier to manage. You can write authentication and authoriztion handlers to allow or restrict usage of certain pages, perform database lookups, rewrite HTTP requests and URLs, log requests, etc.

Bundled with mod_perl are two general-purpose modules that make it possible to run existing Perl CGI scripts without modification (as long as they are well written, not "sloppy"). These Perl modules are found in the Perl site/lib library; they are called Apache::Registry.pm (or ModPerl::Registry.pm) and Apache::PerlRun.pm or (ModPerl::PerlRun.pm). The difference between these two modules is that the Registry.pm module caches all scripts, and the PerRun.pm module doesn't. The advantage of caching (i.e., compiling the script once and keeping it in memory) is speed and persistence. This is all fine as long as the script is well-written, has initialized variables, closed filehandles, sets warnings and diagonostics, and generally follows rules of good Perl programming. If the script is written in a sloppy way, then the PerlRun.pm module is a safer choice. Each time a request is made for the script, everything starts fresh because the code was not cached. Either way, the big advantage is that the Perl interpreter is loaded only once, thereby increasing the performance of your scripts. The mod_perl site (http://perl.apache.org/download/index.html) says that "the standard Apache::Registry module can provide 100x speedups for your existing CGI scripts and reduce the load on your server at the same time."

Check this site for statistics on mod_perl and Apache usage worldwide.

Figure D.1.


Previous Page Next Page