PHP Learning Series-CLI, CGI, FastCGI, php-fpm learning ____php

Source: Internet
Author: User
Tags fpm php language php cli php script sapi stdin time limit

In the last article we learned that the process of decoupling between Web server and PHP is through SAPI. What is CLI, CGI, fastcgi, php-fpm? Don't worry, in this article, we will have a clear understanding of it.

First of all, CLI, CGI, fastcgi is a form of implementation of SAPI, is not a bit vague. No problem, let's have a simple understanding of the concept. CLI Mode

Cli:command line Interface (command-line interface), through the command-line, we do not need to go through the Web server to execute PHP script;

In CLI mode, some of the configurations in PHP.ini do not work for them. such as not subject to PHP script execution time constraints (WEB server usually has a response time limit);

How to execute PHP scripts and pass parameters under different platforms under the specific CLI mode, refer to PHP CLI mode;

Currently, the implementation of PHP script in the CLI mode is less, the reason is not to meet the complex business needs, nor can pass post parameters, upload files, poor UI interaction, more suitable for developers to use; CGI

Cgi:common Gateway Interface (Public Gateway Interface) is a language-independent protocol.

Before we go any further, let's consider this scenario: Web server can fetch the get post parameters that the browser passes over, and the Web application can accept stdin (standard input), so that the parameters that the Web server receives are passed directly to the web The application is OK.

But it's actually that simple. You think about it, two people in business, one in the language (Web Server), the other in the language (Web application), two people how to communicate. It is unlikely that either party will indulge the other. Fortunately, both sides have a good grasp of English, then, then set a standard language: English, the two sides will communicate through English. English here is similar to the cgi,php-cgi is a way to achieve this protocol.

Once a dynamic request arrives in the Web server, the Web server forms stdin based on the CGI Protocol Encapsulation request information and environment variables, passing stdin to php-cgi through a TCP link or socket.

OK, so far, we've known that the request was forwarded to the php-cgi after the Web server forward. So, what did the php-cgi do? What is the relationship between the Zend engine, lexical, and grammatical analysis that we have studied in the previous chapters?


The contents may not be accurate enough, but the general process is fine.

However, the CGI pattern is also known as Fork-execute-kill mode: Whenever a request comes in, the WEB server initiates a php-cgi to process the request, and the php-cgi is automatically destroyed when the request is processed. For QPS, the CGI model is fine, but for hundreds of QPS, the flat sound will be long. Why do you say that? Because the Web server creates a php-cgi for it to allocate memory and other resources, the QPS will cause the memory and other resources of the tension, resulting in the entire flat sound of the super long.

One thing to note: In CGI mode, the php-cgi boot is controlled by the Web server.

For many of the current high concurrent Web sites, the CGI model is very conspicuous can not meet their needs, there is no way to solve it.

Recall the above php-cgi production, destruction process: request to reach the Web server→ generation php-cgi process → request forwarding php-cgi→php-cgi processing request → return to the Web Server processing results →php-cgi destroy. Is there a way to implement php-cgi (which may not be accurate) and resident memory? The answer is yes, and that is the FASTCGI model that we are going to talk about here. fastcgi

FastCGI is also a protocol, the PHP language is implemented in the way of php-cgi. FastCGI is an upgraded version of CGI, and since it's an upgraded version, what does it do to improve the functionality of CGI?

The lifting point is php-cgi and resident memory. For these two points you may not understand, explain.

Pre-build: A certain number of php-cgi are generated before the request reaches php-cgi.

Resident Memory: PHP-CGI is not destroyed after a request has been processed, it will always exist, waiting for the PHP-FPM to allocate the next request.

OK, above also said, pre-generated php-cgi will generate a certain amount. These php-cgi can be processed when a request is forwarded to a Web server, and if each php-cgi tries to process it, it can cause a "surprise swarm effect." So which one can deal with it. Obviously, we need to do a process scheduling for these php-cgi, PHP-FPM appears. PHP-FPM

As I said before, PHP-FPM is the PHP-CGI process manager.

One thing to note here is that when the CGI protocol is php-cgi, the startup of the fastcgi is controlled by the Web server; the php-cgi startup has nothing to do with the Web server, it is only scheduled by PHP-FPM. Moreover, this time the Web server forwarding requests and passing parameters to a certain php-cgi need to go through the PHP-FPM scheduling, and then by PHP-FPM control this request specifically to the php-cgi processing.

PHP-FPM can be run independently on a non-Web server, to achieve the so-called static and dynamic separation.

Steal a picture of someone else, the following is the complete process of the next client request response for the FASGCGI interface protocol.



Note:

Above we talked about the CGI existence of the criticism, and proposed a solution: pre-build, resident memory. In addition to the FASTCGI agreement, there is actually another way. Compile php as an extension of Apache and load and activate Php_module when Apache starts. At this time, php-cgi resident within the httpd process. When a dynamic request arrives, the httpd no longer generates php-cgi, but forwards the dynamic request directly to its internal php-cgi. However, there is also a problem: high QPS performance can degrade, because this method generates only one php-cgi.

CGI and fastcgi are just protocols. Various support and web interaction programming languages have made their own implementations of the CGI/FASTCGI protocol (of course, any language can write CGI scripts), and PHP php-cgi and PHP-FPM is the implementation of the FASTCGI protocol.

Reference: Command line mode for PHP

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.