The relationship between cgi, fastcgi, php-cgi, php-fpm

Reference article:

https://segmentfault.com/q/1010000000256516

http://www.thinkphp.cn/topic/42338.html

This article is well written: https://blog.csdn.net/belen_xue/article/details/65950658

cgi

cgi : The full name is " Common Gateway Interface " (Common GatewayInterface) , a tool for the HTTP server to " talk " with programs on your or other machines , and the programs must run on the network server.

( My understanding is that cgi is a protocol that specifies the format of the data sent by the server )

 

What is CGI for? CGI is to ensure that the data passed by the web server is in a standard format, which is convenient for the writer of CGI programs.

A web serv (eg nginx ) is just a 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 now /index.php , according to the configuration file, nginx knows that this is not a static file and needs to find a PHP parser to process it, then he will simply process the request and hand it over to the PHP parser. What data will Nginx pass to the PHP parser? It is necessary to have the url , the query string, the POST data, and the HTTP header . Okay, CGI is the protocol that specifies what data to transmit and in what format to pass the request to the rear. Think carefully, where do 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 the CGI , and exit the process. The web server then returns the result to the browser.

fastcgi

fastcgi: A resident (long - live) CGI , it can be executed all the time, as long as it is activated, it will not take time to fork every time (this is the most criticized fork-and-execute mode of CGI ) . It also supports distributed computing, that is, FastCGI programs can execute on hosts other than web servers and accept requests from other web servers. ( My understanding is fastcgi is an optimized CGI protocol )


FastCGI is a language-independent, scalable architecture open extension to CGI whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. As we all know, the repeated loading of CGI interpreter is the main reason for the low performance of CGI. If the CGI interpreter is kept in memory and is scheduled by the FastCGI process manager, it can provide good performance, scalability, Fail-Over characteristics and so on.


FastCGI can also be called a protocol standard. For example, php-fpm to be mentioned below is a fastCGI process manager / engine that supports parsing php .


Fastcgi is used to improve the performance of CGI programs.

Improve performance, then where is the performance problem of CGI programs? "The PHP parser will parse the php.ini file and initialize the execution environment " , that's it. Standard CGI performs these steps for each request (not idle! Starting the process is very tiring!), so the processing time of each time will be longer. 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 passes it on to a worker , which can then accept the next request immediately. In this way, repeated work is avoided, and the efficiency is naturally high. And when there are not enough workers , the master can start several workers in advance and wait according to the configuration; of course, when there are too many idle workers , it will stop some, which improves performance and saves resources. This is fastcgi 's management of the process.

 

php-cgi

The interpreter for PHP is php -cgi . php-cgi is just a CGI program, he himself can only parse the request, return the result, and can't 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.

php-fpm

php-fpm is the manager of the fastcgi process, which is used to manage the fastcgi process

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. It seems that no such process exists, and even if php-fpm exists, it cannot be managed (at least for now). Some 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 php-fpm version as the source code , patch the kernel, and then compile it. Later , after the PHP kernel integrates PHP-FPM , it is much more convenient to use the --enalbe-fpm compilation parameter.

After modifying php.ini , the php-cgi process cannot be restarted smoothly. The processing mechanism of php-fpm is that the new worker uses a new configuration, and the existing worker can rest after processing the work at hand. This mechanism is used to smooth the transition.

 

 

Example: What is the difference between cgi and fastcgi ?

The principle is the same, all use standard input and output streams to process text protocols such as HTTP , and all process multiple requests through multi-process mode. The difference is that a process of FastCGI resets the state and suspends after processing a request, waiting for the next request to continue processing; while a process of CGI exits after processing a request, and creates a new one for the next request. process.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325852595&siteId=291194637