Home >Backend Development >PHP Tutorial >Detailed explanation of the operating mode of php on the web server
This article brings you a detailed explanation of the operating mode of PHP on the web server. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .
CGI Common Gateway Interface (Common Gateway Interface)
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 The cross-platform performance is excellent 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 do not 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 high access demand, the cgi process will fork. It becomes a huge burden on the server. Just imagine that hundreds of concurrent requests cause the server to fork hundreds of processes. This is why cgi has always been notorious for low performance and high resource consumption.
fast-cgi (long-live CGI)
A variant of cgi mode, that is (cgi is a normal People, fast-cgi is special forces)
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).
The working principle of FastCGI is:
The FastCGI process manager is loaded when the Web Server starts [PHP’s FastCGI process manager is PHP-FPM (php-FastCGI Process Manager)】(IIS ISAPI or Apache Module);
FastCGI process manager initializes itself and starts multiple CGI interpreter processes (visible in the task manager multiple php-cgi.exe) and wait for a connection from the Web Server.
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.
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 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 connection works
cli Command Line Interface
Type php script.php in the terminal Waiting for the command line
web module mode (module mode for running web servers such as apache)
The module mode is integrated in the form of the mod_php5 module. At this time, the role of the mod_php5 module It receives PHP file requests passed by Apache, processes these requests, and then returns the processed results to Apache. It is an extension of Apache based on CGI to speed up the operating efficiency of PHP. 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 modules can be dynamically loaded at runtime, which means that the server can be expanded without the need to recompile the source code, or even without stopping the server at all. 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? 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 name of 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.
The above is the detailed content of Detailed explanation of the operating mode of php on the web server. For more information, please follow other related articles on the PHP Chinese website!