Cgi,fastcgi,php-cgi and PHP-FPM (turn)

Source: Internet
Author: User
Tags php source code nginx server server memory

Http://www.cnblogs.com/zl0372/articles/php_4.html

Cgi

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 itself can be regarded as a protocol standard, it can be in any language to write specific protocols conforming to the interface standards exist, 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 can also be referred to as a protocol standard, such as the following to say PHP-FPM is to support the parsing of PHP fastcgi process Manager/engine.

FASTCGI Features
    1. FastCGI is language agnostic.
    2. 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.
    3. 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.
    4. 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
    1. Load fastcgi Process Manager (IIS ISAPI or Apache Module) when Web server starts
    2. The FASTCGI process Manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.
    3. 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.
    4. 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.

FastCGI of the poor

Because it is multi-process, so consumes more server memory than CGI multithreading, php-cgi interpreter consumes 7 to 25 megabytes per process, multiplying this number by 50 or 100 is a large amount of memory.

Nginx 0.8.46+php 5.2.14 (FastCGI) server in 30,000 concurrent connection, open 10 Nginx process consumes 150M memory (15m*10=150m), open 64 php-cgi process consumes 1280M of memory (20m*64= 1280M), combined with the memory consumed by the system itself, consumes less than 2GB of memory. If the server memory is small, you can only open 25 php-cgi processes, so that the total amount of memory consumed by php-cgi is 500M.
The above data is from the Nginx 0.8.x + PHP 5.2.13 (FastCGI) build 10 times times more than Apache Web Server (6th edition)

php-cgi

PHP-CGI is the fastcgi manager that comes with PHP.

PHP-CGI deficiency:

    1. php-cgi after changing the php.ini configuration, you need to restart the php-cgi for the new Php-ini to take effect, not to smooth the restart.
    2. Directly kill the php-cgi process, PHP will not be able to run. (PHP-FPM and spawn-fcgi do not have this problem, the daemon will smooth the new child process from the newly generated.) )
php-fpm

PHP-FPM is a PHP fastcgi manager that is only for PHP and can be downloaded in http://php-fpm.org/download.

PHP-FPM is actually a patch of PHP source code designed to integrate FASTCGI process management into a PHP package. It must be patch into your PHP source code and can be used after compiling and installing PHP.

Now we can download in the latest PHP 5.3.2 source tree to directly integrate the PHP-FPM branch, it is said that the next version will be fused into the main branch of PHP. Relative SPAWN-FCGI,PHP-FPM in the CPU and memory control are better, and the former is easy to crash, must be monitored with crontab, and PHP-FPM does not have this annoyance.

PHP5.3.3 has integrated php-fpm, no longer a third-party package. PHP-FPM provides a better way to manage the PHP process, can effectively control the memory and process, can be smooth overloaded PHP configuration, more than spawn-fcgi have a bit more, so by the official PHP included. The PHP-FPM can be turned on with the –ENABLE-FPM parameter when the./configure.

spawn-fcgi

SPAWN-FCGI is a general-purpose fastcgi Management Server, it is a part of LIGHTTPD, many people use lighttpd spawn-fcgi for fastcgi mode management work, but there are many shortcomings. And the php-fpm of how much to alleviate some of the problems, but PHP-FPM has a drawback is to recompile, which for some already running environment may have a small risk (refer), in PHP 5.3.3 can be directly used php-fpm.

SPAWN-FCGI has now become a single project, more stable, but also to many Web site configuration to facilitate. There are a number of sites to match it with Nginx to solve dynamic Web pages.

The newest lighttpd also does not contain this piece (Http://www.lighttpd.net/search?q=Spawn-FCGI), but it can be found in previous versions. Included in the lighttpd-1.4.15 version (http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz), the current spawn-fcgi is HTTP/// REDMINE.LIGHTTPD.NET/PROJECTS/SPAWN-FCGI, the latest version is http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz.

Note: The latest spawn-fcgi can find the latest release address of the Lighttpd.net website by searching for "spawn-fcgi".

Comparison of PHP-FPM and spawn-cgi

The PHP-FPM is very convenient to use, the configuration is in the Php-fpm.ini file, and the start, restart can be done from the PHP/SBIN/PHP-FPM. More convenient is to modify the php.ini can be directly used php-fpm reload loading, without killing the process can be completed php.ini modified loading
The results show that using PHP-FPM can make PHP a little more performance-boosting. PHP-FPM controlled process CPU recovery is slow, memory allocation is very uniform.
SPAWN-FCGI-controlled processes CPU drops quickly, while memory allocations are not evenly spaced. There are many processes that do not seem to be allocated, while others occupy very high. This may be due to the uneven distribution of process tasks. This also leads to a decline in the overall response rate. The rational distribution of php-fpm leads to the reference of the overall response and the average of the task.

Summary:    F astcgi is a communication interface between Nginx and PHP, which is actually processed by starting the PHP-FPM process to parse the PHP script., i.e. PHP-FPM equivalent to a dynamic application server, thus implementing the Nginx dynamic parsing PHP. Therefore, if the Nginx server needs to support PHP parsing, you need to add PHP configuration in nginx.conf: Forward the PHP script to the FASTCGI process listening IP address and port (specified in php-fpm.conf). At the same time, PHP installation, you need to turn on the support fastcgi option, and compile and install php-fpm patch/extension, at the same time, need start the PHP-FPM process before you can parse the PHP script that the Nginx forwards via fastcgi.
PHP-FPM is a completely independent program that does not rely on php-cgi and does not rely on PHP. Because PHP-FPM is a fastcgi service with a built-in PHP interpreter, it can read the php.ini configuration and php-fpm.conf configuration on its own at startup. Attached: PHP fastcgi Process Manager php-FPM Architecture a master process that supports multiple pool, each pool is monitored by the master process on different ports. There are multiple worker processes in the pool. Each worker process has a built-in PHP interpreter, and the process resides in the background, Support prefork dynamic increase. Each worker process supports compiling scripts at run time and caching the generated opcode in memory to improve performance. Each worker process supports a configuration response that automatically restarts after a specified number of requests. The master process restarts the worker process that is hung up. Each worker process maintains a persistent connection to MySQL/memcached/redis, Implementing a " connection pool ", Avoid duplicate connections and transparent programs. The master process uses the Epoll model to receive and distribute requests asynchronously, listen the listening port, epoll_wait waits for a connection, and distributes it to the worker process in the corresponding pool. The worker process Accpet after the Request poll processing the connection, if the worker process is not sufficient, the master process prefork more processes, and if the prefork reaches the Pm.max_children limit, the worker process is all busy, The master process then suspends the request to the connection queue backlog (the default value is 511).

Web server, such as Nginx, is simply the publisher of the 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, here is distributed static data. Well, if the request is now /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 simply handle the request 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 the Web server receives /index.php this request, it launches the corresponding CGI program, which is the parser for PHP. The PHP parser then parses the php.ini file, initializes the execution environment, then processes the request, and then returns the processed result in a format specified by the CGI, exiting the process. The Web server then returns the results to the browser.

Well, CGI is a protocol, it doesn't matter what the process is. What is that fastcgi? FastCGI is used to improve the performance of CGI programs.

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.


FastCGI is a protocol that PHP-FPM implements this Protocol

The management object of PHP-FPM is php-cgi. But can't say PHP-FPM is the manager of the fastcgi process, because the fastcgi is a protocol,

Previously PHP-FPM was not included in the PHP kernel, to use this feature, you need to find the same version of the source code PHP-FPM to patch the kernel, and then compile.

later, after the PHP kernel integrates php-fpm, it is much easier to use --enalbe-fpm This compilation parameter can be.

Some said that after modifying the php.ini configuration file, there is no way to smooth the restart, so the birth of 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.

fastcgi is like a resident (long-live) type of CGI,is used to improve the performance of CGI programs. CGI is a universal Gateway interface, which is the standard interface between an external application and a Web server, and CGI is designed to ensure that the data passed by the Web server is in standard format and is convenient for the writer of CGI programs. If the request is static data /index.html, then Web server will go to the file system to find this file sent to the browser. If you are requesting dynamic Data /index.php, Nginx needs to find the PHP parser to deal with, then he will be simple processing of this request to the PHP parser. What data will the Nginx send to the PHP parser? URL to have it, query string to have it, post data to have it, the 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.

So 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 the processing time will be longer. 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.

PHP-FPM is an implementation of the FASTCGI program, PHP-FPM Management object is php-cgi. was received by the official PHP. Later, after the PHP kernel integrates php-fpm, it is much easier to use --enalbe-fpmThis compilation parameter can be. The Interpreter for PHP 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.

Cgi,fastcgi,php-cgi and PHP-FPM (turn)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.