Home >Backend Development >PHP Tutorial >Five major PHP operating modes
The five most common operating modes of PHP are CGI (Common Gateway Interface), FastCGI (Resident CGI/Long-Live CGI) ), CLI (Command Line Operation/Command Line Interface), Web module mode (the mode in which Web servers such as Apache run), ISAPI (Internet Server Application Program Interface), I hope this article can help everyone.
Note: After PHP5.3, PHP no longer has ISAPI mode, and there is no longer 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.
1.1. CGI mode
CGI is the Common Gateway Interface (Common Gateway Interface), which is a program. In layman's terms, CGI is like a bridge that connects web pages and 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 has excellent cross-platform performance 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 to 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 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. 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-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 and starts multiple CGI interpreter process (visible multiple php-cgi.exe or php-cig) and wait for the connection from the Web Server;
3) When the client request reaches the Web Server, the FastCGI process manager selects and connects 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 stable From a performance perspective, 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 security perspective See, 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 and so on.
【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, PHP runs on the command line. The interface is 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 server-side scripts or system or even GUI applications. PHP-CLI mode is supported under Windows and Linux.
[Advantages]
1) Using multiple processes, after the child process ends, the kernel will be responsible for recycling resources;
2) Using multiple processes, the child process will not exit abnormally Causes the entire process Thread to exit, and the parent process still has the opportunity to rebuild the processA resident main process, only responsible for task distribution, the logic is clearer;
3) Timing tasks can be implemented and shell scripts can be written using PHP under Linux.
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 passed by Apache File requests, process these requests, and 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 requests for PHP files.
In addition to this loading method at startup, Apache's modules can be dynamically loaded at runtime, which means that the server can be functionally expanded without recompiling the source code, or even needing to stop at all. 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
Here we use the LoadModule command, the first parameter of the command is the module The name can be found in the source code of the module implementation. 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 The DLL can reside in memory after being activated by a user request and wait for another user request. Multiple user request processing functions can also be set in one DLL. In addition, the ISAPI DLL application and the WWW server are in the same process. , 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 system starts, the Apache server pre-generates multiple process copies to reside in the memory. Once a request occurs, it will be processed immediately. Use these spare sub-processes for processing, so there is no delay caused by spawning 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.
Related recommendations:
Detailed explanation of the four PHP operating modes
Summary of PHP operating modes
In-depth understanding of PHP operating mode_PHP tutorial
The above is the detailed content of Five major PHP operating modes. For more information, please follow other related articles on the PHP Chinese website!