Home >Backend Development >PHP Problem >A brief discussion on fastcgi and php-fpm in PHP

A brief discussion on fastcgi and php-fpm in PHP

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-15 17:27:202943browse

This article will give you a brief introduction to fastcgi and php-fpm in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on fastcgi and php-fpm in PHP

When talking about PHP development, fastcgi and php-fpm will definitely be mentioned. These two things play a vital role in the performance of PHP. During my internship at Baidu, the ODP environment used was php-fpm, but I had never known anything about it, and even had a vague concept of PHP as a whole. The task was completed ahead of schedule in the past two days. I took this opportunity to learn more about it. Do a little in-depth understanding of PHP.

Web Server

As a server-side language specifically designed for Web development, PHP must be implemented with the help of a Web server. Common PHP architectures include LAMP and LNMP. , find a picture to explain the access of PHP.

A brief discussion on fastcgi and php-fpm in PHP

Apache handles PHP requests

mod_php mode

It can be seen that the request is Sent to Apache, which then parses and processes PHP and returns the results to the client. When we use the LAMP architecture, we need to modify the Apache configuration file httpd.conf

加入如下两句
LoadModule phpx_module PHP_ROOT/phpxapache2_2.dllAddType appliaction/x-httpd-php.php
将下面语句做如下修改<IfModule dir_module> DirectoryIndex index.html</IfModule>改为 <IfModule dir_module> DirectoryIndex index.html index.htm index.php index.phtml</IfModule>

to use LoadModule to load phpx_module (x is the PHP version number). It can be understood that PHP is run as a sub-module of Apache. When a request is made to access PHP, Apache will call phpx_module to parse the PHP code.

How does php_module parse PHP code? - Via sapi.

The relationship between Apache, PHP and sapi is as shown below:

A brief discussion on fastcgi and php-fpm in PHP

sapi is an intermediate layer that provides an interface for external communication, similar to socket, making PHP Can interact with other applications for data. PHP provides a variety of sapi, such as php_module for Apache and Nginx, ISAPI for IIS, etc.

The execution process of Apache calling PHP:

Apache -> httpd -> php_module -> sapi -> PHP

In short, the Web server passes The mode of parsing and executing PHP in php_module mode is mod_php.

mod_fastcgi mode

Another way of PHP's sapi is to provide cgi mode. Now cgi has been replaced by fastcgi.

The so-called cgi is the common gateway interface. When the web server receives the request, it will hand the request to the cgi program (such as PHP's fastcgi), and cgi will process the request (parse PHP), and then send the result Return to the web server, and the web server returns the request to the client.

The shortcoming of cgi is that each request will have a startup and exit process, which is the much-criticized fork-and-execute mode, which is finished in the case of a large number of concurrent requests.

The birth of fastcgi makes up for this shortcoming. Its principle is to start waiting for requests early, and will not exit after responding to the request, and continue to wait for the next request.

The running mode using fastcgi is called mod_fastcgi.

Compare the differences between mod_php and mod_fastcgi. The former runs PHP as a module of the web server, and PHP is highly integrated with the server. The latter uses fastcgi as an intermediate process, and PHP and Web servers only use fastcgi to complete requests and responses.

A brief discussion on fastcgi and php-fpm in PHP

mod_php mode

In mod_php mode, every time Apache accepts a request, a process will be generated to connect to PHP through sapi Requests, in the case of high concurrency, obviously the server will not be able to bear it.

A brief discussion on fastcgi and php-fpm in PHP

mod-fastcgi mode

As can be seen from this picture, whenever a request arrives, httpd will call sapi The request is passed to PHP and does not exit after completion, waiting for the next request. Concurrency performance is greatly improved.

php-fpm

The previous explanation is only to introduce php-fpm. What does php-fpm do? In fact, it is used to assist fastcgi.

fastcgi is a platform-independent and language-independent interface implementation. It only needs to be implemented in different languages ​​​​in accordance with the prescribed methods to complete communication with the Web server. The php-cgi shown in the picture above is the fastcgi manager that comes with PHP.

There are two main problems with php-cgi seen on the Internet:

  • After php-cgi changes php.ini, you need to restart php-cgi to make the new one. php.ini takes effect and cannot be restarted smoothly.

  • If you kill php-cgi directly, PHP will not be able to run

php-fpm perfectly solves the above shortcomings and is a dedicated fastcgi manager.

In short, the role of php-fpm is to specifically manage fastcgi in PHP, so that PHP can safely and stably cooperate with the Web server to complete the request response process under high concurrency conditions.

Recommended learning: php video tutorial

The above is the detailed content of A brief discussion on fastcgi and php-fpm in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:imooc.com. If there is any infringement, please contact admin@php.cn delete