Fastcgi and PHP-FPM

First of all, what does CGI do? CGI is to ensure that the data passed by the web server is in a standard format, which is convenient for the writers of CGI programs.

web server (such as nginx) is just a content distributor. For example, if you request /index.html, then the web server will find this file in the file system and send it to the browser, where static data is distributed. Well, if the request is now /index.php, according to the configuration file, nginx knows that this is not a static file and needs to be processed by a PHP parser, then He will simply handle the request and hand it over to the PHP parser. What data will Nginx send to the PHP parser? You must have the url, the query string, and the POST data as well. The HTTP header must not be less. Okay, CGI specifies what data to be transmitted and in what format it is passed to the back of the protocol for processing this request. Think about it carefully, where do the users you use in the PHP code come from.

When the web server receives /index.phpthis After the request, the corresponding CGI program will be started, here is the PHP parser. Next, the PHP parser will parse the php.ini file, initialize the execution environment, then process the request, and then return the processed result in the format specified by the CGI, and exit the process. The web server then returns the result to the browser.

Well, CGI is a protocol, it has nothing to do with the process. So what is fastcgi? Fastcgi is used to improve the performance of CGI programs.

Improve performance, so where is the performance problem of CGI program? “The PHP parser will parse the php.ini file and initialize the execution environment”, that’s it. Standard CGI will execute these steps for each request (not tired! Startup process is tired!), so it takes a long time to process each time. 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 the request comes, the master will pass it to a worker, and then immediately can accept the next request. In this way, repeated labor is avoided, and the efficiency is naturally high. And when the workers are not enough, the master can start several workers in advance 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 how fastcgi manages the process.

What is PHP-FPM? It is a program that implements Fastcgi and was officially accepted by PHP.

As everyone knows, the interpreter of PHP is php-cgi. php-cgi is just a CGI program. It can only parse the request and return the result. It does not manage the process (the emperor, the concubine really can’t do it!), so there are some programs that can schedule the php-cgi process. For example, spawn-fcgi separated by lighthttpd. Well, PHP-FPM is also such a stuff. 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, finally come back to your question. Some people on the Internet say that fastcgi is a protocol, and php-fpm implements this protocol

Yes.

Some people say that php-fpm is a fastcgi process manager, used to manage fastcgi processes

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 fastcgi is a protocol mentioned earlier, it seems that there is no such a process, even if there is php-fpm, it cannot be managed (at least for now). Some say that php-fpm is a patch of the php kernel

It was right before. Because php-fpm was not included in the PHP kernel at the beginning, to use this function, you need to find the same version of php-fpm as the source code to patch the kernel, and then compile it. Later, after the PHP kernel integrated PHP-FPM, it was much more convenient, using --enalbe-fpm This compilation parameter is fine.

Some people say that after modifying the php.ini configuration file, there is no way to restart smoothly, so php-fpm

Yes, after modifying php.ini , Php-cgi process is indeed unable to restart smoothly. The processing mechanism of php-fpm is that a new worker uses a new configuration, and the existing worker can rest after processing the work at hand, and smooth the transition through this mechanism.

Some people say that PHP-CGI is the FastCGI manager that comes with PHP, so why do you get another php-fpm out?

not right. php-cgi is just a program that interprets PHP scripts.

Leave a Comment

Your email address will not be published.