Understanding PHP running mode


PHP has five running modes, four of which are common

  1. CGI (Common Gateway Interface)
  2. Fastcgi (resident CGI / long live CGI)
  3. CLI (command line running / command line interface)
  4. Loadmodule [unique to Apache]
  5. ISAPI (Internet server application program interface) [unique to IIS]

Note: after php5.3, PHP no longer has ISAPI mode and PHP5 after installation isapi.dll This is the file. To use a higher version of PHP on IIS6, you must install the fastcgi extension, and then make IIS6 support fastcgi.

View current running mode

echo php_sapi_name();

CGI mode


CGI is the common gateway interface. It is a program. Generally speaking, CGI is like a bridge, connecting the web page and the executive program in the web server. It transmits the instructions received by HTML to the executive program of the server, and then returns the results of the executive program of the server to the HTML page. CGI has excellent cross platform performance and can be implemented on almost any operating system.

Calling procedure: fork and execute mode

User request > Web server receives request [commonly used nginx, Apache] – > fork CGI sub process & processing request – > after processing the request, return the result to the web server & destroy the sub process – > Web server returns the result to the user


Understanding PHP running mode


As the earliest running mode of PHP, each request is handled independently, and the calling process is simple and clear enough with strong controllability

The process is isolated to ensure that the data will not be contaminated


Each request needs to fork a new CGI subprocess. If 1000 concurrent requests are made at the same time, it means that 1000 subprocesses will be fork. This will lead to several problems

  1. Fork subprocess needs time and memory [copy on write]
  2. If there are too many subprocesses, the CPU will spend a lot of time on context switching
  3. Each request needs to reload related resources

Fastcgi mode


Fastcgi is an upgraded version of CGI. Fastcgi is like a long live CGI. When starting the web server, load fastcgi Process Manager [php-fpm, IIS ISAPI, Apache module]. When a request comes, the web server only needs to hand it over to fastcgi process manager for processing.

Calling procedure: take php-fpm as an example

When the web server starts, start the php-fpm master process (mainly responsible for allocating requests to idle self processes) and a certain number of fast CGI sub 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 and does not interfere with each other.

User request – > Web server receives request [commonly used nginx, Apache] – > PHP request is detected & forward to FPM master process – > FPM master process specifies idle fast CGI subprocess to process request – > subprocess loads file (such as php.ini )And other resources processing request – > end of processing & clear resources, and return the result to master& The subprocess is suspended and marked as idle – > the master returns the result to the web server – > the web server returns the result to the user


Understanding PHP running mode


  1. In terms of stability, fastcgi runs CGI in an independent process pool. If a single process dies, the system can easily discard it, and then reassign a new process to run the logic
  2. In terms of security, fastcgi and the host web server are completely independent, and fastcgi will not affect the operation of the web server [if php-fpm is closed, 502 bad gateway will be returned to the user]
  3. In terms of performance, fastcgi separates the processing of dynamic logic from the web server, and the heavy load IO processing is left to the host server, so that the host server can concentrate on Io [for an ordinary dynamic web page, the logic processing may only have a small part, and more importantly, the loading of static resources such as pictures]


A fast CGI subprocess can only process one request at a time, so the concurrency performance of a website is limited by the number of subprocesses

If too many processes are opened, CPU will waste a lot of time on context switching.

Every time the request fast CGI subprocess needs to reload the related resources, and the resource needs to be released at the end of the request

CLI (command line running / command line interface)


PHP cli mode belongs to the command line mode, which is the most unfamiliar running mode for many developers who have just started learning PHP and started Wamp and wnmp

This mode does not need other programs to input PHP directly xx.php You can execute PHP code

The obvious differences between the command line mode and the conventional web mode are as follows:

There is no timeout

The buffer buffer is 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

Different classes / functions are available

php.ini Different configurations

PS: see official document for details:www.php.net/manual/zh/features.com…

Loadmodule (unique to Apache)


Module mode is based on mod_ In this case, mod_ The function of PHP5 module is to receive PHP file requests from Apache, process these requests, and then return the processed results to Apache.

In Apache configuration file httpd.conf Load module php7 is usually added_ module “D:/… /php71/php7apache2_ This is what “4. DLL” does

Call procedure:

User request > Apache server > call Mod_ The PHP5 module processes the request and returns the result of the request to Apache


Understanding PHP running mode

ISAPI(Internet Server Application Program Interface)


After php5.3, PHP no longer has ISAPI mode and PHP5 after installation isapi.dll This is the file. To use a higher version of PHP on IIS6, you must install the fastcgi extension, and then make IIS6 support fastcgi. Therefore, we don’t make too much introduction here


Understanding PHP running mode

At the end:

The development of the vast majority of concurrent web sites today needs to consider high performance. Nginx is more and more popular with developers because of its lightweight and excellent concurrency performance. The once popular lamp combination in the whole network is no longer the first choice for PHP developers, and the suddenly rising LNMP combination has become a compulsory course for PHPer, which leads to the less and less mention of Apache based loadmodule mode.

In addition, the CLI mode for writing 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 CGI’s simplicity and security, and uses php-fpm to manage sub processes, which makes web server more focused on I / O processing. Php-fpm management maintains a process pool, which can fork a certain number of sub processes in advance to wait for processing requests. It does not need to fork sub processes when requests come, nor destroy sub processes when requests end

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]