So I was reading up on Apache MPMs and whether it's better to use worker (threaded) or prefork.

The worker MPM uses multiple child processes. It's multi-threaded within each child, and each thread handles a single connection. Worker is fast and highly scalable and the memory footprint is comparatively low. It's well suited for multiple processors. On the other hand, worker is less tolerant of faulty modules, and a faulty thread can affect all the threads in a child process.

The prefork MPM uses multiple child processes, each child handles one connection at a time. Prefork is well suited for single or double CPU systems, speed is comparable to that of worker, and it's highly tolerant of faulty modules and crashing children - but the memory usage is high, and more traffic leads to greater memory usage.

Since I'm on a Slicehost 256slice, I wanted to try out worker to see if memory/speed performance was improved.

sudo apt-get install apache2-mpm-worker

Performance was a tad bit better--my test Merb app was handling a few requests/sec more with worker than prefork.  However, apache2-mpm-prefork is a required dependency when installing PHP5, so installing apache2-mpm-worker totally removed my PHP installation.  I didn't realize this until I was figured out why Apache was suddenly spitting out PHP pages as downloads.  (Moral of the story: really read the messages when you're prompted to do something)

So why is prefork a dependency?  From  the PHP docs:

PHP is glue. It is the glue used to build cool web applications by sticking dozens of 3rd-party libraries together and making it all appear as one coherent entity through an intuitive and easy to learn language interface. The flexibility and power of PHP relies on the stability and robustness of the underlying platform. It needs a working OS, a working web server and working 3rd-party libraries to glue together. When any of these stop working PHP needs ways to identify the problems and fix them quickly. When you make the underlying framework more complex by not having completely separate execution threads, completely separate memory segments and a strong sandbox for each request to play in, feet of clay are introduced into PHP's system.

If you feel you have to use a threaded MPM, look at a FastCGI configuration where PHP is running in its own memory space.

So, lesson learned.  You're stuck with prefork if you have to serve PHP files with Apache.

One Response to “apache2-mpm-prefork, apache2-mpm-worker, and PHP5”

  1. noelspringer Says:

    [quote]You’re stuck with prefork if you have to serve PHP files with Apache.[/quote]
    Installing Apache2 and PHP5 using mod_fcgid:
    http://ivan.gudangbaca.com/installing_apache2_and_php5_using_mod_fcgid

Leave a Reply