By Louis Simoneau

PHP with nginx is about to Become a Lot Easier

By Louis Simoneau

PHP version 5.4 will most likely include the PHP-FPM patch right in the core, which is great news for those of us who like to run PHP under the nginx web server. You may be asking, “What is PHP-FPM, and why should I care?”

PHP-FPM is a patch for PHP core that handles the starting, stopping, and restarting of FastCGI processes as needed. This is important because nginx can only interface with PHP via FastCGI, unlike Apache, which loads the whole PHP environment right into itself. In addition to the performance benefits of nginx over Apache, running PHP via FastCGI rather than as an Apache module has its own benefits:

  • Lower memory usage (since extra nginx workers come without the whole weight of the PHP environment)
  • Easier permissions management (PHP can run as a different user than your server process)
  • If PHP crashes, nginx can keep going

The downside is that, compared to mod_php, the nginx, PHP, and FastCGI stack takes significantly more work to set up. In the past, the way to make this setup work was to co-opt the spawn-fcgi script from the LightTPD project, and use that to start the FastCGI process. However, there are problems with that setup: it’s fragile (if a process stops it might not be restarted correctly) and it’s a pain to set up. After installing and configuring nginx, you need to download LightTPD, grab the script in question and configure it to start automatically, then configure everything to play nicely together. If you’re used to the out-of-the-box experience of mod_php, this might convince you to just upgrade your server instead of switching to the leaner, meaner nginx.

Enter PHP-FPM. This patch bakes FastCGI process management right into PHP. So if you compile PHP with the FPM patch in place and the --enable-fpm configuration option, PHP will take care of starting and stopping processes as nginx requests them, with no additional configuration required. Of course, manually patching and compiling the PHP source is still more work than us lazy web developers would like to do, which is why it’s great news that, as of PHP 5.4, FPM will be folded into the core of the PHP project.

This means that you’ll be able to download PHP, compile it with the --enable-fpm switch, and be off to the races running it with nginx in the same amount of time and effort it would take to set up with Apache.

So if you’ve tried running PHP with nginx in the past and given up after jumping through the seventh hoop, I’d suggest you give PHP-FPM a try. At the moment there’s still a bit of hassle involved, but it’s improving all the time. In the meantime, you get to be the cool kid on the block with the sweet new toy!

  • Peter Mescalchin

    This also has some future benefits for Apache where you can make use of mod_fcgi ( to run PHP the FCGI way.
    This works well, as Apache can then run the much more efficient mpm-worker, rather than mpm-prefork – and like nginx you now have connections serving up static content (JS/CSS/images) without carrying the payload of PHP when it’s not needed – using much lighter threads for static content.

    It’s a nice stepping stone for those that don’t want to loose Apache and move to an alternative web server.

    • Louis Simoneau

      Thanks for pointing that out Peter, I hadn’t seen it before. Personally I enjoy tweaking nginx config and learning as I go, but I can totally understand why you’d want to stick with Apache if that’s what you’re used to, so this is a great alternative.

      • Peter Mescalchin

        Certainly moving to nginx/lighthttpd is a good move if you can do so for the most part – it will pretty much trump Apache when it comes to serving requests.

        I had a look at doing the same, but the thought of re-writing mod_rewrite rules for nginx/lighthttpd made my head hurt – something to save for a later date :)

        This guide gives a nice tute for Apache+FCGI+PHP+APC

  • The HungryCoder

    I am also a fan of nginx. But yes, I am tired of configuring php and nginx. So, if PHP 5.4 includes PHP-FPM in core, I will be in the team of happiest developers ;). However, I pitfall is that, I still could not upgrade to PHP 5.3 as many sites break. :(

    • Andrew

      Many sites break? Would you elaborate?

      Seriously, I’m always hoping for my different hosting providers to upgrade PHP, but they never do because of it breaking sites.

      I have just one humble question: Does it break those sites because it no longer supports features, or just because it makes some features work the way they should but the sites aren’t coded right?


  • I wonder, does this also apply to IIS 7 + FastCGI?

  • Philip Olson

    Also, there is a decent chance that php-fpm will end up in 5.3.3… :)

    • Louis Simoneau

      They said that about 5.3.2 as well, so I’m hedging my bets ;-)

      • philip

        I don’t remember said good chance with 5.3.2, but do know of 5.3.3. In fact, I’d say an 85% chance, mostly because it’s fun to type numbers. Only finalizing/testing the new php-fpm ini syntax stands in the way, really.

  • Anton

    That is great news. Any idea when PHP 5.4 is set to be released?

  • How is nginx pronounced?

    • Louis Simoneau

      Some people say “en-jinks”, but I much prefer the cooler-sounding “Engine X”, and I figure that’s what the developer was going for.

  • the_guv

    Lighttpd offer SPAN-FCGI as a separate module now, so compiling from SPAWN is actually easier (and always was) easier to setup than PHP-FPM.
    The real reason is change, as Louis splendid article says, comes down to performance and, with PHP saying they’ll add FPM to the core code for about 5.4, future-proofing (and then it’ll be real easy to install this method too).
    Here’s some fresh comparison between FastCGI, SPAWN-FCGI, PHP-FPM (integrated) and PHP-FPM (seperate):-

  • devarni

    php_fpm is not better for performance reasons. Compared to the php_cgi there is no big difference. Its mostly a thing for very busy servers.

    Also… mod_php with Apache gives the best performance for PHP, yes there is the memory footprint of Apache etc. on the other side.
    Cherokee with fast cgi gives similar performance then Apache and have this light memory footprint. But for static files is miles ahead Nginx.

Get the latest in Front-end, once a week, for free.