How Linux Handles PHP-FPM for Web Content

This article explains how the Linux operating system manages the PHP FastCGI Process Manager (PHP-FPM) daemon to serve dynamic web pages. It details the interaction between the Linux kernel, system service managers, and web servers like Nginx or Apache. You will learn about process lifecycle management, inter-process communication via sockets, pool execution models, and Linux-level resource allocation.

Process Initialization via systemd

In modern Linux distributions, PHP-FPM is managed as a background daemon by systemd. When the operating system boots or the service is manually started, systemd reads the unit configuration file (typically located at /lib/systemd/system/php-fpm.service) and executes the primary PHP-FPM binary.

The initial process launched is the Master Process, which typically runs with root privileges. Running as root allows the master process to open privileged network ports, bind to restricted Unix domain sockets, and allocate shared memory regions before creating less-privileged child processes.

Master and Worker Architecture

Linux handles PHP-FPM workloads using a master-worker process model:

Inter-Process Communication (IPC)

The Linux kernel acts as the transport layer between the upstream web server (such as Nginx or Apache) and the PHP-FPM workers. When a request for a dynamic file is received, the web server passes it to PHP-FPM using one of two methods:

  1. Unix Domain Sockets: A file-based communication channel (e.g., /run/php/php-fpm.sock) residing entirely within the Linux Virtual File System (VFS). Unix sockets avoid network protocol overhead, bypassing the TCP/IP stack, checksums, and packet wrapping. This method is preferred when the web server and PHP-FPM run on the same Linux host due to reduced CPU overhead and lower latency.
  2. TCP/IP Sockets: Network-based sockets bound to an IP address and port (e.g., 127.0.0.1:9000). This routing utilizes the Linux loopback network interface. While it incurs slight protocol overhead, it allows the web server and PHP-FPM daemon to run on completely separate Linux instances.

Process Management Modes

The Linux kernel schedules worker processes according to the configuration defined in the PHP-FPM pool configuration files. PHP-FPM supports three execution modes that dictate how Linux handles process creation:

Kernel Resource Allocation and Scheduling

When an HTTP request is forwarded through the socket, the Linux kernel's Completely Fair Scheduler (CFS) allocates CPU time slices to the assigned PHP-FPM worker process.

Linux manages these processes using standard kernel-level safeguards: