98

There are few different php "wrappers"(?). What are differences between them? Tried to google some, but cant seem to find informations. (mod-php is not googleable).

Why might I choose one over another?

181

CGI and FastCGI are two protocols not specific to PHP:

  • CGI scripts is a way how to run a server side script (not only PHP!) when a HTTP request arrives. In this setup, the web server launches a new CGI process for every incoming request, causing significant performance overhead.

  • FastCGI is a "better CGI" - to address the limitations of CGI, the FastCGI runs as a server (TCP or UNIX), so that resources can be reused across requests.

PHP-enabled webserver can be configured as following:

  • mod_php is an Apache module to run PHP. In this setup, PHP request is handled under Apache process with everything that goes with it: PHP processes are defined in Apache configuration, PHP runs under Apache user and permissions etc.

  • PHP-FPM is PHP's FastCGI implementation. In this setup, PHP-FPM runs as a standalone FastCGI server and Apache connects to it using FastCGI modules, such as mod_fcgid, mod_fastcgi or mod_proxy_fcgi (Apache 2.4+). In this configuration, permissions, processes related stuff & everything else is controlled by the PHP-FPM server. Performance is comparable with mod_php.

  • SuPHP - this was used to address some shortcomings of mod_php related to permissions: with mod_php PHP scripts are run under the Apache user/group, but mod_suphp can run the scripts as a different user. suPHP is not maintained anymore and should not be used.

  • CGI/FastCGI - I have added this one based on a question in the comments. Without knowing the details of the setup, PHP can be run as FastCGI server using any other FastCGI implementation - as explained in another question. I don't use this setup and don't see any benefit over PHP-FPM.

  • CGI - PHP can also be run as the good-ol' CGI script, but I can't imagine a single good use case for that, apart for compatibility with some very outdated environments.

Regarding advantages and disadvantages of those different approaches, I stick only to mod_php and PHP-FPM, covering two main use cases:

  • mod_php can be useful in certain Docker setups where you want to deliver a single container running a PHP-enabled web server. The fact that everything runs as a single process makes the Docker container configuration easier. On the other hand, running PHP-FPM server in a single container with a webserver would require process orchestration either with supervisord, advanced bash scripting or some other approach and goes against best practices writing Docker containers.

  • PHP-FPM is a more powerful approach that separates the concerns better, so the PHP-FPM server can be configured, (performance-)tuned and maintained separately from the webserver. This also allows to run the PHP-FPM server in a pool or on a different machine than the webserver. As implied above, for Docker containers, a separate PHP-FPM and webserver containers are recommended in this case, making the configuration more complex (and more powerful). PHP-FPM approach is also the only way with nginx webserver as the PHP module for it AFAIK does not exist.

My Docker implementation of the two aforementioned approaches can be found here:

The implementation is designed to work with some of my legacy and new projects in my Kubernetes cluster. Feel free to use it.

So, TLDR:

  • CGI, FastCGI are protocols; CGI is slow, FastCGI is much faster
  • mod_php and PHP-FPM are two main ways how to run PHP
  • mod_SuPHP was an approach that was used to address mod_php shortcomings. It is outdated and PHP-FPM should be used instead.
11
  • 14
    I do not know what ISPConfig panel really uses, but PHP run as PHP-FPM means, that PHP will start it's own built-in FastCGI server and will listen for requests through FastCGI protocol. HTTP server will be receiving requests from internet, handling them as usually and in case a page needs to be run using PHP, request will be handed over FastCGI protocol to PHP and the result would be sent back to browser. Think of that as HTTP server standing between browser and PHP (PHP-FPM FastCGI) server. PHP-FPM is an implementation of FastCGI protocol. Nov 21 '14 at 0:10
  • 1
    @AlešKrajník thanks for that elaborate explanation. Well, I've been trying to run php as seperate user/group. So I started with Su-exec and hit a dead end and then a couple of people recommended php-fpm which you have explaned. However, am still confused about modules mog_cgi, mod_cgid, mod_fastcgi, mod_fcgi and how these work with php-fpm. Also I read in another tutorial they were using mod_fcgid and mod_proxy_fcgi. How struggling to understand how all the pieces come together. Nov 30 '16 at 7:13
  • 2
    @landed PHP-FPM binary is part of every PHP distribution since 5.3.3 or 5.4.0 (check this: php-fpm.org), for that specific PHP version. May 17 '17 at 6:13
  • 4
    php-fpm is all about removing initialization costs. php-fpm pre-starts several php processes, ready to process requests, and have them sleep until requests come in - which means it can respond much faster than traditional cgi, because php is already running when requests come, as opposed to traditional CGI, where a new php process is started for each request, also php-fpm doesn't shut down the php process after processing requests, but keep reusing the same processes. - with 0 overhead of starting and stopping processes, php-fpm responds much faster. starting & stopping processes takes time.
    – hanshenrik
    Jul 22 '17 at 23:11
  • 1
    @Hardoman Yes, that's how I use it. I have improved / refreshed my answer and added links to the Docker images I made for the two main suggested approaches: mod_php with Apache 2.4 and PHP-FPM with Apache 2.4 and mod_proxy_fcgi. Feel free to use it, or please open another question, I'll be happy to provide details on the configuration. Jan 23 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.