The relationship between Fastcgi and PHP-FPM

The original text is shared from the answer of segmentfault @尹川, I am not sure what kind of relationship is between FastCgi and PHP-fpm

If infringement is involved, please contact me to delete< /p>


I checked the relationship between fastcgi and php-fpm on the Internet, and I checked it for almost a week, and I basically read it all over. There are really different opinions, and there is no authoritative definition.

  • Some people on the Internet say that fastcgi is a protocol, and php-fpm implements this protocol;
  • Some people say that php-fpm is a fastcgi process manager. To manage the fastcgi process;
  • Some say that php-fpm is a patch of the php kernel;
  • Some say that after modifying the php.ini configuration file, there is no way to smooth it Restart, so php-fpm was born;
  • Some people say that PHP-CGI is the FastCGI manager that comes with PHP, so why do you get php-fpm again?

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 the distributor of content. For example, if /index.html is requested, 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 /index.php, according to the configuration file, nginx knows that this is not a static file and needs to find a PHP parser to handle it, then he will simply process 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. The HTTP header must not be less. Okay, CGI specifies what data to be transmitted and in what format. The agreement for the latter to process this request.
  • When the web server receives the /index.php request, it 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 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 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.

What is PHP-FPM?

  • It is a program that implements Fastcgi, which 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 the above question.

  1. Some people on the Internet say that fastcgi is a protocol, and php-fpm implements this protocol

Yes.

  1. 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).

  1. Some people say that php-fpm is a patch for 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 integrates PHP-FPM, it is much more convenient, just use the compilation parameter –enalbe-fpm.

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

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

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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1641 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.