Home > Backend Development > PHP Tutorial > Take you to know the PHP operating mode

Take you to know the PHP operating mode

藏色散人
Release: 2023-04-09 18:34:01
forward
3258 people have browsed it

Recommended: "PHP Video Tutorial"

PHP has five operating modes, and there are 4 common ones:

1.CGI (Common Gateway Interface)
2.FastCGI (Resident CGI/Long-Live CGI)
3.CLI (Command Line Interface)
4.LoadModule [Apache exclusive]
5.ISAPI (Internet Server Application Program Interface) [IIS exclusive]

Note: After PHP5.3, PHP no longer has an ISAPI mode, nor does it exist after installation. php5isapi.dll file. To use a higher version of PHP on IIS6, you must install the FastCGI extension and then enable IIS6 to support FastCGI.

View the current running mode
echo php_sapi_name();

CGI mode

Introduction:

CGI is the Common Gateway Interface (Common Gateway Interface). It is a program. In layman's terms, CGI is like a bridge that connects web pages and execution programs in the web server. It receives HTML The instructions are passed to the server's execution program, and then the results of the server's execution program are returned to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system.

Calling process: Fork-And-Execute mode

User request —> web server receives request [commonly used Nginx, apache] —> fork CGI child process &Process the request--> After the request is processed, return the result to the web server&Destroy the child process-> The web server returns the result to the user

Illustration:
Take you to know the PHP operating mode
Advantages:

As the earliest operating mode of PHP, each request is processed independently, the calling process is simple and clear enough, and the controllability is strong
The processes are isolated, ensuring The data will not be polluted

Disadvantages:

Each request requires forking a new CGI subprocess. If there are a thousand concurrent requests at the same time, it means that Forking a thousand sub-processes will lead to several problems:

  1. Forking sub-processes takes time and takes up memory [copy-on-write]
  2. There are too many sub-processes. Will cause the CPU to spend a lot of time on context switching
  3. Each request requires reloading related resources

FastCGI mode

Introduction:

FastCGI is an upgraded version of CGI. FastCGI is like a long-live CGI. When starting the web server, the FastCGI process manager [PHP- FPM, IIS ISAPI, Apache Module], when a request comes, the web server only needs to be handed over to the FastCGI process manager for processing.
Calling process: Take PHP-FPM as an example

When the web server starts, the PHP-FPM master process is started (mainly responsible for allocating requests to idle self-child processes for processing) and certain Number of fast-cgi child processes (responsible for processing requests). The PHP-FPM master process manages a process pool. There are several fast-cgi sub-processes in the pool. Each fast-cgi sub-process processes a request independently without interfering with each other.

User request—> The web server receives the request [commonly used Nginx, apache]—> Detects that it is a PHP request & forwards it to the FPM master process—> The FPM master process specifies the idle fast-cgi sub-process to handle the request —> The sub-process loads files (such as php.ini) and other resource processing requests —> Processing ends & clears resources, the result is returned to master & the sub-process hangs, marked as idle —> master returns the result to the web server — > The web server returns the results to the user

Illustration:
Take you to know the PHP operating mode
Advantages:

  1. From stable From a safety point of view, FastCGI uses an independent process pool to run CGI. If a single process dies, the system can easily discard it and then reassign a new process to run the logic
  2. From a safety point of view, FastCGI Completely independent from the host web server, FastCGI will not affect the operation of the web server [If PHP-FPM is turned off, 502 bad gateway will be returned to the user]
  3. From a performance point of view, FastCGI changes the dynamic logic The processing is separated from the web server, and the heavy-load IO processing is still left to the host server, so that the host server can concentrate on IO. [For an ordinary dynamic web page, there may only be a small part of the logical processing, more pictures, etc. Loading of static resources】

Disadvantages:

A fast-cgi sub-process can only handle one request at a time, so the concurrent performance of the website is limited by the number of sub-processes
If too many processes are opened, the CPU will waste a lot of time on the process. Context switch on.
Each time the fast-cgi child process is requested, the relevant resources need to be reloaded, and the resources need to be released after the request is completed

CLI (Command Line Run/Command Line Interface)

Introduction:

php-cli mode belongs to the command line mode. It is the most unfamiliar operating mode for many developers who have just started learning PHP and started wamp and wnmp
This mode does not require the use of other programs. You can directly enter php xx.php to execute the php code.
The obvious difference between the command line mode and the regular web mode is:
No timeout period
Buffer buffering is turned off by default
Use of STDIN and STDOUT standard input/output/error
echo var_dump, phpinfo and other outputs are directly output to the console
The classes/functions that can be used are different
The php.ini configuration is different

PS: See the official documentation for details: www.php.net/manual/zh/features.com...

LoadModule (Apache exclusive)

Introduction:
The module mode is integrated in the form of mod_php5 module. At this time, the function of mod_php5 module is to receive PHP file requests passed by Apache, process these requests, and then return the processed results to Apache. .
In the Apache configuration file httpd.conf, the usually added LoadModule php7_module "D:/…/php71/php7apache2_4.dll" plays the role of this

Calling process:

User request—> Apache server—> Call the mod_php5 module to process the request—> Return the request result to Apache—> Apache returns the result to the user

Illustration:
Take you to know the PHP operating mode

ISAPI (Internet Server Application Program Interface)

Introduction:

In PHP5. After 3, PHP will no longer have an ISAPI mode, and there will no longer be the php5isapi.dll file after installation. To use a higher version of PHP on IIS6, you must install the FastCGI extension and then enable IIS6 to support FastCGI. Therefore, I won’t introduce too much here

Illustration:
Take you to know the PHP operating mode

Written at the end:

Today, with the explosive development of the Internet, most websites need to consider the high concurrency performance of the website. Nginx is increasingly favored by developers due to its lightweight and excellent concurrency performance. The LAMP combination that was once popular across the Internet is no longer the first choice for PHP developers. The suddenly emerging LNMP combination has become a required course for PHPer. This has resulted in the Apache-based LoadModule model being mentioned less and less.
In addition, the CLI mode used to write command line scripts and the almost extinct ISAPI mode are obviously not suitable for building websites.
As an enhanced version of CGI, Fast-CGI mode inherits the simplicity and security of CGI and uses PHP-FPM to manage child processes, allowing the web server to focus more on processing I/O. PHP-FPM manages and maintains a process pool. , a certain number of child processes can be forked in advance to wait for processing requests. There is no need to fork the child process when the request comes, nor to destroy the child process when the request ends.

The above is the detailed content of Take you to know the PHP operating mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
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