Table of Contents
Detailed explanation of FastCGI and mod_php in PHP
Home Backend Development PHP Tutorial Detailed explanation of FastCGI and mod_php in PHP_PHP tutorial

Detailed explanation of FastCGI and mod_php in PHP_PHP tutorial

Jul 12, 2016 am 09:07 AM
fastcgi php

Detailed explanation of FastCGI and mod_php in PHP

The knowledge about FastCGI and mod_php on the Internet is relatively messy and not comprehensive, so I have compiled it here for the convenience of beginners.

Background

The most common way for PHP is to run in Apache as a module (mod_php), which is also the default way for Apache to run PHP; but in Nginx, Nginx uses PHP-FPM, but what exactly is PHP-FPM? Dongdong? What does it have to do with php? Let’s explore it together today.

PHP handlers

The first thing to remember is that any kind of web server (Apache, Nginx, etc.) is designed to send static resources such as HTML and pictures to users. The web server itself cannot interpret any dynamic scripts (PHP, Python, etc.) wait).

The PHP processor is used to interpret the PHP code in the web application, interpret it as HTML or other static resources, and then pass the parsed results to the web server, and finally the web server sends it to the user.

Most web servers cannot parse PHP code, so it needs a program that can parse PHP code, which is a PHP processor.

Now we know that both Apache and Nginx require PHP processor to process PHP code, so how to connect the server and PHP processor? In other words, how does the server communicate with the php processor?

The answer is through SAPIServer Application Programming Interface (Server-side Application Programming Interface). Simply put, SAPI refers to the programming interface for specific PHP applications. Just like a PC, no matter which operating system is installed, as long as it meets the interface specifications of the PC All can run normally on a PC. There are many ways to execute PHP scripts, through a web server, or directly under the command line, or embedded in other programs. If you are interested, you can study the PHP kernel.

Here we continue to discuss the two connection methods provided by PHP’s most commonly used SAPI: mod_php and mod_fastcgi.

mod_php mode

Let’s review, how can Apache identify PHP code? Is it necessary to add or modify the following sentences in Apache’s configuration file httpd.conf:

<ol class="linenums"><li class="L0"><p><code><span class="com">//添加</span></code></p></li><li class="L1"><p><code><span class="typ">LoadModule</span><span class="pln"> php5_module modules</span><span class="pun">/</span><span class="pln">libphp5</span><span class="pun">.</span><span class="pln">so</span></code></p></li><li class="L2"><p><code><span class="typ">AddType</span><span class="pln"> application</span><span class="pun">/</span><span class="pln">x</span><span class="pun">-</span><span class="pln">httpd</span><span class="pun">-</span><span class="pln">php </span><span class="pun">.</span><span class="pln">php</span></code></p></li><li class="L3"><p><code><span class="com">//修改</span></code></p></li><li class="L4"><p><code><span class="pun"><</span><span class="typ">IfModule</span><span class="pln"> dir_module</span><span class="pun">></span></code></p></li><li class="L5"><p><code><span class="typ">DirectoryIndex</span><span class="pln"> index</span><span class="pun">.</span><span class="pln">php index</span><span class="pun">.</span><span class="pln">html index</span><span class="pun">.</span><span class="pln">htm index</span><span class="pun">.</span><span class="pln">html</span></code></p></li><li class="L6"><p><code><span class="pun"></</span><span class="typ">IfModule</span><span class="pun">></span></code></p></li></ol>
Copy after login

That is, php runs as a sub-module of Apache. When a php file is accessed through the web, Apache will call php5_module to parse the php code.

After configuring to load the mod_php module, php is part of the Apahce process itself, and each new Apache child process will load this module.

mod_fastcgi mode

Let’s first look at the instructions on the PHP-FPM official website:

PHP-FPM - A simple and robust FastCGI Process Manager for PHP
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

PHP-FPM is a FastCGI process manager for PHP, which is explained very simply. This shows that PHP-FPM assists mod_fastcgi mode to work, but what is FastCGI? What processes are managed?

What is CGI?

CGI (Common Gateway Interface) is one of the most important technologies in WWW technology and has an irreplaceable important position.

CGI is an interface standard between external applications (CGI programs) and Web servers. It is a procedure for transferring information between CGI programs and Web servers.

The CGI specification allows Web servers to execute external programs and send their output to Web browsers. CGI turns the Web's set of simple static hypermedia documents into a complete new interactive media.

To put it bluntly, CGI is a protocol between an external application (CGI program) and the web server. CGI is to ensure that the data passed by the server is in a standard format.

What is FastCGI?

FastCGI is like a long-live CGI. It can be executed all the time. As long as it is activated, it will not take time to fork every time (this is the most criticized fork-and of CGI). -execute mode). It also supports distributed computing, that is, FastCGI programs can be executed on hosts other than the website server and accept requests from other website servers.

FastCGI is a language-independent, scalable architecture CGI open extension. Its main behavior is to keep the CGI interpreter process in memory and thus obtain higher performance. As we all know, repeated loading of the CGI interpreter is the main reason for low CGI performance. If the CGI interpreter remains in memory and accepts FastCGI process manager scheduling, it can provide good performance, scalability, Fail-Over features, etc.

Generally, the entire workflow of FastCGI is like this:

In other words, FastCGI is an upgraded version of CGI, a language-independent protocol used to communicate between programs (such as PHP, Python, Java) and web servers (Apache2, Nginx). In theory, programs written in any language can be Provide Web services through FastCGI.

The characteristic of FastCGI is that it will complete multiple requests in one process in order to improve efficiency. Most FastCGI implementations will maintain a process pool.

Popular explanation: FastCGI needs to be started in advance, and multiple CGI modules can be started. It runs there and waits for the web to send requests, and then parses and operates PHP. After completion, it generates html and returns it to the web, but after completion it It will not exit, but will continue to wait for the next web request.

PHP-FPM

PHP-FPM is an implementation of FastCGI for PHP. It is responsible for managing a process pool to handle requests from the web server.

But PHP-FPM is just a "PHP FastCGI Process Manager", it will still call the PHP interpreter itself to process the request. The PHP interpreter (under Windows) is php-cgi.exe.

Conclusion

Having said so much, I don’t know if I have expressed it clearly. Please correct me if there is anything incorrect. Thank you.

Original text: http://article.gitos.cn/2015/Aurthur/PHP-Mod-PHP-And-Fast-CGI-Explain.html Author: Aurthur



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1059862.htmlTechArticlePHP’s FastCGI and mod_php Detailed explanation The knowledge about FastCGI and mod_php on the Internet is relatively messy and not comprehensive, so I will sort it out here. , so that beginners can check it easily. Background PHP is most commonly used...
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
3 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
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
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: 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 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.

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.

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: 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.

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: 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.

See all articles