Anatomy of CGI, fastcgi, php-cgi, PHP-FPM

Source: Internet
Author: User
This article introduces the content is about CGI, fastcgi, php-cgi, PHP-FPM analysis, has a certain reference value, now share to everyone, the need for friends can refer to

Defined


First of all, what does CGI do? CGI is designed to ensure that the data passed by the Web server is in a standard format and facilitates the writer of CGI programs.

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.


cgi

    • Common Gateway Interface, an interface standard between an external program and a Web server, is the process of passing information between a CGI program and a Web server

    • Each request generates a CGI process, the CGI program executes, and the process exits

    • Server independent, standalone programming language

FastCgi

    • FastCgi is like a resident CGI, which can be executed all the time, so long as it is activated, it does not need to fork once, and also supports distributed operations, that is, the FASTCGI program can execute on hosts other than the Web server and accept requests from other Web servers.

    • Multiple requests can be processed at the same time

    • Long-term memory consumption

php-cgi

    • PHP's official fastcgi process Manager

    • After the php.ini is modified, you must kill php-cgi and then start php.ini to take effect. Can not be smoothly restarted

    • Memory cannot be allocated dynamically

php-fpm

    • Unofficial FASTCGI Process Manager, later php5.4 started, was officially included, compile PHP only need –enable-fpm can open php-fpm

    • Can smooth restart PHP

    • Dynamic scheduling process




Nginx is only responsible for reverse proxy/Request forwarding, is not responsible for managing the php-cgi process, so nginx generally with the ability to manage the work process (sub-process) php-fpm use.
It should be noted that PHP-FPM is a separate SAPI, it is not php-cgi management, that is, PHP-FPM is not related to php-cgi, PHP-FPM built-in PHP interpreter, PHP-FPM sub-process is their own fork out, Does not call the php-cgi, you put the system php-cgi deleted will not affect the normal operation of the PHP-FPM service.

PHP-FPM in PM = static configuration under the working process of the resident background, that is, if you configure 5 worker Processes Pm.max_children = 5, the PHP-FPM service will automatically fork out 5 sub-processes and reside in the background, will not exit after the request processing ends, You will not quit when you are idle. If you use a database persistent connection in a PHP script, these 5 worker processes also establish and maintain 5 persistent connections to the database, enabling you to reuse the database connection resources while processing multiple requests, avoiding each request being established/ Releases a database connection. Persistent connections can also be timed out for automatic reconnection, which is completely transparent to scripts in php-fpm, and scripts only need to indicate the use of persistent connections at startup.

PHP-FPM in PM = dynamic configuration of the working process "part" of the resident background, that is, maintain a certain number of resident processes, the service is busy to fork out more processes, service idle when the automatic shutdown of some processes, Return the memory resources to the operating system. The virtual hosting provider should be more like this way.

All in all, php-fpm this mode of operation is similar to Apache Prefork MPM, which can be statically active multi-process network services.




PHP-CGI is the early PHP officially produced fastcgi manager, does not support smooth restart, changed the php.ini will kill the original php-cgi and restart to take effect; dynamic worker scheduling is not supported, only a few workers can be specified at the beginning.

PHP-FPM is a fastcgi process manager that was added from 5.3.3, adding dynamic scheduling to dynamically increase or decrease the number of worker processes according to the pressure change of the request. Support for reload instructions to have the worker process restart after completing the current request and apply php.ini new configuration.





PHP54 is a relationship before, php54 after another.


PHP54, PHP-FPM (third-party compilation) is the manager, PHP-CGI is the interpreter

After PHP54, PHP-FPM (official comes with), Master and pool mode. PHP-FPM and php-cgi have no relationship. PHP-FPM is the interpreter and the manager again.


Reference website https://www.zhihu.com/question/55835080

https://segmentfault.com/q/1010000000256516

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.