• mod_php, FastCGI or PHP-FPM for Server?

    by  • January 1, 2014 • Blog • 9 Comments

    Most administrators know that PHP, the widely used scripting language, can be embedded in HTML and works with all major web servers. What’s less widely known, however is that you can run PHP in different ways on your server. The most common option is the mod_php module that’s runs by default in the Apache HTTP Server. If your primarily goal is performance, however, you should consider other options. PHP-FPM (FastCGI Process Manager) and PHP FastCGI each have pros and cons, but either can speed up the performance of your PHP.

    php

    mod_php

    Let’s start by looking at running Apache with mod_php. This package is present in just about every Linux distribution’s repository, so installing it is easy, and so is configuring and managing the software. In fact, its ease of use may be the main reason to deploy mod_php.

    With mod_php the PHP interpreter is “embedded” inside the Apache process; Apache doesn’t call any external PHP process, which means that Apache and PHP can communicate better. However, every single Apache child must load mod_php, which results in a bigger process than necessary being used for serving static resources such as image files, CSS, and JavaScript.

    Another problem with this method is that it works only on Apache. This is not always an issue, as Apache is the most complete web server you can find, but websites that run on small virtual private servers (VPS), or big sites that have to serve million of pages a day, might want a different web server that can scale up or down better than Apache, and would therefore need a different way to run PHP too.

    Pros:

    • Easy to install and update.
    • Easy to configure with Apache.

    Cons:

    • Works only with Apache.
    • Forces every Apache child to use more memory.
    • Needs a restart of Apache to read an updated php.ini file.

    FastCGI

    FastCGI is a generic protocol for interfacing interactive programs with a web server. It’s a variation on the earlier Common Gateway Interface (CGI) that reduces the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more page requests at once.

    Apache can use FastCGI in the form of mod_fcgid, and other popular webservers, such as lighttpd, nginx, Cherokee, and even Microsoft IIS, can use FastCGI as well.

    With FastCGI you can set up multiple versions of PHP. This is particular useful when you have old websites made for, say, PHP 5.1 that are not compatible with the latest version of PHP. With FastCGI you can serve old PHP to the old website while using the latest version for all the others, something that is not possible with mod_php.

    FastCGI also utilizes suexec to support different users with their own instances of PHP. This feature is particularly important for improving security in a shared environment, where you could have different users each managing their own websites. FastCGI reduces the memory footprint of your web server, but still gives you the speed and power of the entire PHP language.

    Pros:

    • Compatible with many web servers.
    • Smaller memory footprint than mod_php.
    • More configuration options, including multiple PHP and suexec.

    Cons:

    • Requires more configuration than mod_php.
    • Not widely known in the IT community.

    PHP-FPM

    PHP-FPM, the newest way to use PHP with a webserver, is an alternative PHP FastCGI implementation with some additional features that can be useful for websites that run on both small VPSes and multiple servers. It can be used with any web server that is compatible with FastCGI.

    PHP-FPM gives administrators the ability to gracefully stop and start PHP worker processes without losing any queries. This allows you to gradually update the configuration and binary without losing any queries. You can start workers with different uid, gid, and chroot environments and different php.ini options. You can also run an emergency restart on all the processes in the event of any accidental destruction of the shared memory opcodes cache, if you’re using an accelerator.

    Pros:

    • Compatible with many web servers.
    • Smaller memory footprint than mod_php.
    • More configuration options than FastCGI.

    Cons:

    • Requires more configuration than mod_php.
    • Not widely known in the IT community.
    • Project is still relatively young.

    Conclusions

    I can think of no general rule or guideline to tell you what’s the right method for you or your company to use to speed up PHP. Personally, I use mod_php on most servers for its stability and the fact it is so well-known. I use PHP-FPM on small VPSes where saving memory while keeping fast performance is a must. The important thing to keep in mind is the variety of different solutions available; knowing about all of them is fundamental when planning your project.

    9 Responses to mod_php, FastCGI or PHP-FPM for Server?

    1. Zenmad
      December 16, 2014 at 2:14 pm

      php-fpm vs mod_php only need to take a look if your site serve a lot of traffic, other than that, they should work the same. In the big site, we have to use php-fpm for the best performance and optimization.

      • Fenrir
        December 30, 2014 at 10:16 pm

        You forgot The biggest advantage of php-fpm : pool management
        Each pool can be view as a full instance of php with it’s own limit/restriction (memory, childs, folders, modules, env vars, logs, …)
        You can create a pool by application and/or path and/or vhost

    2. Ben
      January 12, 2015 at 8:34 am

      Thanks for explaining this!

    3. January 22, 2015 at 10:51 pm

      Thanks for sharing!

    4. March 13, 2015 at 10:26 pm

      Very useful! Thanks for sharing.

    5. May 16, 2015 at 12:20 pm

      Thanks for sharing. Can you please tell the other PHP modules?

    6. Dan Reznik
      September 29, 2015 at 2:28 pm

      can someone recommend a hosting service that runs on mod_php or mod_suphp

      Dan.

    7. March 2, 2016 at 4:10 pm

      I’m glad my friend send me this url to read, right now I’m configuring my server ^_^

    Leave a Reply

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