PHP-FPM Working principle

Source: Internet
Author: User

One, CGI and fastcgi

When you work with PHP-FPM, you need to understand what CGI is.

CGI is the protocol that enables Web server and the background language to interact, and with this protocol, developers can use any language to process requests from the Web server to dynamically generate content. One of the fatal drawbacks of single CGI is that each request requires a new process of fork, and as the web emerges, high concurrency becomes more and more common, and inefficient ways of doing so are clearly not sufficient. In this way, Fastcgi was born, CGI quickly withdrew from the historical stage, Fastcgi, as the name implies for faster CGI, which allows processing multiple requests within a process, rather than a request processing completed directly end the process, performance has greatly improved.

As for FPM (FastCGI Process Manager), it is the implementation of FastCGI, and any Web Server that implements the FastCGI protocol can communicate with it. FPM is also available in standard FastCGI and offers some enhancements.

FPM is a PHP process manager that contains both master and worker processes: The master process has only one, is responsible for listening to the port, receives requests from the Web Server, and the worker process is typically multiple (the exact number is configured according to actual needs), each Inside the process is embedded a PHP interpreter, is the PHP code to really execute the place, is my on-board FPM Process situation, 11 master processes, 3 worker processes:

650) this.width=650; "src=" Https://pic2.zhimg.com/ff986d4481f8f809713e98bb2b91a685_b.png "class=" origin_image Zh-lightbox-thumb "width=" 886 "alt=" Ff986d4481f8f809713e98bb2b91a685_b.png "/>

The request is received from the FPM, and the process is completed with the following specific procedures:

    1. FPM's master process receives the request

    2. The master process assigns a specific worker process based on the configuration for request processing, and if there are no processes available, returns an error, which is why we have encountered more than 502 errors with Nginx.

    3. The worker process processes the request and returns a 504 error if it times out

    4. Request processing end, return result

The flow of fpm from receiving to processing requests is that, so how does Nginx send the request to FPM? This needs to be explained from the Nginx level.

We know that Nginx is not only a WEB server, but also a powerful proxy server, in addition to the proxy for HTTP requests, can also make a number of other protocol request agent, including this article with the FPM-related fastcgi protocol. To enable Nginx to understand the FASTCGI protocol, Nginx provides a fastcgi module to map the HTTP request to the corresponding fastcgi request.

Nginx fastcgi module provides the fastcgi_param instruction to deal with these mappings mainly, the following Ubuntu under the Nginx configuration file, the main task is to translate the Nginx variables into PHP can understand the variables.

650) this.width=650; "src=" Https://pic3.zhimg.com/64519c5be72799e4eb8853b549f912ee_b.png "class=" origin_image Zh-lightbox-thumb "width=" 892 "alt=" 64519c5be72799e4eb8853b549f912ee_b.png "/> In addition, it is very important that the fastcgi_pass instructions, this refers to To specify the address to which the FPM process listens, Nginx will translate all PHP requests into fastcgi requests before sending them to this address. The following is a simple Nginx configuration file that can work:

650) this.width=650; "src=" Https://pic1.zhimg.com/644b6dae3878fa8caa202531971e768c_b.png "class=" origin_image Zh-lightbox-thumb "width=" 956 "alt=" 644b6dae3878fa8caa202531971e768c_b.png "/> In this configuration file, we created a new virtual host, listening on port 80, we The b root directory is/home/rf/projects/wordpress. We then pass all the requests that end with. PHP to the FastCGI module through the location command, thus handing all PHP requests to FPM processing, thus completing the Nginx-to-fpm closed loop.


PHP-FPM Working principle

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.