Table of Contents
Apache handles PHP requests" >Apache handles PHP requests
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

Jun 15, 2021 pm 05:27 PM
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.

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>
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles