Relationship between FastCGI and PHP-FPM

Source: Internet
Author: User
Tags php source code

1 CGI

(1) What is CGI:

CGI (Common Gateway Interface) Public Gateway Interface, is one of the most important technology of WWW technology, has irreplaceable important position, CGI is the interface standard between external application (CGI program) and Web server, Is the discipline of passing messages between CGI programs and Web servers. The CGI specification allows the Web server to execute external programs, send their output to a Web browser, and CGI to turn a simple set of hypermedia documents from the Web into a complete new interactive media.

(2) Purpose of CGI:

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.

(3) Process of CGI:

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.

If the request is/index.php, according to the configuration file, Nginx know that this is not a static file, need to find a PHP parser to handle, then it will be simple processing of the request to the PHP parser. Nginx will pass those data to (such as URL, query string, post data, HTTP header) PHP parser it? CGI is the protocol that specifies what data to pass, in what format, to the rear to handle the request.

When the Web server receives the/index.php 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. Web server then returns the results to the browser

(4) Description of CGI functions:

The vast majority of CGI programs are used to interpret the input information from the form, to generate the appropriate processing on the server, or to feed the corresponding feedback to the browser. CGI programs are web pages with interactive functions

(5) Summary:

As mentioned above, CGI is a protocol that has nothing to do with the process or anything.

2 Fastcgi

(1) The role of fastcgi:

FastCGI is used to improve the performance of CGI programs.

(2) Performance issues with CGI programs:

"The PHP parser parses the php.ini file, initializes the execution environment," and here it is. The standard CGI performs this step for each request (not too tired!). Start the process very tired to say! ), so the time to process each request will be longer, which is obviously unreasonable!

(3) What fastcgi is doing: managing the process

First fastcgi starts 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.

3 PHP-FPM

The parser for PHP is php-cgi. PHP-CGI is just a CGI program, his own can only parse the request, return results, not process management, so there are some can dispatch php-cgi process, and PHP-FPM is able to dispatch php-cgi process of a program. PHP-FPM after a long period of development, gradually received the recognition of everyone

PHP-FPM is actually a patch of PHP source code designed to integrate FASTCGI process management into a PHP package. It must be patch in your PHP source code before it can be used after compiling and installing PHP. PHP5.3.3 has inherited php-fpm and is no longer a third-party package. PHP-FPM provides a better way to manage PHP processes, effectively control memory and processes, and can smoothly reload PHP configurations

4 Summary

(1) CGI is a public gateway interface that works in a process manner. That is, when a request is made, a CGI process is created, closing the CGI process and exiting the memory at the end of the application

(2) FastCGI is the development of CGI, at the end of the application, the FASTCGI process does not exit memory, but waits for the next request. PHP's CGI way is fastcgi way, in the PHP4 is like this

(3) PHP-FPM is the fastcgi manager. Prior to this, the fastcgi process is managed by the operating system, and once a fastcgi process fails, It is possible for the operating system to run normally. PHP-FPM's role is to create a fence between the operating system and the fastcgi, thus blocking the transmission of the fastcgi fault to the operating system.

Relationship between FastCGI and PHP-FPM

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.