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:
- The web server receives an HTTP request.
- 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.
- The web server sends this FastCGI record to PHP-FPM at the configured address (socket or port).
- PHP-FPM receives the record, selects an available PHP worker from its pool, and passes the request data to it.
- The PHP worker executes the PHP script.
- The execution result (HTML, headers, etc.) and errors are packaged back into a FastCGI response.
- PHP-FPM sends the FastCGI response back to the web server.
- 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.