What is the relationship between FastCGI and PHP-FPM?

I checked the relationship between fastcgi and php-fpm on the Internet. All over, there are really different opinions, and there is no authoritative definition.

Some people on the Internet say that fastcgi is a protocol , Php-fpm implements this protocol; some say, php-fpm is a fastcgi process manager, used to manage fastcgi processes; some say, php-fpm is a patch of the php kernel; some say, modified After the php.ini configuration file, there is no way to restart smoothly, so php-fpm was born; others say that PHP-CGI is the FastCGI manager that comes with PHP, so why do you get php-fpm out again? More dizzy;

Post a post and want to hear Listen to everyone’s understanding, I have read it all online, because I have been checking it for a week, haha, so I want to hear the original understanding.

At the beginning, I was quite entangled with this issue. I read the “HTTP Authoritative Guide After “, I feel a lot clearer.

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 (for example, nginx) is only the distributor of content. 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 go to a PHP parser to process it, then he The request will be simply processed and handed 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.php after this request , Will start the corresponding CGI program, 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.

Okay, CGI is a The agreement has nothing to do with the process. So what is fastcgi? Fastcgi is used to improve the performance of CGI programs.

Improve performance, so what 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.

Then PHP-FPM what is it then? It is a program that implements Fastcgi and was officially accepted by PHP.

As you all know, 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, come finally Come back to your question.
Some on the Internet say that fastcgi is a protocol, and php-fpm implements this protocol

Correct.

Some say, php -fpm is the manager of the fastcgi process, used to manage the 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 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

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 sufficient.

Some say, modify After adding the php.ini configuration file, there was no way to restart smoothly, so php-fpm was born

Yes, modify After php.ini, the 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.

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

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

Leave a Comment

Your email address will not be published.