Difference between CGI, FastCGI, and php-fpm, cgifastcgiphp-fpm

Source: Internet
Author: User

Difference between CGI, FastCGI, and php-fpm, cgifastcgiphp-fpm
Refer to the online description to organize the online description:

First, what does CGI do? CGI is used to ensure that the data transmitted by the web server is in the standard format, so that the compilation of CGI programs is convenient.
Web server (such as nginx) is only the content distributor. For example, if you request/index.html, the web server will find the file in the file system and send it to the browser. static data is distributed here. Okay, if the current request is/index. php: According to the configuration file, nginx knows that this is not a static file and needs to be handled by the PHP parser. Then nginx will simply process this request and hand it over to the PHP parser. What data will Nginx transmit to the PHP parser? There must be a url, a query string, or a POST data, and a few HTTP headers. Okay, CGI is the protocol that specifies the data to be transmitted and the format in which the request is sent to the backend. Think about where the users you use in PHP code come from.
When the web server receives the/index. php request, it starts the corresponding CGI program, which is the PHP parser. Next, the PHP parser parses the php. ini file, initializes the execution environment, processes the request, returns the processed result in the format specified by CGI, and exits the process. The web server then returns the result to the browser.
Okay, CGI is a protocol. It has nothing to do with the process. What is fastcgi? Fastcgi is used to improve CGI program performance.
To improve the performance, what is the problem with the CGI program performance? "The PHP parser parses the php. ini file and initializes the execution environment. The Standard CGI performs these steps for each request! I am tired of starting the process !), Therefore, the processing time is long. This is obviously unreasonable! So how does Fastcgi do it? First, Fastcgi will first start a master, parse the configuration file, initialize the execution environment, and then start multiple workers. When the request comes, the master will pass it to a worker and then immediately accept the next request. In this way, repetitive work is avoided, and the efficiency is naturally high. In addition, when the worker is not enough, the master can start several workers in advance according to the configuration. Of course, when there are too many idle workers, some will also be stopped, which improves performance and saves resources. This is how fastcgi manages processes.
What is PHP-FPM? It is a program that implements Fastcgi and is officially accepted by PHP.
As we all know, the interpreter for PHP is php-cgi. Php-cgi is just a CGI program. It can only parse requests, return results, and no process management. (Huang Shang, Chen really cannot do it !) So there are some programs that can schedule the php-cgi process, such as spawn-fcgi separated by lighthttpd. Good PHP-FPM is such a Dongdong, after a long period of development, gradually get everyone's approval (you know, a few years ago, but complained that PHP-FPM stability is too bad), is becoming more and more popular.
Because fastcgi is a protocol, it seems that no such process exists, and it cannot be managed even if php-fpm exists (at least for the time being ). Some say that php-fpm is a patch of the php kernel.
Previously, it was correct. Because php-fpm was not included in the PHP kernel at the beginning, to use this function, you need to find the same php-fpm version as the source code to patch the kernel and then compile it. Later PHP kernel integrated with the PHP-FPM is much more convenient, the use of -- enalbe-fpm this compilation parameter can be.
Some say that after modifying the php. ini configuration file, you cannot restart it smoothly, so php-fpm is born.
Yes. After modifying php. ini, the php-cgi process cannot be restarted smoothly. The Processing Mechanism of php-fpm is that the new worker uses a new configuration, and the existing worker can stop working after it completes processing. This mechanism is used to smooth the process.
Fastcgi is an upgraded CGI version. It is a language-independent protocol used to communicate with programs (such as PHP, Python, Java) and Web servers (Apache2, Nginx ), theoretically, programs written in any language can provide Web services through Fastcgi.
Fastcgi is characterized by the completion of multiple requests in one process in sequence to improve efficiency. Most Fastcgi implementations maintain a process pool.
PHP-fpm is an implementation of Fastcgi for PHP. It manages a process pool to process requests from Web servers. Currently, PHP-fpm is built in PHP.
However, PHP-fpm is only a "PHP Fastcgi Process Manager", and the handler still calls the PHP interpreter to handle the request. The PHP interpreter (in windows, click php-cgi.exe.

Related Article

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.