A brief discussion on fastcgi and php-fpm in PHP
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.
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.
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:
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.
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
