Total Modern Mod_perl

by Stas Bekman

One of the most popular Apache modules is mod_perl. We kick off our regular coverage of mod_perl with an overview of the module geared for both true believers and Apache neophytes.

Editor's note: With this column Stas Bekman kicks off his regular coverage of mod_perl, one of the most popular Apache modules in use. In future columns he'll focus on various mod_perl topics, geared for both beginning and advanced Apache devotees.

In this article I'll give an initial introduction to mod_perl, make you want to give it a try, and present a few examples of the well-known sites that are powered by mod_perl-enabled Apache.

What is mod_perl?

mod_perl is at the heart of the Apache/Perl integration project, which brings together the full power of the Perl programming language and the Apache Web server.

From the outset Apache was designed so that you can extend it by the addition of modules. Modules can do anything you need to do, such as rewrite HTTP requests, restrict access to certain pages, perform database lookups and so on. Modules are normally written in C, which can be hard work. mod_perl is a module that lets you do all of these things, and more, using Perl--which makes the development much quicker than C. Apache is the most popular Web server on the Internet, and mod_perl is one of the most popular modules for extending it.

Why is mod_perl So Popular?

If you love Perl and your favorite Web server is Apache, you will love mod_perl at first sight. Once you try it in action you will never look back, as you will find that mod_perl has everything you need. But even if you do find that there is something missing, just speak up. Before you can count to three, someone will have made it for you--which, of cours,e will make you want to give something in return. Eventually you will contribute something on your own, and that will save time for a huge mod_perl community so that they can create even more things for others to use.

You get the picture--mod_perl empowers its users, who in turn empower mod_perl, which in turn empowers its users, who in turn.... It's as simple as the nuclear reaction you learned about at school, or will learn at some point.

With mod_perl it is possible to write Apache modules entirely in Perl, so this lets you easily do things that are more difficult or impossible in regular CGI programs, such as running subrequests, writing your authentication and logging handlers, and much more.

The primary advantages of mod_perl are power and speed. You have full access to the inner workings of the Web server and you can intervene at any stage of HTTP-request processing. This allows for customized processing of the various phases, for example URI to filename translation, authorization, response generation, and logging.

There are big savings in startup and compilation times. Having the Perl interpreter embedded in the server saves the very considerable overhead of starting an external interpreter for any HTTP request that needs to run Perl code. At least as important is code caching: the modules and scripts are loaded and compiled only once, when the server is first started. Then for the rest of the server's life the scripts are served from the cache, so the server only has to run the precompiled code. In many cases this is as fast as running compiled C programs.

There is very little run-time overhead. In particular, under mod_perl there is no need to start a separate process per request, as is often done with other Web-server extensions. The most wide-spread such extension mechanism, the Common Gateway Interface (CGI), is replaced entirely with Perl code that handles the response generation phase of request processing. Bundled with mod_perl are two general-purpose modules for this purpose: Apache::Registry, which can transparently run existing unmodified Perl CGI scripts and Apache::PerlRun, which does a similar job but allows you to run scripts which are to some extent dirtier.

Also, mod_perl allows you to configure your Apache server and handlers in Perl (using the PerlSetVar directive and the <Perl> sections). This makes the administration of servers with many virtual hosts and complex configuration a piece of cake. Hey, you can even define your own configuration directives!

How Fast and Stable is mod_perl?

Many people ask, "How much of a performance improvement does mod_perl give?'' Well, it all depends on what you are doing with mod_perl--and possibly whom you ask. Developers report speed boosts from 200 percent to 2000 percent. The best way to measure is to try it and see for yourself (see http://perl.apache.org/tidbits.html and http://perl.apache.org/stories/ for the facts).

Every second of every day, thousands of Web sites all over the world are using mod_perl to serve hundreds of thousands of Web pages. Apache and mod_perl are some of the best-tested programs ever written. Of course they are continually being developed and improved, but you do not have to work on the bleeding edge of development--you can use the stable products for your sites and let others do the testing of the new versions for you.

I want to show you just a few of the many very busy and popular sites that are driven by mod_perl. A thousand words can't substitute the experience. Visit the sites and feel the difference. They will persuade you that mod_perl rules!

  • ValueClick serves more than 70 million requests per day from about 20 machines. Every response is dynamic, with all sorts of calculation, storing, logging, counting, you name it. All of their application programming is done in Perl.

  • Singles Heaven is a Match Maker site with almost 26,000 members and growing. The site is driven by mod_perl, DBI, Apache::DBI (which provides a persistence to database connections) and MySQL. The speed is enormous, chatting with mod_perl is a pleasure. Every page is generated by about 10 SQL queries, for it does many dynamic checks on every page--like checking for new emails, users who are watched by various watchdogs and many more. You don't feel these queries are actually happening, the speed is as fast as the "Hello World" script. Enter as anonymous user to try it.

  • Internet Movie Database serves around two million page views per day. All database lookups are handled inside Apache via mod_perl. Each request also goes through several mod_perl handlers and the output is then reformatted on the fly with mod_perl SSI to embed advertising banners and give different views of the site depending on the hostname used.

  • Deja.com serves about 160 million page views per month. It's uses a cluster of mod_perl enabled Apache servers running on the Linux operating system.

  • MP3.com served 142 million page views to 560,000 individual visitors in April 2000.

How Many Sites are Running a mod_perl-Enabled Apache Web Server?

According to the Netcraft survey, as of May 2000 nine million hosts are running the free Apache Web server, which is more than 60 percent of all survey hosts.

What about mod_perl? http://perl.apache.org/netcraft/ reports that sites running mod_perl account for 852,675 host names and 108,327 unique IP addresses. This is actually an underestimate, since when hosts are scanned for running Web servers only well known ports are checked (80, 81, 8080 and a few others). If a server runs on unusual port it does not enter the count unless the owner has manually added it to the Netcraft database. Here is a graph of the growth in mod_perl usage:

mod_perl growth

For the latest numbers see http://perl.apache.org/netcraft/.

The Road Ahead

You probably are all excited about the release of Apache 2.0, the next generation of the best Web server. The major new feature of this new generation of the Web server is threaded processes, which should make the server faster. Early tests show that the alpha version is faster than the latest stable version from the previous generation.

You probably are not less excited about the recent release of Perl 5.6, whose main new feature is stable support for threads, something that existed in the previous Perl version but which was quite shaky.

What has all this to do with mod_perl? mod_perl 2.0 is being developed at this very moment and will benefit enormously from the new Apache and Perl features. The most important improvement will be a reduced process size--a parsed Perl opcodes tree will be almost completely shared between threads of the same process.

Do you believe in coincidences? Both Perl 5.6 and Apache 2.0 were released in the same week. Looks very suspicious to me. If you get the obvious conspiracy uncovered please let me know.

Of course there are lots of bumps ahead of us. It will take time before all our applications will be able to benefit from the threading features. The main reason lies in fact that most of the Perl modules available from CPAN aren't thread safe. But you shouldn't despair. You can turn off threads for Perl code that is not thread safe or which uses modules which aren't thread safe.

I Want mod_perl Now, Where Do I Get It?

mod_perl's home is http://perl.apache.org. From the site you will be able to download the latest mod_perl software and various documentation, find commercial products and free third-party modules, read the success stories, and learn more about mod_perl.

It's quite important to get yourself subscribed to the mod_perl list. If you want know what happens with mod_perl, if you want to know what new features are being developed, if you want to influence and contribute or if you simply want to get help, then you don't want to skip this mailing list. To subscribe to the list send an empty email to modperl-subscribe@apache.org.

Are There Any mod_perl Books and Documentation?

Lincoln Stein and Doug MacEachern wrote Writing Apache Modules with Perl and C. You will also find a vast list of mod_perl documentation on the mod_perl home page: http://perl.apache.org.

I Love mod_perl, I Want To Know Who Wrote This Great Free Product!

Well, Doug MacEachern is the person to blame. He is the guy who gave mod_perl to the mod_perl community. He is the Linus of the mod_perl project.

But as you know in a big community there are always people who love to help, and there is a bunch of developers from all around the world who patch mod_perl, develop entire Perl modules for it, debug the server and advocate it. I'm afraid the list of contributing developers is too long to include here. But you are welcome to join the mod_perl mailing list and see all these folks in action. I promise you, you won't regret the show, since you are going to learn much more than just about mod_perl. See for yourself.

About the Author

Stas Bekman is an author of the mod_perl Guide and is currently co-athoring a book about mod_perl for O'Reilly and Associates, Inc together with Eric Cholet. He is a member of the Apache Software Foundation. You can find more about his works and joys at his personal web site.

This article was originally published on Wednesday Jun 7th 2000
Mobile Site | Full Site