PHP currently more common five operating modes ____php

Source: Internet
Author: User

Run mode

About PHP currently more common five major operating modes:

1 CGI (Universal Gateway Interface/Common gateways Interface)

2) FastCGI (resident type cgi/long-live CGI)

3 CLI (command-line run/command line Interface)

4 Web Module mode (Apache and other Web server running mode)

5) ISAPI (Internet Server application program Interface)

Note: After PHP5.3, PHP no longer has ISAPI mode, after installation there is no 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, popular speaking CGI is like a bridge, the Web page and the Web server in the implementation of the program to connect the HTML received instructions to the server to execute the program, Return the results of the server execution program 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 model, and it's been used very little for years.


Each user request will first create a CGI subprocess, then process the request, end the subprocess after processing, which is the Fork-and-execute mode. When the user requests a very long time, will be a large number of crowding out the system's resources such as memory, CPU times, resulting in low performance. So the CGI server has how many connection requests there will be the number of CGI child processes, the child process repeatedly loading is the main reason for the low performance of CGI.


If you do not want to embed PHP in server-side software (such as Apache) as a modular installation, you can choose to install it in CGI mode. Or use PHP in a different CGI wrapper to create a secure chroot and SETUID environment for your code. So each client requests a PHP file, the Web server calls Php.exe (Win is Php.exe,linux is PHP) to explain the file, and then the interpretation of the results as a Web page back to the client. This installation usually installs the PHP executable file to the Cgi-bin directory of the Web server.   CERT proposal CA-96.11 recommend that you do not place any interpreter in the Cgi-bin directory. The advantage of this approach is that the Web server and specific program processing independent, clear structure, strong controllability, and the disadvantage is that if the high access requirements, the CGI process fork will become a large server burden, want to Just like hundreds of concurrent requests that cause the server to fork out hundreds of processes is clear. That's why CGI has been saddled with the stigma of low performance and high resource consumption.

1.2, fastcgi mode

FastCGI is an upgraded version of CGI, fastcgi like a resident (long-live) CGI that can be executed all the time, as long as it is activated, it will not take a while to Fork once (this is CGI's most criticized Fork-and-execute model).

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, and fastcgi are supported by a number of scripting languages, including PHP.

The FastCGI interface adopts the C/s structure, which separates the HTTP server from the script resolution server, and initiates one or more script resolution Daemons on the script resolution 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 HTTP servers to handle static requests in a single-minded manner or to return the results of a 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) when the WEB server is started;

2 fastcgi The Process Manager itself, 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 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;

4 after the fastcgi process completes 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. The fastcgi process then waits and processes the next connection from the FASTCGI process Manager (running in webserver). In the normal CGI mode, Php-cgi.exe quits here.

In CGI mode, you can imagine how slow CGI is usually. Each Web request PHP must reparse php.ini, reload all DLL extensions, and reinitialize all data structures. With fastcgi, all of these occur only once when the process is started. An additional benefit is that persistent database connections (persistent DB connection) can work.

Note: PHP's FastCGI process Manager is PHP-FPM (php-fastcgi processes manager)

Advantages

1 from the perspective of stability, fastcgi is a separate process pool to run CGI, a single process dead, the system can easily discard, and then redistribute new processes to run the logic;

2 from the security perspective, FASTCGI supports distributed computing. FastCGI and host server completely independent, fastcgi how down also won't bring server down;

3 from the performance point of view, fastcgi the processing of dynamic logic from the server, the large load of IO processing or left to host server, so that the host server can be dedicated IO, for a normal Dynamic Web page, logic processing may be only a small part of A lot of pictures are static.

Shortcomings

Said the good, also said the shortcoming. From my actual use, the fastcgi mode is more suitable for the server in the production environment. But for the development of the machine is not very appropriate. Because when the Zend Studio debugger is used, FASTCGI returns a 500 error on the page because it thinks the PHP process timed out. This is very annoying, so I switched back to ISAPI mode on the development machine. The new version of some servers is poorly supported and is a better choice for modular installations that are 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, so in the process of processing long time task is troublesome, this also makes fastcgi can not allow online debugging. Because it is a multiple process, it consumes more server memory than CGI multithreading, and the 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-line, different from the PHP environment (PHP-CGI,ISAPI, etc.) that runs on the Web server. In other words, PHP can not only write the front page, it can also be used to write the background of the program. PHP's CLI shell script applies to all PHP advantages, enabling the creation of either a script or system or even a server with a GUI application that supports PHP-CLI mode under Windows and Linux.

Advantages

1 using multiple processes, after the completion of the child process, the kernel will be responsible for the recovery of resources;

2 The use of multiple processes, the abnormal exit of the subprocess will not cause the entire process thread exit, the parent process has the opportunity to rebuild the process;

(3) A resident main process, which is responsible for the distribution of tasks, is more clearly logical.

We often use "php–m" in Linux to find PHP installed those extensions is the PHP command line mode of operation, interested students can input "php–h" to in-depth study of the operating mode.

1.4 Module Mode

Module mode is integrated in the form of MOD_PHP5 module, at this time the function of the MOD_PHP5 module is to receive the PHP file request from Apache, and handle these requests, then return the processed result to Apache. If we configure the PHP module in the configuration file before Apache starts

(MOD_PHP5), PHP module through the registration of Apache2 Ap_hook_post_config Hook, when the Apache launch this module to accept the PHP file request.

In addition to this startup loading mode, Apache modules can be dynamically loaded at runtime, which means that the server can extend functionality without having to recompile 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 dynamic loading, we need to compile the module into a dynamic link library. The dynamic load at this point is loading 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 loaded dynamically, it can only be statically compiled into the core of Apache. This means that it's started with Apache.

How does Apache load a module? Let's take the MOD_PHP5 module mentioned above 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. If you need to load a module while the server is running, you can send a signal hup or ap_sig_graceful to the server, and once that signal is received, Apache reloads the module without restarting the server.

This is the mode that we used to use the Apache server in a Windows environment, and in a modular (DLL), PHP is started and run with the Web server. (It's an extension of Apache based on CGI to speed up PHP's efficiency).

1.5 ISAPI Mode

The ISAPI (Internet Server application program Interface) is a set of Internet service-oriented API interfaces provided by Microsoft, an ISAPI DLL that can be in memory long after being activated by a user request, Waiting for another request from the user, you can also set up multiple user request processing functions in one DLL, in addition, the ISAPI DLL application and the WWW server are in the same process and are significantly more efficient than CGI. (due to Microsoft's exclusivity, can only run in the Windows environment)

PHP as the Apache module, the Apache server after the system is started, the advance generation of multiple copies of the process reside in memory, once the request appears, immediately use these free subprocess processing, so there is no delay in the generation of child processes. These server replicas do not exit immediately after processing an HTTP request, but stay on the computer for the next request. Faster response to client browser requests and higher performance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.