PHP Run mode introductory article

Source: Internet
Author: User
Tags server memory

PHP common mode of operation:

1.CGI(Universal Gateway Interface/Common Gateway Interface)--apache Server encounters a PHP script that is interpreted by the CGI program and returns an explanation of the results to the Apache server.

2.FastCGI(resident cgi/long-live CGI)--CGI's enhanced version

3.CLI(command line Run/Interface)--php command line run mode

4.Web Module mode (the mode that the Web server runs like Apache)--integrates PHP as a module into the Apache server and runs in the same process.

Learn more about:

1.cgi

CGI is a single-process, multi-threaded operation, after the execution of the program will be destroyed, so each time you need to load the configuration and environment variable Fork-and-execute (Create-execute). 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.

Because of the low CGI performance, high resource consumption, the pattern is older, and now rarely used.

2.fastcgi

FastCGI is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it will not take a moment to fork it every time. The FASTCGI process Manager itself initializes, launches multiple CGI interpreter processes (multiple Php-cgi.exe visible in Task Manager) and waits for connections from the Web server.

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, launches multiple CGI interpreter processes (visible multiple php-cgi.exe or PHP-CIG) and waits for connections 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. WEB server sends CGI environment variables and standard input to the fastcgi subprocess php-cgi;

4. The FASTCGI child process returns standard output and error information from the same connection to the Web Server after processing is complete. 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 processes manager)

Advantages

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 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, Logic processing may be only a small part, a lot of pictures and other 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.

3.cli

"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.

4. Run in PHP 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 configure the PHP module (MOD_PHP5) in its configuration file before Apache starts, the PHP module registers the apache2 ap_hook_post_config hook and starts the module to accept PHP file requests when Apache starts.

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).

Reference: http://www.cnblogs.com/xia520pi/p/3914964.htmlhttp://www.jb51.net/article/38329.htm

Http://www.jb51.net/article/37756.htm


PHP Run mode introductory article

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.