Analysis of CGI fastCgi php-fpm PHP-CGI

Analysis of CGI fastCgi php-fpm PHP-CGI

  • nginx in the LNMP environment does not support php, and the fastcgi plug-in needs to be used to process php-related requests. And php needs the php-fpm component to provide this function. The version of php-fpm before php5.3.3 existed in the form of a patch package, but after php5.3.3, you only need to use --enable-fpm to load the module when compiling and installing, and there is no need to install it separately.
  • A web server (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 it to the rear to process the request. 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.
  • 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 all. 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 will pass it on to a worker, and then immediately can accept the next request. 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.
  • As we all know, 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.
  • 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.
  • Fastcgi is an upgraded version of CGI, a language-independent protocol used to communicate with programs (such as PHP, Python, Java) and Web servers (Apache2, Nginx). In theory, programs written in any language can provide Web services through Fastcgi .
    The characteristic of Fastcgi is that multiple requests will be completed in turn in a process to achieve the purpose of improving efficiency. Most Fastcgi implementations will maintain a process pool.
    PHP-fpm is an implementation of Fastcgi for PHP, which is responsible for managing a process pool to handle requests from web servers. Currently, PHP-fpm is built into PHP.
    But PHP-fpm is just a "PHP Fastcgi Process Manager", it still calls the PHP interpreter itself to process the request, the PHP interpreter (under Windows) is php-cgi.exe.
  • fastcgi is a communication protocol between app server and web server. The normal architecture app server is master, web server is client
    php-fpm with two functions: 1. Implemented a server program that supports fastcgi protocol 2. Process manager
    With php-fpm, you can turn php scripts into multi-process Mode, the app server using the fastcgi protocol communicates with the web server

Reference: https://segmentfault.com/q/1010000000256516

Guess you like

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