Nginx – Apache MPMs – Worker vs Prefork

apache-2.2lighttpdnginxPHPphp-fpm

I'm trying to figure out what which is the best Apache MPM I can install on my VPS. I saw some benchmarks and MPM Worker seems to perform better than the Prefork one but for some reason everyone seems to be recommending Prefork over Worker for PHP setups.

Is this just because some PHP functions are not thread-safe? Only setlocale() comes to mind, but I'll be using the same locale on every instance so I don't think that will be a problem. My VPS has little memory and I feel that the worker MPM would be a better match for my needs, however I'm not sure.

Can someone help me make up my mind about this? Thanks!


PS: I've also looked into nginx and lighttpd. nginx seems awesome but I'm trying to avoid compiling from source and I still don't quite understand what php-fpm is for. Regarding lighttpd – it was incredible easy to setup PHP/FCGI with it, but I heard that it has memory leaks. Is this still true?

Best Answer

You should use prefork when using apache + mod_php. The FAQ clearly outlines the reason why:

http://www.php.net/manual/en/faq.installation.php#faq.installation.apache2

Why shouldn't I use Apache2 with a threaded MPM in a production environment?

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, further weaknesses are introduced into PHP's system.

nginx + php-fpm is also an excellent way to run php applications. nginx has native support for FastCGI and php-fpm is one of the best ways to run php in a FastCGI environment. See the php documentation here:

http://www.php.net/manual/en/install.fpm.php