Home > headlines > body text

Detailed explanation of the five common PHP operating modes

爱喝马黛茶的安东尼
Release: 2020-01-07 17:43:06
forward
4344 people have browsed it

Detailed explanation of the five common PHP operating modes

About the five common operating modes of PHP:

1) CGI (Common Gateway Interface)

2) FastCGI (Resident CGI/Long-Live CGI)

3) CLI (Command Line Run/Command Line Interface)

4) Web module mode (Web servers such as Apache Running mode)

5) ISAPI (Internet Server Application Program Interface)

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

1.1. CGI mode

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 to Connected to the execution program in the Web server, it passes the instructions received by HTML to the server's execution program, and then returns the results of the server's execution program to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system. CGI is already an older model and has rarely been used in recent years.

Every time there is a user request, a CGI sub-process will be created first, then the request will be processed, and the sub-process will be terminated after processing. This is the Fork-And-Execute mode. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be occupied, resulting in low performance. Therefore, a server using CGI will have as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance.

If you don’t want to embed PHP into server-side software (such as Apache) and install it as a module, you can choose to install it in CGI mode. Or use PHP with different CGI wrappers to create secure chroot and setuid environments for your code. In this way, each client requests a PHP file, and the Web server calls php.exe (php.exe under win, php under Linux) to interpret the file, and then returns the result of the interpretation to the client in the form of a web page. This installation method usually installs the PHP executable file into the cgi-bin directory of the web server. CERT Recommendation CA-96.11 recommends not placing any interpreters in the cgi-bin directory. The advantage of this method is that it separates the Web Server from specific program processing, has a clear structure and strong controllability. At the same time, the disadvantage is that if there is a high access demand, the CGI process fork will become a huge server burden. , imagine that hundreds of concurrent requests cause the server to fork hundreds of processes and you will understand. This is why CGI has always been notorious for low performance and high resource consumption.

1.2, FastCGI mode

FastCGI is an upgraded version of CGI. FastCGI is like a long-live CGI, which can always During execution, as long as it is activated, it will not take time to Fork every time (this is the most criticized fork-and-execute mode of CGI).

FastCGI is a scalable, high-speed interface for communication between HTTP servers and dynamic scripting languages. Most popular HTTP servers support FastCGI, including Apache, Nginx and lighttpd. At the same time, FastCGI is also supported by many scripting languages, including PHP.

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. 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 process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

[Principle]

1) The FastCGI process manager (IIS ISAPI or Apache Module) is loaded when the Web Server starts;

2) The FastCGI process manager initializes itself, Start multiple CGI interpreter processes (visible multiple php-cgi.exe or php-cig) and wait for connections from the Web Server;

3) When the client request reaches the Web Server, the FastCGI process manager Select and connect to a CGI interpreter. The Web server sends the CGI environment variables and standard input to the FastCGI sub-process php-cgi;

4) After the FastCGI sub-process completes processing, it returns the 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 for and handles the next connection from the FastCGI process manager (running in WebServer). In normal CGI mode, php-cgi.exe exits here.

In CGI mode, you can imagine how slow CGI usually is. Every web request to PHP must re-parse php.ini, reload all dll extensions and re-initialize all data structures. With FastCGI, all of this happens only once, when the process starts. An added bonus is that persistent database connections work.

Note: PHP’s FastCGI process manager is PHP-FPM (PHP-FastCGI Process Manager)

[Advantages]

1) From the perspective of stability, FastCGI uses an independent process pool to run CGI. If a single process dies, the system can easily discard it and then redistribute new processes to run the logic;

2) From a security perspective, FastCGI supports distributed computing. FastCGI is completely independent from the host server. No matter how FastCGI goes down, it will not bring down the server;

3) From a performance point of view, FastCGI separates the processing of dynamic logic from the server. Heavy-load IO processing is still Leave it 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, and a large number of static images such as pictures.

[Disadvantages]

After talking about the advantages, let’s talk about the disadvantages. From my actual use, FastCGI mode is more suitable for servers in production environments. But it is not suitable for development machines. Because when using Zend Studio to debug the program, FastCGI will think that the PHP process has timed out and return a 500 error on the page. This was so annoying that I switched back to ISAPI mode on my development machine. The support for new versions of some servers is not good, and modular installation that does not require distributed load balancing is a better choice. The current communication between FastCGI and the server is not smart enough. If a FastCGI process takes too long to execute, it will be killed and restarted as a dead process. This is very troublesome when processing long-term tasks. This also makes FastCGI unable to allow online debugging. Because it 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. Multiplying this number by 50 or 100 is a large amount of memory.

1.3 CLI mode

PHP-CLI is the abbreviation of PHP Command Line Interface. As its name implies, it is the interface for PHP to run on the command line. Different from the PHP environment (PHP-CGI, ISAPI, etc.) running on the web server. In other words, PHP can not only write front-end web pages, it can also be used to write back-end programs. PHP CLI Shell Scripting applies to all PHP advantages, enabling the creation of either scripts or server-side systems or even with GUI applications. PHP-CLI mode is supported under both Windows and Linux.

[Advantages]

1) When using multiple processes, after the child process ends, the kernel will be responsible for recycling resources;

2) When using multiple processes, the child process will be abnormal Exiting will not cause the entire process Thread to exit, and the parent process will still have the opportunity to rebuild the process;

3) A resident main process is only responsible for task distribution, and the logic is clearer.

We often use "php -m" under Linux to find out which extensions PHP has installed, which is the PHP command line running mode; interested students can enter "php -h" to study the running mode in depth.

1.4 Module mode

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. And process these requests, and then return the processed results to Apache. If we configure the PHP module

(mod_php5) in its configuration file before Apache starts, the PHP module registers the ap_hook_post_config hook of apache2 and starts this module when Apache starts to accept PHP files. request.

In addition to this loading method at startup, Apache modules can be dynamically loaded at runtime, which means that the server can be functionally expanded without recompiling the source code, or even at all. No need to stop the server. All we need to do is to send the signal HUP or AP_SIG_GRACEFUL to the server to notify the server to reload the module. But before dynamic loading, we need to compile the module into a dynamic link library. Dynamic loading at this time is to load the dynamic link library. The processing of dynamic link libraries in Apache is completed through the module mod_so, so the mod_so module cannot be dynamically loaded, it can only be statically compiled into the core of Apache. This means it is started along with Apache.

How does Apache load modules? Let’s take the mod_php5 module mentioned earlier as an example. First we need to add a line to Apache's configuration file httpd.conf:

LoadModule php5_module modules/mod_php5.so
Copy after login

Here we use the LoadModule command. The first parameter of the command is the name of the module. The name can be implemented in the module. Found in the source code. The second option is the path where the module is located. If you need to load a module while the server is running, you can send the signal HUP or AP_SIG_GRACEFUL to the server. Once the signal is received, Apache will reload the module without restarting the server.

This operating mode is what we often used when using the apache server in the windows environment. In modularization (DLL), PHP is started and run together with the web server. (It is an extension of apache based on CGI to speed up the operating efficiency of PHP).

1.5 ISAPI Mode

ISAPI (Internet Server Application Program Interface) is a set of API interfaces for Internet services provided by Microsoft. An ISAPI DLL can reside in memory after being activated by a user request, waiting for another request from the user, and can also Multiple user request processing functions are set up in one DLL. In addition, the ISAPI DLL application and the WWW server are in the same process, and the efficiency is significantly higher than CGI. (Due to Microsoft's exclusivity, it can only run in the windows environment)

PHP is an Apache module. After the Apache server starts the system, it pre-generates multiple process copies and resides in the memory. Once a request occurs, , these idle sub-processes are used immediately for processing, so that there is no delay caused by generating sub-processes. These server copies do not exit immediately after processing an HTTP request, but stay in the computer waiting for the next request. The response to client browser requests is faster and the performance is higher.

PHP Chinese website has a large number of free PHP introductory tutorials, everyone is welcome to learn!

This article is reproduced from: https://www.jianshu.com/p/e9eef6dc7d67

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