PHP development should be aware of the concept of PHP running mode, this article will share with you about PHP is now more common five operating modes: including CGI, fast-cgi, CLI, ISAPI, Apache module DLL, The following author of the five operating modes do some of their own analysis, there is a wrong place, please leave a message.
CGI run mode
CGI is the Universal Gateway Interface (Common Gateway Interface), it is a program, the popular CGI is like a bridge, the Web page and the Web server to connect the execution program, it takes HTML received instructions to the server's execution program, The results of the server execution program are then returned to the HTML page. CGI has excellent cross-platform performance and can be implemented on almost any operating system.
The CGI method encounters a connection request (user request) to create a CGI subprocess, activate a CGI process, then process the request, and end the subprocess after processing. This is the Fork-and-execute mode. So how many CGI sub-processes are there for the CGI server, and the sub-process loading is the main reason for the poor CGI performance. will be when the user requests a very long time, will be a lot of crowding out the system resources such as memory, CPU times, resulting in low performance.
FAST-CGI operating mode
FAST-CGI is an upgraded version of CGI, fastcgi is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it does not have to take a moment to fork it every time. PHP uses PHP-FPM (FastCGI process Manager), the full name of the PHP FastCGI process Manager to manage.
How the FastCGI works
1. Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) when Web server starts
2, the FASTCGI process manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.
3. When a client request arrives at Web server, the FASTCGI process manager selects and connects to a CGI interpreter. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.
4. After the fastcgi process finishes processing, the standard output and error information are returned from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here.
In the above scenario, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all extensions, and initialize all data structures. With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.
CLI Run mode
The CLI is the command-line run mode of PHP, you will often use it, but it may not be noticed (for example: we often use "php-m" in Linux to find PHP installed those extensions is the PHP command line running mode, interested students can input php-h to delve into the operation mode)
ISAPI Run mode
ISAPI, the Internet Server application Program Interface, is a set of Internet service-oriented API interfaces provided by Microsoft, an ISAPI DLL that can be long-standing in memory after being activated by a user request. Waiting for another user request, you can also set up a DLL in a number of user request handler functions, in addition, the ISAPI DLL application and the WWW server in the same process, the efficiency is significantly higher than the CGI. (due to the exclusivity of Microsoft, only running in the Windows environment)
Apache2handler
PHP as an Apache module, the Apache server after the system startup, pre-generated multiple copies of the process resides in memory, once a request appears, immediately use these spare child processes to process, so there is no generation of child process caused by the delay. These server replicas do not exit immediately after processing an HTTP request, but instead stay on the computer waiting for the next request. Requests for customer browsers respond faster and have higher performance.
The DLL run mode of Apache module
This mode of operation is often used by Apache servers in a Windows environment, and in a modular (DLL), PHP is started and run with a Web server. (It is an extension of Apache based on CGI, which accelerates the efficiency of PHP operation)
For each of the above operating mode has its own advantages and disadvantages, there is no absolute good and bad, mainly to see what the environment under what kind of needs.
A brief discussion on Cgi,fast-cgi,cli,isapi,apache mode of PHP five operation mode