CGI review

Reference:

Explain the difference between fastcgi and php-fpm: http://segmentfault.com/q/1010000000256516

What is CGI, FastCGI, PHP-CGI, PHP-FPM, Spawn-FCGI? :Http://www.mike.org.cn/articles/what-is-cgi-fastcgi-php-fpm-spawn-fcgi/

一、cgi,fastcgi,php-cgi,php- Fpm connection and difference:
cgi and fastcig are protocols, fastcgi adds scheduling function;
php-cgi is the interpreter of php;
php-fpm is a code implementation of the fastcgi protocol.

At the beginning, I was quite entangled with this issue. After reading the “HTTP Authoritative Guide”, I felt 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.

The web server (such as nginx) is just 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 will simply process the request and hand it in 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 to pass to 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 the request of /index.php, 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 perform these steps for each request (not idle! Startup process is very tiring!), so the processing time for each time will be relatively long. This is obviously unreasonable! So how does Fastcgi do it? First, Fastcgi will first 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, PHP’s interpreter 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 thing. 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 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 integrates PHP-FPM, it is much more convenient, just use the compilation parameter --enalbe-fpm.

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 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?

< blockquote>

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

Leave a Comment

Your email address will not be published.