1. Operation mode
The five most common operating modes for PHP are now:
1) CGI (Universal Gateway Interface/Common Gateway Interface)
2) FastCGI (resident type cgi/long-live CGI)
3) CLI (command line run/Interface)
4) Web module mode (the mode of operation of Web server such as Apache)
5) ISAPI (Internet Server application program Interface)
Note: After PHP5.3, PHP no longer has ISAPI mode, and after installation no longer has php5isapi.dll this file. To use a high version of PHP on IIS6, you must install the fastcgi extension, and then make IIS6 support fastcgi.
1.1 CGI 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. CGI is already a relatively old pattern, and it has seldom been used in recent years.
For each user request, the child process of the CGI is created first, then the request is processed, and the child process is finished after processing, which is the Fork-and-execute mode. When the user requests a very long time, will be heavily crowding out the system resources such as memory, CPU times, resulting in low performance. 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.
If you do not want to embed PHP into the server-side software (such as Apache) as a module installation, you can choose to install in CGI mode. Or use PHP for different CGI wrappers to create a secure chroot and SETUID environment for your code. So that each client requests a PHP file, the Web server calls Php.exe (Php.exe,linux is PHP) to interpret the file, and then returns the result of the explanation as a Web page to the client. This installation typically installs the PHP executable file to the Web server's Cgi-bin directory. CERT Proposal CA-96.11 recommends that you do not put any interpreter in the Cgi-bin directory. The advantage of this approach is that the Web server and the specific program processing independent, clear structure, controllability, and the disadvantage is that if in the case of high access requirements, CGI process fork will become a large server burden, want to Like a hundreds of concurrent requests cause the server to fork out hundreds of processes to understand. This is why CGI has been saddled with poor performance and high resource consumption for notoriety reasons.
1.2 fastcgi mode
FastCGI is an upgraded version of CGI, fastcgi like a resident (long-live) type of CGI, it can be executed all the time, as long as the activation, not each time will have to spend to Fork once (this is the most criticized by CGI Fork-and-execute mode).
FastCGI is a scalable, high-speed interface for communicating between HTTP server and dynamic scripting languages. Most popular HTTP servers support FASTCGI, including Apache, Nginx, and lighttpd, while FASTCGI is supported by a number of scripting languages, including PHP.
FastCGI interface mode uses the C/s structure, can separate the HTTP server and the script parsing server, and start one or more script parsing daemon on the script parsing server. Each time the HTTP server encounters a dynamic program, it can be delivered directly to the fastcgi process to execute, and the resulting results are returned to the browser. This approach allows the HTTP server to handle static requests exclusively or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system. Principle
1) Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) at Web server startup;
2) The FASTCGI process Manager itself initializes, initiates multiple CGI interpreter processes (visible multiple php-cgi.exe or PHP-CIG) 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 subprocess php-cgi; 4) The fastcgi child process finishes processing and returns standard output and error information 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 webserver). In the normal CGI mode, php-cgi.exe exits here.
In CGI mode, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all DLL 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.
Note: PHP's FastCGI process Manager is PHP-FPM (php-fastcgi process Manager) "Benefits"
1) From the stability perspective, fastcgi is a separate process pool to run CGI, a single process is dead, the system can easily discard, and then redistribute new processes to run the logic;
2) from a security perspective, FASTCGI supports distributed operations. FastCGI and the host server is completely independent, fastcgi how to down will not bring the server down;
3) From a performance standpoint, fastcgi separates the processing of the dynamic logic from the server, and the heavy-duty IO processing is left to the host server, so that the host server can focus on Io, and for a normal dynamic Web page, the logical processing may be only a small part, A lot of is the picture and so on static. Disadvantages
Say the good, also say the shortcomings. From my actual use, using FASTCGI mode is more suitable for the production environment of the server. But it's not very suitable for development machines. Because when using the Zend Studio debugger, the PHP process is returned with a 500 error on the page because fastcgi will think it timed out. This was very annoying, so I switched back to ISAPI mode on the development machine. Support for new versions of some servers is not good, and it is a better choice for a modular installation that is not required for distributed load balancing. The current fastcgi and server communication is not smart enough, a fastcgi process if the execution time is too long will be treated as a dead process to kill the restart, so in the processing of long-time task is very troublesome, this also makes fastcgi can not allow online debugging. Because it is multi-process, so consumes more server memory than CGI multithreading, php-cgi interpreter consumes 7 to 25 megabytes per process, multiplying this number by 50 or 100 is a large amount of memory.
1.3 CLI Mode
PHP-CLI is the abbreviation for PHP command line interface, as its name means, the interface that PHP runs on the command lines, different from the PHP environment (PHP-CGI,ISAPI, etc.) running on the Web server. In other words, PHP can not only write the foreground page, it can also be used to write the background of the program. PHP's CLI shell scripts apply to all PHP advantages, making it either support scripts or systems or even with GUI applications that support PHP-CLI mode under Windows and Linux. Advantages
1) Using multi-process, after the completion of the child process, the kernel will be responsible for recycling resources;
2) Using multiple processes, the child process exception exits will not cause the entire process thread to exit, the parent process also has the opportunity to rebuild the process;
3) A permanent master process, which is responsible for the distribution of tasks, and the logic is clearer.
We often use "php–m" under Linux to find PHP installed those extensions is the PHP command line running mode, interested students can enter "Php–h" to delve into the operation mode.
1.4 Module Mode
Module mode is integrated in the form of a MOD_PHP5 module, at which point the MOD_PHP5 module is to receive PHP file requests from Apache, process the requests, and then return the processed results to Apache. If we have a PHP module configured in its configuration file before Apache starts
(MOD_PHP5), the PHP module registers the apache2 ap_hook_post_config hook, launches this module when Apache starts to accept the PHP file request.
In addition to this boot-time loading, Apache modules can be loaded dynamically at runtime, which means that the server can be expanded without the need to re-compile the source code or even stop the server at all. All we need to do is send a signal to the server Hup or ap_sig_graceful notify the server to reload the module. But before we load it dynamically, we need to compile the module into a dynamic-link library. Dynamic loading at this time is the loading of the dynamic link library. The processing of the dynamic link library in Apache is done through the module Mod_so, so the Mod_so module cannot be dynamically loaded, it can only be statically compiled into Apache core. This means that it was launched with Apache.
How does Apache load the module? Let's take the MOD_PHP5 module mentioned earlier as an example. First we need to add a line to the Apache 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 module implementation of the source code. The second option is the path where the module is located. If you need to load the module while the server is running, you can send the signal hup or ap_sig_graceful to the server, and once the signal is accepted, Apache will reload the module without restarting the server.
This is a running mode that we used to use with 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).
1.5 ISAPI Mode
ISAPI (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)
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.
PHP operating Environment comparison of CGI, FastCGI, CLI, Apache, ISAPI