PHP: What are the advantages of FastCGI over mod_php

apache-2.2fastcgimod-phpperformancePHP

It was recently suggested to me that I use FastCGI with PHP. Now I went to the FastCGI page and read it but I don't really understand what the advantages are.

Best Answer

Using mod_php each Apache worker has the entire PHP interpreter loaded into it. Because Apache needs one worker process per incoming request, you can quickly end up with hundreds of Apache workers in use, each with their own PHP interpreter loaded, consuming huge amounts of memory.

(Note, this isn't exactly true, Apache's worker_mpm allows you to serve many requests with a single threaded Apache worker. However, even in 2009, this is not the recommended way to deploy PHP because of suspected threading issues with the PHP extensions.)

By using PHP in fast_cgi mode (using something like spawn-fcgi from the lighttpd package) has the following benefits

  • tune the number of PHP workers separately from the number of incoming connections
  • allow you to put you PHP workers on a different server, or scale across many servers without changing you web tier
  • gives you flexibility to choose a different web server, like nginx, or lighttpd
  • allow you to run your PHP application in a different security domain on your web server