Sobes.tech
Middle

What is PHP-FPM and how does it interact with FastCGI?

sobes.tech AI

Answer from AI

PHP-FPM (FastCGI Process Manager) is an alternative (and more advanced) implementation of the FastCGI interface for PHP. It manages pools of PHP processes that can handle incoming requests from a web server.

Interaction with FastCGI:

PHP-FPM itself is a FastCGI process manager. It implements the FastCGI protocol and listens on a specific TCP port or Unix socket. The web server (e.g., Nginx or Apache with mod_fcgid or mod_proxy_fcgi) acts as a FastCGI client.

Interaction process:

  1. The web server receives an HTTP request.
  2. If the request is directed to a PHP file, the web server does not process it itself but packages the request data (e.g., environment variables, request body) into a FastCGI record format.
  3. The web server sends this FastCGI record to PHP-FPM at the configured address (socket or port).
  4. PHP-FPM receives the record, selects an available PHP worker from its pool, and passes the request data to it.
  5. The PHP worker executes the PHP script.
  6. The execution result (HTML, headers, etc.) and errors are packaged back into a FastCGI response.
  7. PHP-FPM sends the FastCGI response back to the web server.
  8. The web server receives the response, converts it back into a standard HTTP response, and sends it to the client.

FastCGI is a protocol, an interface between the web server and a program that handles dynamic requests (e.g., PHP). It solves the high load problem that occurs with CGI, where each request spawns a new process. FastCGI allows creating a pool of persistent processes (workers) that are reused to handle multiple requests, significantly reducing overhead.

PHP-FPM is a specific implementation of this protocol, providing additional process management and monitoring features such as:

  • Management of process pools (dynamic, static, on-demand).
  • Ability to restart workers when request or memory limits are exceeded.
  • Request statistics collection.
  • Proper worker shutdown during reload.
What is PHP-FPM and how does it interact with… - sobes.tech