Diagram of the relationship between CGI, FastCGI and PHP-FPM

不言
Release: 2023-03-23 22:52:02
Original
15508 people have browsed it

The main content of this article is about the diagram of the relationship between CGI, FastCGI and PHP-FPM. It has a certain reference value. Now I share it with you. Friends in need can refer to it


Directory

  • Basic

  • Module method

  • CGI

  • Introduction to FastCGI

    • A brief introduction to FastCGI

    • ##How FastCGI works

  • PHP-FPM Introduction

  • Summary

  • References

When building a LAMP/LNMP server, you will often encounter the concepts of PHP-FPM, FastCGI and CGI. If you only know a little about them, it will be difficult to build a high-performance server. Next we will graphically explain the relationship between these concepts.

Basics

In the entire website architecture, Web Server (such as Apache) is only the distributor of content. For example, if the client requests index.html, then the Web Server will find this file in the file system and send it to the browser. What is distributed here is static data.

If the request is index.php, according to the configuration file, the Web Server knows that this is not a static file and needs to find a PHP parser to process it, then it will process the request Simply process it and then hand it over to the PHP parser.

When the Web Server receives the index.php request, it will start the corresponding CGI program, which is the PHP parser. Next, the PHP parser will parse the php.ini file, initialize the execution environment, then process the request, return the processed result in the format specified by CGI, exit the process, and the Web server will return the result to the browser. This is a complete dynamic PHP Web access process. It will be easier to understand if we introduce these concepts next.

  • CGI: It is a protocol for data exchange between Web Server and Web Application.

  • FastCGI: Same as CGI, it is a communication protocol, but it has some optimizations in efficiency than CGI. Likewise, the SCGI protocol is similar to FastCGI.

  • PHP-CGI: is the interface program of PHP (Web Application) to the CGI protocol provided by Web Server.

  • PHP-FPM: is the interface program for the FastCGI protocol provided by PHP (Web Application) to the Web Server. In addition, it also provides relative Smart some task management.

In WEB,

  • Web Server generally refers to Apache, Nginx, IIS, Lighttpd, Tomcat and other servers,

  • Web Application generally refers to PHP, Java, Asp.net and other applications.

Module method

Before understanding CGI, let’s first understand another method of Web server transmitting data: PHP Module loading method. Take Apache as an example. In PHP Module mode, should you add the following sentences to Apache's configuration file httpd.conf:

# 加入以下2句LoadModule php5_module D:/php/php5apache2_2.dllAddType application/x-httpd-php .php# 修改如下内容<IfModule dir_module>
    DirectoryIndex index.php index.html</IfModule>
Copy after login

The above is the manual configuration after installing php and apache environment under Windows. In Linux The source code installation is roughly configured like this:

# ./configure --with-mysql=/usr/local --with-apache=/usr/local/apache --enable-track-vars
Copy after login

So, in this way, their common essence is to use LoadModule to load php5_module, which is to use php as a sub-module of apache run. When accessing php files through the web, apache will call php5_module to parse the php code.

So how does php5_module pass data to the php parser to parse the php code? The answer is through sapi.

Let’s look at another picture and talk about the relationship between apache, php and sapi in detail:

Diagram of the relationship between CGI, FastCGI and PHP-FPM

From the above picture, we can see that sapi It is such an intermediate process. SAPI provides an interface for external communication, which is somewhat similar to a socket, allowing PHP to interact with other applications (apache, nginx, etc.). PHP provides many kinds of SAPI by default, the common ones are php5_module, CGI, FastCGI for apache and nginx, ISAPI for IIS, and Shell CLI.

So, the above apache call php execution process is as follows:

apache -> httpd -> php5_module -> sapi -> php
Copy after login

Okay. Let’s figure out apache and php through php5_module!

This mode installs the PHP module into Apache, so every time Apache ends a request, a process will be generated, and this process will completely include PHP's various calculations and other operations.

In the picture above, we can clearly see that every time apache receives a request, a process will be generated to connect to php to complete the request through sapi. It is conceivable that if there are too many users, concurrent If the number is too high, the server will be unable to bear it.

Moreover, when mod_php is compiled into apache, it is difficult to determine whether it is a problem with php or apache when a problem occurs.

CGI

CGI (Common Gateway Interface) full name is "Common Gateway Interface", WEB server and PHP application A tool for "talking" whose program must run on a network server. CGI can be written in any language as long as the language has standard input, output and environment variables. Such as php, perl, tcl, etc.

What data will the WEB server pass to the PHP parser? URL, query string, POST data, HTTP header will all be there. Therefore, CGI is a protocol that stipulates what data is to be transmitted and in what format it is passed to the backend for processing the request. Think carefully about where the users you use in your PHP code come from.

In other words, CGI is specially used to deal with web servers. When the web server receives a user request, it will submit the request to the cgi program (such as php-cgi). The cgi program will process (parse php) according to the parameters submitted in the request, and then output a standard html statement and return it to the web server. The WEB server returns it to the client. This is how ordinary cgi works.

The advantage of CGI is that it is completely independent of any server and only acts as an intermediary. Provides interfaces to apache and php. They complete data transfer through CGI wiring. The advantage of this is to minimize the association between the two and make them more independent.

But there is a troublesome thing about CGI, that is, every web request will have a startup and exit process, which is the most criticized fork-and-execute mode. When the scale of concurrency is high, it will be a dead end.

FastCGI introduction

FastCGI brief introduction

Fundamentally speaking, FastCGI is used to improve the performance of CGI programs. Similar to CGI, FastCGI can also be said to be a protocol.

FastCGI is like a resident (long-live) CGI. It can be executed all the time. As long as it is activated, it will not cost every time. It’s time to fork once. 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.

Working Principle of FastCGI

FastCGI interface mode adopts C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing daemons on the script parsing server at the same time. Every time the HTTP server encounters a dynamic program, it can be delivered directly to the FastCGI process for execution, and then the result is returned to the browser. This method allows the HTTP server to exclusively handle static requests, or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

Diagram of the relationship between CGI, FastCGI and PHP-FPM

  1. Load the FastCGI process manager (Apache Module or IIS ISAPI, etc.) when the Web Server starts up

  2. FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi can be built), and waits for connections from the Web Server.

  3. When a client request reaches the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The web server sends CGI environment variables and standard input to the FastCGI subprocess php-cgi.

  4. After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits and handles the next connection from the FastCGI process manager (running in the Web Server). In CGI mode, php-cgi exits at this point.

Features of FastCGI and CGI:

  1. For CGI, PHP must re-parse and reload php.ini for every Web request All are expanded and all data structures are reinitialized. With FastCGI, all of this happens only once, when the process starts. An added bonus is that persistent database connections work.

  2. Since FastCGI is multi-process, it consumes more server memory than CGI multi-threading. The php-cgi interpreter consumes 7 to 25 megabytes of memory per process. Multiply this number by 50 Or 100 is a very large memory number.

Introduction to PHP-FPM

To understand PHP-FPM, you must first talk about PHP-CGI.

PHP-CGI is the FastCGI manager implemented by PHP. Although it is an official product of PHP, it is not powerful at all, the performance is too poor, and it is also very troublesome and impersonal. It is mainly reflected in:

  1. php-cgi changes php. After ini configuration, you need to restart php-cgi for the new php-ini to take effect, and smooth restart is not possible.

  2. Kill the php-cgi process directly, and php will not be able to run.

The above two problems have been bothering many people for a long time, so many people still use the Module method. It wasn't until 2004 that a loser named Andrei Nigmatulin invented PHP-FPM. The emergence of this artifact completely broke this situation. This is a PHP-specific Diagram of the relationship between CGI, FastCGI and PHP-FPM manager. It easily overcomes the above two problems, and, It also showed stronger performance in other aspects.

In other words, PHP-FPM is a specific implementation of the FastCGI protocol. It is responsible for managing a process pool to handle requests from the Web server. Currently, after PHP5.3 version, PHP-FPM is built into PHP.

Because PHP-CGI is just a CGI program, it can only parse requests and return results, but does not manage processes. Therefore, there have been some programs that can schedule the php-cgi process, such as spawn-fcgi separated from lighthttpd. Similarly, PHP-FPM is also a management program used to schedule and manage the PHP parser php-cgi.

PHP-FPM can achieve smooth restart after php.ini modification by generating a new child process.

Summary

Finally, let’s summarize what problems these technologies can solve after continuous upgrades (otherwise they wouldn’t be upgraded).

So, if you want to build a high-performance PHP WEB server, the best way currently is Apache/Nginx FastCGI PHP-FPM(PHP-CGI), don’t use Module loading or CGI method anymore:)

The pictures in this article were made with Visio, source file: php-fpm

Original address: https://www.awaimai.com/371.html

Related Recommended:

PHP7 Kernel Analysis 1 CGI and FastCGI

##What is the relationship between CGI, FastCGI and PHP_FPM?

The above is the detailed content of Diagram of the relationship between CGI, FastCGI and PHP-FPM. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template