Question: Some people on the Internet say that fastcgi is a protocol, and php-fpm implements this protocol; some say that php-fpm is the manager of the fastcgi process, used to manage the fastcgi process; Some say that php-fpm is a patch for the PHP kernel; some say that after modifying the php.ini configuration file, there is no way to restart smoothly, so php-fpm was born; others say that PHP-CGI is a version of PHP. FastCGI Manager with
First of all, what is CGI? CGI is to ensure that the data passed by the web server is in a standard format, which is convenient for writers of CGI programs.
The web server (such as nginx) is just a distributor of content. For example, if /index.html
is requested, the web server will find the file in the file system and send it to the browser. What is distributed here is static data. Okay, if the request now is /index.php
, according to the configuration file, nginx knows that this is not a static file and needs to be processed by the PHP parser, then it will simply process the request and hand it over to the PHP parser. What data will Nginx pass to the PHP parser? The URL must be present, the query string must be present, the POST data must be present, and the HTTP header must be present. Well, CGI is the protocol that stipulates what data is to be transmitted and in what format to be passed to the backend for processing the request. Think carefully about where the users you use in your PHP code come from.
When the web server receives the /index.php
request, it will start the corresponding CGI program, which is the PHP parser. Next, the PHP parser will parse the php.ini file, initialize the execution environment, process the request, return the processed result in the format specified by CGI, and exit the process. The web server then returns the results to the browser.
Well, CGI is a protocol and has nothing to do with processes or anything like that. So what is fastcgi? Fastcgi is used to improve the performance of CGI programs.
Improve performance, so what are the performance problems of CGI programs? "The PHP parser will parse the php.ini file and initialize the execution environment", that's it. Standard CGI will perform these steps for each request (don't be tired! Starting the process is very tiring!), so the time to process each time will be relatively long. This is obviously unreasonable! So how does Fastcgi do it? First, Fastcgi will start a master, parse the configuration file, initialize the execution environment, and then start multiple workers. When a request comes in, the master will pass it to a worker, and then the next request can be accepted immediately. This avoids duplication of work and is naturally highly efficient. And when there are not enough workers, the master can pre-start several workers according to the configuration and wait; of course, when there are too many idle workers, some will be stopped, which improves performance and saves resources. This is fastcgi's process management.
So what is PHP-FPM? It is a program that implements Fastcgi and was officially accepted by PHP.
As we all know, the interpreter of PHP is php-cgi. php-cgi is just a CGI program. It can only parse requests and return results, but does not know how to manage processes (Your Majesty, I really can’t do that!) So there are some programs that can schedule php-cgi processes. For example, spawn-fcgi is separated from lighthttpd. Well, PHP-FPM is also like this. After a long period of development, it has gradually been recognized by everyone (you know, in the past few years, everyone complained about the poor stability of PHP-FPM), and it has become more and more popular.
Okay, let’s finally come back to your question.
1. Some people on the Internet say that fastcgi is a protocol, and php-fpm implements this protocol
Yes.
2. Some say that php-fpm is the manager of fastcgi process, which is used to manage fastcgi process
Yes. The management object of php-fpm is php-cgi. But it cannot be said that php-fpm is the manager of the fastcgi process, because as mentioned earlier, fastcgi is a protocol, and it seems that no such process exists. Even if php-fpm exists, it cannot manage it (at least for now). Some people say that php-fpm is a patch of the PHP kernel. This was true before. Because php-fpm was not included in the PHP kernel at the beginning, to use this function, you need to find php-fpm that is the same as the source code version, patch the kernel, and then compile it. Later, it became much more convenient after the PHP kernel integrated PHP-FPM. Just use the --enalbe-fpm
compilation parameter.
3. Some people say that after modifying the php.ini configuration file, there is no way to restart smoothly, so php-fpm was born
Yes, after modifying php.ini, the php-cgi process cannot be restarted smoothly. The processing mechanism of php-fpm is that new workers use new configurations, and existing workers can rest after finishing the work at hand. This mechanism is used to smooth the transition.
4. Others say that PHP-CGI is the FastCGI manager that comes with PHP. If so, why create php-fpm?
No. php-cgi is just a program that interprets PHP scripts.
Original text excerpted from: https://segmentfault.com/q/1010000000256516