
I was actually questioning what happens when I execute a PHP script or when an http request reaches Apache or Nginx.
So I went deep into this a bit; the web server receives the hhtp request first. If the request is for a .php file, it forwards it to PHP-FPM (FastCGI Process Manager), which executes the PHP script and returns the result back to the web server. The web server then sends the final http response to the client (browser).
The request flow looks something like this:
An http request from the browser sent, apache/nginx receive it, the PHP-FPM process it and execute it, and it comes back with the results to apache/nginx, then sends the results to the browser.
Why is PHP-FPM and why we did not execute the request directly into the server?
– It manages a pool of PHP worker processes, making request handling fast and efficient.
– It can serve multiple applications (multi-tenant setups) using separate pools, each with its own PHP version (if you’re in such cases), user, memory limits, and configuration.
– It provides better performance and resource management than older PHP execution ways.
I also sketched the flow in a simple diagram for my own reference so I can come back to it whenever needed. It might explain things better than the text.