Why php-fpm Can Match nginx Performance: Architecture, Configuration, and Best Practices
This article explains php‑fpm’s architecture—prefork with async I/O, configurable event mechanisms, and its concurrency model identical to nginx—while offering practical configuration tips such as optimal worker counts and highlighting stability considerations compared to nginx and OpenResty.
Understanding php-fpm
PHP is a top‑ranked internet development tool. The term "PHP" can refer to two things:
Broadly, any script file ending with .php, i.e., the code written by developers.
Narrowly, the PHP deployment program such as LNMP, where "PHP" often means php‑fpm .
What is php-fpm?
php‑fpm is a built‑in PHP interpreter that uses a prefork + asynchronous I/O model, providing a high‑performance server program.
php‑fpm has been part of the PHP source package since version 5.3.3 (previously distributed as a patch). It offers the following core features:
Prefork model: a master process listens on a socket and forks multiple child processes.
Child processes handle client requests using asynchronous I/O.
The event model can be configured in php-fpm.conf.
;
Specify the event mechanism FPM will use. The following is available:
;- select (any POSIX os)
;- poll (any POSIX os)
;- epoll (linux >=2.5.44)
;- kqueue (FreeBSD >=4.1, OpenBSD >=2.9, NetBSD >=2.0)
;- /dev/poll (Solaris >=7)
;- port (Solaris >=10)
;- DefaultValue: not set (auto detection)
;events.mechanism = epollIf events.mechanism is not set, php‑fpm defaults to using epoll on Linux.
Performance and Configuration
The I/O model and concurrency handling of php‑fpm are essentially the same as those of nginx, which is renowned for its performance. Common misconfigurations include setting an excessively high pm.max_children value (e.g., 128+ on an 8 GB server). Recommended settings are:
nginx workers: 2 × CPU‑core count php‑fpm workers: 3 × CPU‑core count php‑fpm’s stability is slightly lower than nginx’s because each php‑fpm process runs a PHP script; poorly written scripts (infinite loops or blocking calls) can stall the process. In contrast, nginx communicates with backend servers over the network and can set timeouts to avoid being blocked.
FastCGI and Integration
php‑fpm uses FastCGI, which operates as a short‑lived connection: each request is processed and then the connection is closed.
Because php‑fpm is tightly bound to FastCGI, it cannot run independently of an HTTP server (e.g., nginx). Adding native HTTP support to php‑fpm could allow it to run standalone, potentially competing with Node.js.
Comparison with OpenResty
OpenResty extends nginx with a Lua interpreter, while php‑fpm embeds a PHP interpreter. Functionally they are similar, differing only in the scripting language used. OpenResty is useful as an nginx enhancement, but it is unnecessary to replace php‑fpm with OpenResty solely for language reasons.
Overall, php‑fpm’s architecture is highly efficient and often surpasses many Python deployment tools.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
