php-cgi and php-fpm notes

cgi
Note: in the face of a link request (user request)
1. To create a line cgi child process, then the child process to handle cgi requests, after the end of this process the child process, which is the form-and-execute mode
server 2.cgi way how many connections will be how much cgi child processes, each child will need to start the cgi interpreter, load the configuration, initialization connection to other servers, this is the main reason cgi performance underground. When the user requests number is very large, the system would occupy a lot of resources such as memory, cpu time, resulting in low efficiency


FastCGI
FastCGI like a permanent (long-live) type of CGI, it can always perform with, as long as the activation, will not always have to take the time to fork once (this is the fork-and-execute CGI most criticized mode). It also supports distributed computing, namely FastCGI program can be executed on the host server and site other than to accept requests from the server to the other sites.

FastCGI is a language-independent, scalable, open extension framework CGI, the main behavior is to keep the CGI interpreter process in memory and therefore achieve higher performance. As we all know, CGI interpreter repeated loading is the main reason for poor performance CGI, CGI interpreter if kept in memory and accept scheduling FastCGI process manager, can provide good performance, scalability, Fail- Over characteristics and so on.

FastCGI can also be called a standard protocol, such as the following to say php-fpm is to support a process manager fastCGI parse the php / engine.


Explanation: connection request is received (user request)
execution environment fastcgi 1.web srv through socket (socket or domain IPsocket), speaking to a data transfer program process fastcgi
after 2.fastcgi program process receives a request, for the corresponding logical processing
3. Finally, the process results socket (socket or domain IPsocket), returns to the SRV Web
4.web SRV response packet constructed http response, returned to the browser

 

FastCGI works of
load when you start 1. Web Server FastCGI Process Manager (IIS ISAPI Module or the Apache)
2. FastCGI process manager initializes itself, to start multiple CGI interpreter process (shows multiple php-cgi) and waits Web connection Server.
3. 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.
4. FastCGI child process after the completion of treatment standard output and error messages from the same connection returns Web Server. When the child FastCGI process closes the connection request will come to complete the process. FastCGI child process and then wait for treatment from under FastCGI Process Manager (running to Web Server) is a connection. In CGI mode, php-cgi this will quit.

In these cases, you can imagine how CGI is usually slow. Each Web request must be re-parse PHP php.ini, reload all extensions and try to initialize the entire data structure. Use FastCGI, all of which occur only once when the process started. An additional benefit is that persistent database connections (Persistent database connection) can work.

 


FastCGI is insufficient
because it is a multi-process, multi-threaded so than CGI consumes more server memory, PHP-CGI interpreter process consumes every 7-25 megabytes of memory, this figure is multiplied by 50 or 100 is a large amount of memory.

Nginx 0.8.46 + PHP 5.2.14 (FastCGI) server at 30,000 concurrent connections, open 10 Nginx process consumes memory 150M (15M * 10 = 150M), open 64 php-cgi process consumes memory 1280M (20M * 64 = 1280M), coupled with the system's own memory consumption, total consumption of less than 2GB of memory. If the server memory is small, can only open 25 php-cgi process, so that the total amount of memory consumed by the php-cgi only 500M.
The above data is extracted from Nginx 0.8.x + PHP 5.2.13 (FastCGI) to build ten times better than the Apache Web server

 

PHP-CGI

PHP-CGI is bundled with PHP FastCGI manager.

PHP-CGI shortcomings:

After 1. php-cgi php.ini configuration changes need to restart php-cgi to make new php-ini into force, not a smooth restart.
2. directly kill php-cgi process, php can not run. (PHP-FPM and Spawn-FCGI do not have this problem, the daemon will smooth the newly generated new child process.)

 


PHP-FPM

PHP-FPM is a PHP FastCGI manager, is used only for PHP, you can get in http://php-fpm.org/download download.

PHP-FPM is actually a patch PHP source code, intended to be integrated into PHP FastCGI Process Manager package. It is necessary to patch into your PHP source code can be used only after compile and install PHP.

We can now be downloaded directly integrated branch PHP-FPM in the latest PHP source tree 5.3.2, the next version is said to be the main PHP integration into the branches to go. Relative Spawn-FCGI, PHP-FPM control in terms of memory and CPU are better, but the former is easy to collapse, it must be monitored with crontab, and PHP-FPM is no such trouble.

PHP5.3.3 been integrated php-fpm, and no longer a third-party package. PHP-FPM PHP provides a better process management, can effectively control the memory and processes can smoothly overloading PHP configuration, with a little more than a spawn-fcgi, it is a collection of PHP official. When ./configure -enable-fpm with parameters to open PHP-FPM.

Guess you like

Origin www.cnblogs.com/beekimlin/p/12484934.html
Recommended