When a Web server accepts a request from a browser, it is returned directly to the browser if it is a static resource, and if it is a dynamic resource, CGI can be understood as a protocol or a class of handlers that can execute a php or Python script.
Web server exec Such a process, and then give him some input parameters, he slowly finished the results back to the Web server, and from the protocol level, the CGI protocol is to standardize the Web server and CGI program some of the input and output parameters meaning.
The CGI (Common Gateway Interface) was developed by the U.S. National Supercomputer Application Center (NCSA) for NCSA HTTPd WEB servers in 1993. The Web server uses the UNIX shell environment variable to hold parameters passed from the Web server, and then generates a separate process to run the CGI. The first implementation of CGI is written by Perl, but CGI has the following problems:
- Inefficient: Each connected fork is processed by one process.
- Very limited functionality: CGI receives only one request and outputs a response.
So there's the Apache mod_php,fastcgi (Fast Common Gateway Interface)
FASTCGI uses a process/thread pool to process a series of requests. These processes/threads are managed by the FASTCGI server, not the Web server. When a request comes in, the Web server passes the environment variable and the page request through a long socket connection to the fastcgi process. So fastcgi has the following advantages:
- Performance: Bypass the overhead of CGI opening up new processes through the process/thread pool.
- Compatible: Very easy to retrofit existing CGI standard programs.
- Language-Independent: FastCGI is a set of standards, theoretically speaking, as long as the standard output (STDOUT) language can be used as the FASTCGI standard Web backend.
Web server isolation: The fastcgi back end and Web server run in different processes, and any failure of the backend does not cause the Web server to hang.
Patent: No intellectual property issues for private APIs such as Apache mod_php.
Extensions: fastcgi backend and Web server communicate through sockets, both distributed and easily scaled out.
5.PHPFPM is a concrete implementation of PHP for FASTCGI, it will create a number of CGI sub-processes after the start, and then the main process is responsible for managing the child process, and it provides a socket externally, That Web server when you want to forward a dynamic request only need to follow the format required by the FASTCGI protocol to send data to this socket, that PHPFPM created by the child process to scramble for the socket connection, who grabbed who processed and returned the results to the Web server, What did the PHPFPM main process do? Let's say one of the sub-processes exits unexpectedly, and the PHPFPM will monitor him. Once a CGI subprocess is discovered, another one is started, and many other management functions
6 PHPFPM as a standalone process exists through the socket to establish a connection with Nginx, while mod_php is loaded into the Apache server as a module, and both of them as CGI dispatch managers, they are not the same way to manage it