relationship between fastcgi and PHP-FPMCgi
The CGI name is a "public Gateway Interface" (Common Gateway Interface), a tool that the HTTP server "chats" with programs on your or other machines, and its programs must be run on a network server.
CGI can be written in any language, as long as the language has standard input, output, and environment variables. such as PHP,PERL,TCL and so on.
FastCGI
FastCGI is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it will not take a moment to fork once (this is the most notorious fork-and-execute mode of CGI). It also supports distributed operations where the FastCGI program can execute and accept requests from other Web servers on hosts other than the Web server.
FastCGI is a language-independent, extensible architecture for CGI open extensions 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 of poor CGI performance, if the CGI interpreter remains in memory and accepts the FASTCGI process manager scheduling, it can provide good performance, scalability, fail-over characteristics and so on.
FASTCGI Features
- FastCGI is language agnostic.
- FASTCGI applications in the process, run independently of the core Web server, provide a more secure environment than the API. APIs link the application's code with the core Web server, which means that an application in the wrong API can corrupt other applications or core servers. The application code of a malicious API can even steal a key from another application or core server.
- FASTCGI technology currently supports languages such as C + +, Java, Perl, TCL, Python, SmallTalk, Ruby, and so on. Related modules are also available on popular servers such as Apache, ISS, lighttpd and more.
- FastCGI does not depend on the internal architecture of any Web server, so fastcgi remains stable even if the server technology changes.
How the FastCGI works
- Load fastcgi Process Manager (IIS ISAPI or Apache Module) when Web server starts
- The FASTCGI process Manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.
- When a client request arrives at the Web server, the FASTCGI process manager selects and connects to a CGI interpreter. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.
- The FASTCGI child process returns standard output and error information from the same connection to the Web Server after processing is complete. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here.
In the above scenario, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all extensions, and initialize all data structures. With FastCGI, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.
At the beginning of this problem I also quite tangled, read the "HTTP authoritative guide" after the feeling clear a lot.
First of all, what does CGI do? CGI is designed to ensure that the data passed by the Web server is in a standard format and facilitates the writer of CGI programs.
Web server, such as Nginx, is simply the publisher of the content. For example, if the request /index.php , according to the configuration file, Nginx know that this is not a static file, need to find a PHP parser to deal with, then he will make this request simple processing and handed over to the PHP parser. What data will the Nginx send to the PHP parser? URL to have it, query string also have to have, post data also have, HTTP header can not be less, good, CGI is to specify what data to pass, in what format to the rear processing the request of the Protocol. Think carefully about where the users you are using in your PHP code come from.
When Web server receives 好了,CGI是个协议,跟进程什么的没关系。那fastcgi又是什么呢?Fastcgi是用来提高CGI程序性能的。
Improve performance, what is the performance problem with CGI programs? "The PHP parser parses the php.ini file, initializes the execution environment," and here it is. The standard CGI performs these steps for each request (no idle fatigue!). Start the process very tired to say! ), so it takes longer to process each time. This is obviously unreasonable! So how did fastcgi do it? First, fastcgi initiates a master, parses the configuration file, initializes the execution environment, and then starts multiple workers. When the request comes in, master passes it to a worker, and then immediately accepts the next request. This avoids duplication of labor, and efficiency is naturally high. And when the worker is not enough, master can pre-boot several workers according to the configuration and so on, of course, the idle worker too much, will also stop some, this improves the performance, also saves the resources. This is FastCGI's management of the process.
What is that php-fpm? is a realization of the FASTCGI program, by the PHP official received.
As we all know, PHP's interpreter is php-cgi. PHP-CGI is just a CGI program, he himself can only parse the request, return the results, will not process management (the emperor, my concubine really do not AH!) So there are some programs that can dispatch php-cgi processes, such as spawn-fcgi separated by LIGHTHTTPD. Well php-fpm is such a thing, after a long period of development, gradually got everyone's approval (to know, in the previous few years everyone complained php-fpm stability is too poor), but also more and more popular.
Well, finally come back to your question.
Online some say that fastcgi is a protocol that PHP-FPM implemented this Protocol
Right.
Some say that PHP-FPM is the manager of the fastcgi process, used to manage the fastcgi process
Right. 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 the fastcgi is a protocol, it seems that no such process exists, even if there is php-fpm can not manage him (at least for now). Some say that php-fpm is a patch for the PHP kernel
Used to be right. Because PHP-FPM is not included in the PHP kernel at the beginning, to use this function, we need to find the same version of the source code php-fpm the kernel patch, and then compile. Later, after the PHP kernel integrates php-fpm, it is much easier to use有的说,修改了php.ini配置文件后,没办法平滑重启,所以就诞生了php-fpm
Yes, after modifying php.ini, the php-cgi process does not have the means to smooth the restart. PHP-FPM This mechanism is the new worker with a new configuration, the already existing workers can rest after processing the hands of the work, through this mechanism to smooth over.
Also say php-cgi is PHP comes with the fastcgi manager, then why do you get a php-fpm out
Wrong. PHP-CGI is just a program that explains PHP scripts.
PHP CGI fast-cgi php-fpm