The difference between CGI, fpm, FastCGI, php-fpm, and swoole (to be continued)

Before talking about Fastcgi, you need to talk about CGI. CGI is to ensure that the data passed by the web server is in a standard format. It is a protocol that is convenient for the writers of CGI programs.
Fastcgi is a more advanced way of CGI, which is used to improve the performance of CGI programs.
A web server (such as 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 resources are distributed. 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. At this time, CGI is the protocol that specifies what data/format to be transmitted to the php parser.
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 CGI, and exit the process. The web server then returns the result to the browser.

So where is the performance bottleneck of CGI compared to Fastcgi? CGI forks a new process for each http request to process. The process includes parsing the php.ini file, initializing the execution environment, etc. Then this process returns the processed data to the web server, and finally the web server sends the content To the user, the process that was just forked also exits. If the user requests dynamic resources next time, the web server forks a new process again, and it goes on again and again.

Fastcgi will first fork a master, parse the configuration file, initialize the execution environment, and then fork 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 Fastcgi's process management.

Most Fastcgi implementations maintain a process pool. Note: Swoole, as httpserver, actually works like this. So what is PHP-FPM? It is a program that implements the Fastcgi protocol and is used to manage the process started by Fastcgi, that is, a program that can schedule the php-cgi process. Now that PHP-FPM is integrated in the PHP kernel, just use the compilation parameter –enalbe-fpm. In addition, after modifying the php.ini configuration file, there is no way to restart smoothly. You need to restart php-fpm. At this time, the worker of the new fork will use the new configuration, and the existing worker will continue to finish the work at hand.

Guess you like

Origin blog.csdn.net/EasyTure/article/details/112604639