Simple to understand the concepts and principles of distinction and run under CGI, FastCGI and php-fpm of

What is CGI?

CGI (Common Gateway Interface), common gateway interface, which is the transfer of information between the interface standard Web server and an external application (CGI program). Through the CGI interface, Web server can access information submitted by the client, and forwarded to the server CGI program, and finally returns the results to the client.

In other words, CGI is actually an interface standard. We usually refer to CGI refers to the CGI program that implements CGI interface standard procedure.

As long as a standard language having input, output and environmental variables, such as perl, php, C, etc., can be used to write CGI programs.

CGI program works:

Web servers generally deal only with static file requests (such as jpg, htm, html), if you encounter a dynamic script requests (such as php), web server master process, you fork a new process to start the CGI program, that will dynamic script requests to the CGI program to process. CGI program needs to start a process, for example, reads the configuration file, load expansion. After the CGI program starts, it will resolve dynamic script, then returns the results to the Web server, Web server and then finally returns the results to the client, just fork process will also shut down.

In this way, each time a user requests a dynamic scripting, Web servers must be re-fork a new process to start the CGI program, the CGI program to handle dynamic scripting, dealt with the process turns off.

The efficiency of this way of working is very low.

PHP interpreter if embedded inside a Web server process execution

Later, there was a more efficient way: Web server built-in module. For example, apache mod_php the module. The php interpreter module is made, and then loaded into the server apache.

In this way, apache server at startup, it will start at the same time php module. When a client requests a file php, apache server will not have to fork out a new process to start php interpreter, but directly to the php file to run in the php module for processing. Obviously, in this way, it would be more efficient.

Because when apache server starts, will read php configuration file, load the php module, in the process of running the apache. Php will not re-read the configuration file. So, every time we modify the php configuration file, you must restart apache, php new configuration file to take effect.

mod_php PHP interpreter embedded into the Apache process, used only with Apache, while fast-cgi cgi and appear as a separate process, as long as the corresponding Web server implementation or fast-cgi cgi protocol, the request can handle PHP . mod_php this way embedded biggest drawbacks is the large memory footprint, whether to use the PHP interpreter will load it into memory, typical is the deal CSS, JS static files such that there is no need to load interpreter.

What is FastCGI?

FastCGI is like a permanent (long-live) type of CGI program that has been running. FastCGI program can also be deployed in the Web server on a different host, it can accept requests from other Web servers.

FastCGI is language-independent. The main behavior is to keep the CGI interpreter process in memory and thus obtain efficient performance. As we all know, CGI interpreter repeated loading is the main reason for poor performance CGI.

FastCGI is a process management tool that can manage the CGI process in memory.

FastCGI Process Manager needs to be started separately. After starting the FastCGI, it generates a master process and a plurality of sub-FastCGI process (child process is actually CGI interpreter process).

When a client requests a dynamic script on the server Web, Web server will dynamically via TCP protocol FastCGI script to the main process, the main FastCGI process according to the situation, schedule a free child process to resolve the dynamic script processing is complete, the results will return to the Web server, Web server, and then returns the results to the client. After the client request is processed, FastCGI child process and does not turn off, but continue to wait for the main course schedule tasks.

FastCGI important features:

1, FastCGI is between the HTTP server and dynamic scripting language interface or communication tools.

2, FastCGI advantage that the dynamic language parsing the HTTP server and separated.

3, Nginx, Apache, Lighttpd and most dynamic languages ​​support FastCGI.

4, FastCGI interface mode using the C / S architecture, is divided into client (HTTP server) and server (Dynamic Language resolution server).

5, PHP dynamic language you can start the server daemon of multiple FastCGI.

6, HTTP client and server via FastCGI dynamic languages ​​FastCGI server communications.

Schematics and operating procedures:

 

First, load FastCGI Process Manager (Apache Module or IIS ISAPI, etc.) when the Web Server start

Two, FastCGI process manager initializes itself, to start multiple CGI interpreter process (can be built multiple php-cgi), and waits for a connection from the Web Server.

Third, when a client request arrives Web Server, FastCGI Process Manager to select and connect to a CGI interpreter. Web server sends to the child FastCGI php-cgi CGI environment variables and the standard input.

Fourth, after the completion of the child FastCGI process, from the same connection to standard output and error messages returned Web Server. When the child FastCGI process closes the connection request will come to complete the process. FastCGI process then waits for the child, and the processing from under FastCGI Process Manager (running to Web Server) is a connection. In CGI mode, php-cgi this will exit the

What is php-fpm?

fpm FastCGI Process Manager is an abbreviation, then, is fpm FastCGI Process Manager for short.

php-fpm is FastCGI process manager in php.

For the previous version php5.3 it, php-fpm is a third-party patch designed to integrate into PHP FastCGI Process Manager package.

In later versions php5.3 in, php-fpm is no longer a third-party packages, it has been integrated into the php source code of. php-fpm of PHP provides a better process management, can effectively control the memory and processes can smoothly overloading PHP configuration, has more advantages than the spawn-fcgi, so php-fpm is the official PHP acquired.

php-fpm of managed objects is php-cgi. But I can not say php-fpm is fastcgi process manager, as said earlier, is a fastcgi protocol, such a process does not seem to exist, even if there is php-fpm can not manage his (at least for now). Some said, php-fpm is a kernel patch php used to be right. Since the very beginning of php-fpm not included in the PHP core inside, to use this feature, you need to find the same source version of php-fpm patch the kernel, then compile. Later PHP core with integrated PHP-FPM is more convenient to use --enalbe-fpm This compiler parameters.

Guess you like

Origin www.cnblogs.com/FLy-1992/p/11645762.html