First, we introduce some concepts to understand CGI and FastCGI
Cgi
The Universal Gateway Interface (Common Gateway interface/cgi) is an important Internet technology that allows a client to request data from a Web browser to a program executing on a network server. CGI describes a standard for transferring data between a server and a request handler.
FastCGI
The Fast Universal Gateway Interface (Fast Common gateway interface/fastcgi) is a protocol that allows an interaction program to communicate with a Web server. FASTCGI is an enhanced version of the early Universal Gateway Interface (CGI).
FASTCGI is dedicated to reducing the overhead of interacting between Web servers and CGI programs, allowing the server to process more Web requests at the same time.
Okay, now I get it.
CGI and FastCGI are a communication protocol specification, not an entity
We usually need to use the word specification, CGI or CGI programs.
CGI programs and FASTCGI programs are programs that implement these two protocols, which can be implemented in any language. (PHP-CGI and PHP-FPM are the programs that implement FASTCGI)
Why do I recommend using the FASTCGI program
In addition to the description of the two protocols, here is a supplementary to the difference between the programs they implement.
About CGI Programs
CGI makes it possible for external programs to interact with Web servers. CGI programs run in separate processes and create a process for each Web request, which is easy to implement, but inefficient and difficult to scale. In the face of a large number of requests, the process of the establishment and extinction of the operating system greatly reduced performance. In addition, resource reuse is restricted because the address space cannot be shared.
About the FASTCGI program
Unlike creating a new process for each request, FASTCGI uses a continuous process to process a series of requests. These processes are managed by the FASTCGI server, not the Web server. When a request comes in, the Web server passes the environment variable and this page request through a socket such as FASTCGI process and Web server (both located locally) or a TCP connection (fastcgi process on the remote server Farm) passed to the fastcgi process.
FastCGI to improve the efficiency of CGI.
The difference between php-cgi and PHP-FPM
PHP-CGI is the fastcgi Manager with PHP. Start php-cgi, using the following command:
Php-cgi-b 127.0.0.1:9000
Php-cgi and php-fpm like, is also a fastcgi process manager, php-cgi problem is 1, php-cgi change php.ini configuration need to restart php-cgi to make new php-ini effective, not smooth restart 2, Directly kill the php-cgi process, PHP will not run. (PHP-FPM and spawn-fcgi do not have this problem, the daemon will smoothly regenerate new child processes.) ) In view of the php-cgi deficiency, PHP-FPM came into being.
The PHP-FPM management object is php-cgi. Using PHP-FPM to control the fastcgi process of php-cgi
Nginx How to invoke PHP
Web servers, such as Nginx, are just the distributors of content. For example, if the request/index.html, then the Web server will go to the file system to find this file, sent to the browser, where the distribution of static data. Well, if the request is now/index.php, according to the configuration file, Nginx know this is not a static file, need to find a PHP parser to deal with, then he will be simple processing of this request to the PHP parser. What data will nginx pass to the PHP parser? URL to have it, query string must have it, post data must have, HTTP header can not be less, good, CGI is to specify what data to pass, in what format to pass to the back of the Protocol to deal with this request. Think carefully about where the users you're using in your PHP code come from.
When the Web server receives the/index.php request, it starts the corresponding CGI program, which is the parser for PHP. The PHP parser then parses the php.ini file, initializes the execution environment, and then processes the request, then returns the processed result in the format specified in the CGI, exiting the process. The Web server then returns the results to the browser.
How Apache invokes PHP
Apache has a mod_php extension. PHP is a plug-in program for Apache, you must rely on the Web server to run. When the client browser triggers the event--->url submitted to the Apache server---->apache Server according to the characteristics of the PHP program is PHP program, submitted to the PHP engine program--->php engine program parsing and read the database to generate the corresponding page
fastcgi operation mode Analysis:
The working principle of fastcgi is:
(1), when the Web Server starts loading FastCGI process Manager "PHP FastCGI process Manager is PHP-FPM (php-fastcgi process Manager)" (IIS ISAPI or Apache Module);
(2), fastcgi the process manager itself, initiates multiple CGI interpreter processes (visible in Task Manager Php-cgi.exe) and waits for a connection from the Web server.
(3) When the client requests to reach the Web server, the FASTCGI process manager selects and connects to a CGI interpreter. The WEB server sends CGI environment variables and standard input to the fastcgi subprocess Php-cgi.exe.
(4) The FASTCGI process completes processing and returns standard output and error information from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed. The fastcgi process then waits and processes the next connection from the FASTCGI process Manager (running in webserver). In the normal CGI mode, Php-cgi.exe quits here.
In the above scenario, you can imagine how slow CGI is usually. Each Web request PHP must reparse php.ini, reload all DLL extensions, and reinitialize all data structures. With fastcgi, all of these occur only once when the process is started. An additional benefit is that persistent database connections (persistent DB connection) can work.
Why use FASTCGI instead of multithreaded CGI interpreter?
This may be a multifaceted consideration, for example:
(1) You cannot, in any case, stabilize the use of multithreaded CGI interpreters on Windows platforms, whether IIS ISAPI or Apache module, they always run for a period of time and crash. Is that weird? But it does exist!
Of course, there are many times when you can stabilize the use of multithreaded CGI interpreter, but you may find that the Web page sometimes error, in any case can not find the reason, and the fastcgi way to change the probability of this error will be greatly reduced. I'm not sure why, but I think the CGI interpreter for the standalone address space may eventually be a little bit more stable than the form of the shared address space.
(2), Performance! Performance? Is it possible that fastcgi is faster than a multithreaded CGI interpreter? But sometimes this is true, only testing your site, can be final conclusion. Why, I find it difficult to say, but there is information that in the era of Zend Winenabler, Zend is also recommended to use the Windows platform fastcgi instead of IIS ISAPI or Apache Module, but now Zend does not do this product.
Advantages of running PHP in FastCGI mode:
There are several main benefits to running PHP in FastCGI mode. The first is that PHP does not break down the Apache when it is wrong, but PHP's own process is lost (but FastCGI will immediately restart a new PHP process to replace the lost process). Second, FastCGI mode runs better than ISAPI mode (I used to test with apachebench, but I forgot to save the results and you are interested in testing it yourself).