102

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?

Gacek
  • 1,181
  • 2
  • 9
  • 10

1 Answers1

190

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.
Aleš Krajník
  • 2,481
  • 1
  • 15
  • 11
  • Maybe I will clarify what inspired me for this question. I just got a VPS with a ISPConfig panel (Opensource panel to admin pages). There I can chose a PHP version for server/side. And those are listed there. So basically it seems that chosing PHP-FPM choses FastCGI with additional "wrappers"? – Gacek Nov 20 '14 at 11:22
  • 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. – Aleš Krajník Nov 21 '14 at 00: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. – David Okwii Nov 30 '16 at 07:13
  • So does php-fpm supports php in the versions such that there could be version7..its not like its a different versioning? – landed May 15 '17 at 16:14
  • 2
    @landed PHP-FPM binary is part of every PHP distribution since 5.3.3 or 5.4.0 (check this: https://php-fpm.org/), for that specific PHP version. – Aleš Krajník May 17 '17 at 06: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
  • I have CGI/FastCGI in phpinfo() output but I don't have neither mod_fastcgi, mod_fcgi installed. Also mog_cgi, mod_cgid as disabled in httpd.conf. PHP-FPM is not installed. Then how PHP scripts are running? – Hardoman Jan 22 '21 at 10:58
  • Also from the answer is still not clear if it's better to create a FCGI wrapper or use PHP-FPM – Hardoman Jan 22 '21 at 10:59
  • @Hardoman It's hard to say based on your input, however the answer is 6 years old and partially outdated. In Apache 2.4 probably the best module to use for FastCGI is now "proxy_fcgi", so perhaps try to look for that one. You are running PHP as CGI/FastCGI version, not covered by my answer above: it is still FastCGI protocol without PHP's FPM manager, for more information check this: https://www.php.net/manual/en/install.fpm.php. – Aleš Krajník Jan 23 '21 at 12:43
  • @AlešKrajník thanks. Did I get it right that since I use Apache 2.4 it's better to look into proxy_fcg + FPM instead? – Hardoman Jan 23 '21 at 13:35
  • 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. – Aleš Krajník Jan 23 '21 at 15:05