108

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?

2 Answers 2

196

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. Commented Nov 21, 2014 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. Commented Nov 30, 2016 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. Commented May 17, 2017 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. Commented Jul 22, 2017 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. Commented Jan 23, 2021 at 15:05
1

CGI (Common Gateway Interface)

  • Description: CGI is one of the oldest methods for executing server-side scripts. It works by creating a new process for each request, which can lead to high resource consumption.
  • Advantages: Platform-independent. Easy to setup and config.
  • Disadvantages: High resource usage due to process creation for each request. Slower performance compared to other methods.

FastCGI (Improved CGI):

  • Description: FastCGI is an improved version of CGI that overcomes performance issues by using persistent processes to handle multiple requests.
  • Advantages: Better performance than CGI due to process persistence. Reduced resource consumption.
  • Disadvantages: Config complexity compared to CGI.

mod_php:

  • Description: mod_php is an Apache HTTPD module that embeds PHP interpreter directly into the web server, allowing PHP code to run natively.
  • Advantages: Good performance as PHP code runs within the server process. Easy to config and manage.
  • Disadvantages: Security risks if not properly configured. Lack of process isolation can lead to stability issues. High resource usage by Apache HTTPD.

SuPHP (mod_suphp) - OLD:

  • Description: SuPHP is a tool for executing PHP scripts with permissions of their owners, enhancing security by restricting access to files.
  • Advantages: Improved security by running scripts with user permissions. Isolation of processes for enhanced stability.
  • Disadvantages: Overhead due to permission checks for each request. Config complexity compared to other methods.

PHP-FPM (FastCGI Process Manager):

  • Description: PHP-FPM(itself) is a process manager for FastCGI that enhances performance and scalability by managing pools of PHP processes. But the context is an implementation of FastCGI protocol.
  • Advantages: Efficient process management for handling high loads. Improved performance and scalability.
  • Disadvantages: Requires additional config compared to traditional FastCGI. Complexity in setting up and tuning process pools.

In conclusion, FastCGI and PHP-FPM are popular choices for high-performance PHP web apps, while mod_php may be suitable for simpler setups.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.