Research on the relationship between FastCGI and PHP-FPM 1. CGI is to ensure that the data transmitted by the web server is standard format, convenient CGI program writers. (Understood as an agreement)
Web server (such as nginx) is only the content distributor. For example, if you request/index.html, the web server will find the file in the file system and send it to the browser. static data is distributed here. Okay, if the current request is/index. php: according to the configuration file, nginx knows that this is not a static file and needs to be handled by the PHP parser. then nginx will simply process this request and hand it over to the PHP parser. What data will Nginx transmit to the PHP parser? There must be a url, a query string, or a POST data, and a few HTTP headers. okay, CGI is the protocol that specifies the data to be transmitted and the format in which the request is sent to the backend. Think about where the users you use in PHP code come from.
When the web server receives the/index. php request, it starts the corresponding CGI program, which is the PHP parser. Next, the PHP parser parses the php. ini file, initializes the execution environment, processes the request, returns the processed result in the format specified by CGI, and exits the process. The web server then returns the result to the browser.
2. Fastcgi is used to improve CGI program performance.
To improve the performance, what is the problem with the CGI program performance? "The PHP parser parses the php. ini file and initializes the execution environment. The standard CGI performs these steps for each request! I am tired of starting the process !), Therefore, the processing time is 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 accept the next request. In this way, repetitive work is avoided, and the efficiency is naturally high. In addition, when the worker is not enough, the master can start several workers in advance according to the configuration. of course, when there are too many idle workers, some will also be stopped, which improves performance and saves resources. This is how fastcgi manages processes.
3. PHP-FPM is a program that implements Fastcgi.
As we all know, the interpreter for PHP is php-cgi. Php-cgi is just a CGI program. it can only parse requests, return results, and no process management. (Huang Shang, Chen really cannot do it !) So there are some programs that can schedule the php-cgi process, such as spawn-fcgi separated by lighthttpd. Good PHP-FPM is such a Dongdong, after a long period of development, gradually get everyone's approval (you know, a few years ago, but complained that PHP-FPM stability is too bad), is becoming more and more popular.
4. fastcgi is a protocol, which is implemented by php-fpm.