Web20 University

How PHP FPM Works

PHP-FPM, which stands for “PHP FastCGI Process Manager,” is an alternative PHP FastCGI implementation. It provides some additional features, such as adaptive process spawning, which are particularly useful for sites suffering from heavy load. The primary role of PHP-FPM is to manage and handle PHP processes and requests in a web environment.

Here is a high-level overview of how PHP-FPM works:

  1. PHP-FPM acts as a daemon: It runs continuously in the background, waiting for PHP file requests.

  2. It uses FastCGI to interface with web servers: FastCGI is a protocol for interfacing interactive programs with a web server. It’s a variation on the earlier Common Gateway Interface (CGI). FastCGI’s main aim is to reduce the overhead related to interfacing the web server and CGI programs, allowing a server to handle more web page requests simultaneously.

  3. Process management: PHP-FPM maintains a pool of worker processes, ready to respond to PHP requests. The FastCGI Process Manager (FPM) can spawn or kill processes based on demand and configuration.

  4. Delegation of requests: When a request is received, it is passed by the web server (like Nginx or Apache) to one of the PHP-FPM worker processes for processing.

  5. Processing the request: The PHP-FPM worker processes the request and executes the appropriate PHP script. The results (HTML, JSON, XML, etc.) are then passed back to the web server.

  6. Return of the response: The web server then sends this output back to the client (such as a web browser).

The advantages of using PHP-FPM include:

  • Adaptive process spawning: PHP-FPM is smart enough to manage the number of processes it spawns. It can do this based on configuration directives you provide, helping you to avoid overloading your server.

  • Graceful Stop/Start: It can handle traffic gracefully when restarting PHP-FPM or PHP upgrades, reducing potential downtime.

  • Advanced process management: PHP-FPM also allows for more advanced process management configuration, including the ability to set up pools with different settings, choose how to handle incoming requests when all child processes are busy, and more.

Remember, PHP-FPM doesn’t serve the PHP files by itself. It needs a web server, like Nginx or Apache, to pass the requests to it. It then processes those requests and returns them back to the web server.