Understanding CGI, FastCGI, and PHP‑FPM: How Web Servers Process PHP Requests
This article explains the role of CGI as a protocol for passing request data, how FastCGI improves performance by reusing processes, and why PHP‑FPM is the widely adopted FastCGI implementation that enables smooth configuration reloads for PHP applications.
Initially I was confused about this topic, but after reading "The HTTP Definitive Guide" it became clearer.
First, what does CGI do? CGI ensures that data passed from the web server is in a standard format, making it easier for CGI program developers.
A web server (e.g., Nginx) is merely a content distributor. For a request like /index.html, it serves the static file. For /index.php, based on its configuration, Nginx knows the request needs a PHP interpreter, so it forwards the request along with the URL, query string, POST data, and HTTP headers. CGI defines which data to pass and in what format.
When the web server receives /index.php, it launches the corresponding CGI program—the PHP interpreter. The interpreter reads php.ini, initializes the execution environment, processes the request, returns the result in the CGI-specified format, and exits. The web server then sends the result to the browser.
CGI is a protocol, not tied to process management. FastCGI improves CGI performance. Standard CGI starts a new process for each request, which is costly. FastCGI starts a master process that initializes the environment once and then spawns multiple worker processes. The master distributes incoming requests to workers, allowing immediate handling of subsequent requests and scaling workers up or down as needed.
PHP‑FPM is an implementation of FastCGI maintained by the PHP community. The PHP interpreter php‑cgi is a CGI program that can only parse requests and return results; it lacks process management. Tools like spawn‑fcgi were created to manage php‑cgi processes. PHP‑FPM evolved, gained stability, and became the standard way to run PHP with FastCGI, enabled via the --enable‑fpm compile option.
Regarding common confusion: FastCGI is a protocol, while PHP‑FPM implements that protocol and manages php‑cgi workers. PHP‑FPM does not manage a non‑existent FastCGI process; it manages the workers that speak the FastCGI protocol. Historically, PHP‑FPM required patching the PHP source, but it is now integrated into the core.
When php.ini is changed, php‑cgi cannot reload smoothly. PHP‑FPM solves this by spawning new workers with the updated configuration while letting existing workers finish their current tasks, achieving a graceful restart.
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
