Exchanging apache2-mpm-prefork for apache2-mpm-worker, and using php5-cgi on Debian to improve performance

Running a webserver on a Xen guest with a limited amount of memory, and serve dynamic content, makes one wanting to optimize it, or switch to another webserver,  like lighttpd. I was close to doing that, but I like my current apache setup, so I tried something else instead, which seems to have worked nicely for me. I switched to apache2-mpm-worker instead of apache-mpm-prefork, which is the default on Debian.

Lowering the number of preforked apache processes and changing other parameters in the apache2 configuration only gets one so far. It is robust, when there’s enough memory available to cope with the overhead.. but here is how to change to the mpm-worker instead, when having used mpm-prefork before, and php5, on Debian (Lenny).

  • Stop apache2, ‘/etc/init.d/apache2 stop’
  • Run aptitude, find “apache2-mpm-prefork”, hit – (for Remove, we want to save the configuration)
  • That will break some packages, hit “g” and aptitude will (did for me) solve it by removing apache2-mpm-prefork and libapache-mod-php5, and install apache2-mpm-worker, and php5-cgi instead.
  • After aptitude is done, read /usr/share/doc/php5-cgi/README.Debian.gz, which says the following:
To use php5-cgi with apache2
  1) activate CGI (it's on by default in default debian setups)
     a) If using the prefork MPM, use 'a2enmod cgi'
     b) If using a threaded MPM, use 'a2enmod cgid'
  2) activate mod_actions (a2enmod actions)
  3) Add the following to a config snippet in /etc/apache2/conf.d
<IfModule mod_actions.c>
        Action application/x-httpd-php /cgi-bin/php5
  • Do the above, I put the config in step 3 in /etc/apache2/conf.d/php5-cgi.conf
  • Start apache2, ‘/etc/init.d/apache2 start’
  • Done.

The amount of memory used is a lot less on my machine. PHP now runs as its own process, for the requests that require it. I didn’t change the default configuration for apache2-mpm-worker, it seems to run nicely as is. That is, from /etc/apache2/apache2.conf:

<IfModule mpm_worker_module>
    StartServers          2
    MaxClients          150
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadsPerChild      25
    MaxRequestsPerChild   0

Do note that if you have other packages and dependencies installed, aptitude will possibly not solve the dependencies the same way. Feel free to comment if you have other experiences using apache2-mpm-worker and php5-cgi to conserve memory on a small Xen guest with a small amount of available RAM, and serve content as good as possible.

Related article:

A revised opinion on apache2-mpm-prefork and apache2-mpm-worker, and php5 and php5-cgi on Debian, regarding performance






10 responses to “Exchanging apache2-mpm-prefork for apache2-mpm-worker, and using php5-cgi on Debian to improve performance”

  1. Evert Avatar

    So this is a good idea for a VPS with quad-core, but only 256MB? mpm-worker will be more memory-efficient than prefork?

  2. John Griessen Avatar
    John Griessen

    debian unstable gives me a dependency clash about apache2-mpm-worker and wordpress. Were you able to use debian packages somehow to get both installed? Or am I jumping to conclusions, (“WordPress powers beardy’s.”)

  3. beardy Avatar

    Thank you for the comment John.
    Yes you were a bit, but it’s a valid and logic jump. 🙂 However, I don’t use the packaged wordpress in Debian for this site, I use the upstream version, installed manually for this site specifically. It’s just how I prefer it in this instance, nothing wrong with the Debian package, just my preference.
    So the answer to the question is no, I don’t have both packages installed. My webserver runs lenny (stable) though, the “apache2-mpm-worker” package in Debian lenny does not conflict with the “wordpress” package.
    I hope you know what you are doing, running unstable on a server.

  4. beardy Avatar

    Sorry for a very, very delayed answer Evert, but yes, for me it has worked better than prefork anyway, when there is no load. I’m beginning to think that I should try prefork again for comparison. mpm-worker has a smaller “memory footprint” when the load is low, but when it goes up a bit, a few more visitors, the spawned php5 cgi processes clog the machine very fast. If you, like me, doesn’t want to spend money on more memory, you’ll have to experiment with your configuration and see what works best in your specific situation, depends on what sites you host.

  5. JC Avatar

    Thanks so much for this post!

    I just ran into the same performance issue with my server and switching to apache2-mpm-worker is doing the trick so far. I only have 256MB on my VPS and I know while this is a small amount, it should still be able to handle my blog due to low traffic, but it was responding really really slowly. I checked the resource usage and almost all my swap was being used up. Huge difference right now!

    I’m on Ubuntu 10.04 btw.

  6. Hayden Avatar

    prefork IS the fastest way to run PHP on Apache. There’s no question!

  7. Rod Bacon Avatar

    This too helped me, although I had to manually get around a few dependency problems within apt. I can confirm that I get much better performance in a low memory VPS environment, but will need to wait and see how I go in busier times.

  8. Evert Avatar

    Thanks! I needed that for the GopherProxy server. Google bots have been hammering that server a lot lately! 😉

  9. Krupz Avatar

    Thanks! Very helpful! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.