Web server (such as nginx) is only the content distributor. For example, if the 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 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/index.php
After this request, the corresponding CGI program will be started. Here 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.
Okay, CGI is a protocol. It has nothing to do with the process. What is fastcgi? 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.
What is PHP-FPM? It is a program that implements Fastcgi and is officially accepted by PHP.
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.
Okay. Come back to your question. Some people on the Internet say that fastcgi is a protocol, which is implemented by php-fpm.
Yes.
Some say that php-fpm is the manager of fastcgi processes and is 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, and it seems that no such process exists, even if php-fpm exists, it cannot be managed (at least currently ). Some say that php-fpm is a patch of the php kernel.
Previously, it was correct. 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 to patch the kernel and then compile it. Later PHP kernel integrated with the PHP-FPM will be more convenient to use--enalbe-fpm
Compile the parameter.
Some say that the php. ini configuration file cannot be restarted smoothly, so php-fpm is born.
Yes. 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 stop working after it completes processing. This mechanism is used to smooth the process.
There is also said that the PHP-CGI is the FastCGI manager of PHP, So why then get a php-fpm out
No. Php-cgi is just a program that explains PHP scripts.
Popular analogy:
You (PHP) are going to talk to the eskimoo people (web servers, such as Apache and Nginx) about business.
If you speak Chinese (PHP code), he speaks eskimoo (C code) and cannot understand each other. What should I do? Then we can convert our speech into English (FastCGI protocol.
How to convert it? You need to use a translation machine (PHP-FPM) (of course the other Party also has a translation machine, that is his own)
Our translation machine is the newest, and the old-fashioned one (PHP-CGI) has been eliminated. But it (PHP-FPM) will only be used by young people (Linux systems), old people (Windows systems) won't play with it, so they have to continue with the old one.