How Apache Working
Because Apache is a web server, it is directly connected to the kernel. It listens on a port (let's say 80 or 443) and the kernel passes the incoming requests to it.
Now, the Apache Master process receives the request. Based on .htaccess and other server settings, it passes the request to an Apache Worker process. That's it.
Note: By default, a lot of Apache worker processes are already spun up and in standby mode. Any incoming request is immediately assigned to an available worker process. If all workers are busy, Apache will create a new one.
How PHP MOD Working
Here is where the PHP MOD workflow starts. Because PHP MOD is bundled with the Apache worker process, the PHP interpreter lives inside the Apache worker process itself. This means they all live in the RAM already.
Now, what happens if any request comes in? Whether the user wants to use the PHP interpreter or not, the request reaches the Apache worker process. If any static file work needs to happen, that process does it. If any PHP file work needs to happen, that same process does it too.
How PHP FPM Working
In PHP FPM, the PHP engine is completely independent of the Apache worker. Because of this, an Apache worker process stays a lightweight process (could be around 5 to 10 MB).
When the Apache worker process assigns the work, it checks whether the incoming request is for a static file or a PHP file:
- If the request is for a static file: It does not disturb the PHP FPM Master process at all. The Apache worker process goes to the directory, picks up the file, and responds to the user.
- If the request is for a PHP file: It passes that request to the PHP FPM Master process and immediately returns to an idle position.
Now, the PHP FPM Master process acts as the manager. It does not do any of the actual coding work itself; it just keeps an eye on all the other PHP FPM Worker processes. It assigns that work to one of the workers because that Worker process pool is already spun up and waiting. The actual work is completely handled right here by the PHP FPM Worker process.
Now The real question arrive: if both spin up a process and that process live in ram, so how php fpm save memory and php mod, didn't
As I said above, in PHP MOD, the PHP engine is bundled with the Apache worker. Suppose 10 static file requests and 10 PHP file requests come in at the exact same time. In MOD PHP, we would have to use 20 Apache worker processes. Let's say each process takes 50 MB of RAM:
- Total: 50 MB * 20 = 1000 MB
Now, the exact same thing happens in PHP FPM. A request arrives for 10 static files and 10 PHP files. What happens here is that only 10 PHP FPM worker threads actually work on the PHP scripts. Let's say they take 30 MB in size:
- Total: 10 * 30 MB = 300 MB
The remaining 10 requests are for static files. For these, no PHP FPM worker process is requested! The Apache worker thread goes directly to the directory, grabs the file, and responds. Let's say these lightweight threads take around 2 MB in size:
- Total: 10 * 2 MB = 20 MB
So, in PHP FPM, we consume only 300 MB + 20 MB = 320 MB.
ONE most important that php fpm and php mod difference, you can see when server is under pressure, means they use more then worker pool process, because if they use only worker process they lived in the ram, so they can't affect overall memory consumption