Skip to main content

Review the basics of CGI, FastCGI, php-cgi, and php-fpm

· 4 min read
zenge
Software Rookie @ China

php Organize an answer from Zhihu

1、CGI is a protocol

Firstly, what is CGI? CGI is designed to ensure that the data passed from the web server is in a standard format, making it convenient for CGI program developers.

The web server (such as nginx) is just a content distributor. For example, if the request is /index.html, the web server will go to the file system to find this file and send it to the browser, and the data distributed here is static. Well, if the request is /index.php, according to the configuration file, nginx knows that it is not a static file and needs to find a PHP parser to process it, so it will pass this request to the PHP parser after a simple processing. What data will Nginx pass to the PHP parser? The URL should be there, the query string should be there, the POST data should be there, and the HTTP header cannot be missing. Good, CGI is a protocol that specifies which data needs to be passed and in what format to pass it to the backend for processing this request. Think about it, where does the data you use in PHP code come from.

After receiving the request /index.php, the web server will start the corresponding CGI program, which is the PHP parser here. Next, the PHP parser will parse the php.ini file, initialize the execution environment, process the request, and then return the processed result in the format specified by the CGI protocol, and exit the process. The web server will then return the result to the browser. Well, CGI is a protocol, and it has nothing to do with processes.

2、FastCGI is also a protocol

What is FastCGI? FastCGI is an improved protocol used to enhance the performance of the CGI protocol.

How to improve performance? Where are the performance issues of the CGI program? "The PHP parser parses the php.ini file and initializes the execution environment," this is where it is. The standard CGI executes these steps for each request (not tired at all! Starting a process is really tiring!), so it takes a long time to process each request. This is obviously unreasonable. So how does FastCGI do it? First, FastCGI starts a master, parses the configuration file, initializes the execution environment, and then starts multiple workers. When a request comes in, the master passes it to a worker and can immediately accept the next request. This avoids redundant work and improves efficiency. Moreover, when there are not enough workers, the master can start several workers in advance according to the configuration; of course, when there are too many idle workers, some will be stopped, which improves performance and save resources. This is how FastCGI manages processes.

Two protocols, each with its own implementation program.

3、php-cgi implements CGI

php-cgi is a program that implements the CGI protocol, and the PHP interpreter is php-cgi. It itself can only parse requests and return results, without process management.

4、PHP-FPM implements FastCGI

A program that can schedule php-cgi processes, such as spawn-fcgi separated from lighthttpd. PHP-FPM is also such a thing, a program that implements the FastCGI protocol, which has gradually gained recognition after long-term development (you know, a few years ago, people were complaining about the poor stability of PHP-FPM), and is also becoming more and more popular, having been adopted by the PHP official team.

The PHP kernel is already integrated with PHP-FPM, and the compilation parameter --enalbe-fpm is sufficient.

Some say that after modifying the php.ini configuration file, it's impossible to restart smoothly, which is why php-fpm was born?

Yes, the php-cgi process does not restart smoothly after modifying the php.ini. The mechanism of php-fpm to deal with this is that the new worker uses the new configuration, and the existing worker can rest after processing the work at hand, and the transition is smoothed out through this mechanism.