PHP working mode

Source: Internet
Author: User
Tags php source code

PHP run mode

1.CGI Universal Gateway Interface (less)
2.FAST-CGI resident Type CGI [NGIXN common]
3.CLI command Run (command line is used much)
4.web Module Mode (operation mode of Web server such as Apache) [Apache Common]

1.CGI Universal Gateway Interface
is a program, popular, like a bridge, the Web page and the Web server to connect the execution program,
It passes the instructions that the HTML receives to the server's execution program, and then returns the results of the server execution program
Back to HTML pages, CGI has excellent cross-platform performance and can be implemented on almost any system. Older models,
It's a little too good to use now.
Each user request will first create a CGI subprocess, then process the request and end the subprocess after processing.
This is the Fork-and-execute mode. When the user requests a very large number of resources, will be heavily crowding out the system,
such as memory, CPU time, and so on, resulting in low performance, CGI server has how many connection requests, there will be
How many CGI sub-processes, sub-process repeatedly loading is the main reason for poor CGI performance
This mode installs:
Remove Comments:
LoadModule Php5_module modules/libphp5.so This line, if not to go, is handler mode, that is, module mode
Then add the action in httpd.conf:
Action application/x-httpd-php/cgi-bin/

2.Fastcgi mode
FAST-CGI is an upgraded version of CGI, FastCGI is like a resident (long-live) type of CGI,
It 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.
How the FastCGI works is:
(1), the WEB server starts loading the FastCGI process Manager "PHP FastCGI process Manager is PHP-FPM (php-fastcgi processes manager)"
(IIS ISAPI or Apache Module);

(2), fastcgi process Manager itself initialized, start multiple CGI interpreter processes (multiple Php-cgi.exe visible in Task Manager)
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 messages 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
FastCGI the next connection in the Process manager (running in webserver). In the normal CGI mode,
The Php-cgi.exe then withdrew.

In CGI mode, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini,
Reload all of the DLL extensions and reinitialize all data structures. Using FastCGI,
All of this occurs only once when the process is started. An additional benefit is that
Persistent database connections (persistent connection) can work.

Advantages of fastcgi:
1) From a stability perspective, fastcgi is running in a separate process pool to CGI, a single process dies,
The system can easily discard and reassign new processes to run the logic.

2) from a security perspective, FASTCGI supports distributed operations. FastCGI and host server are completely independent,
How fastcgi down will not bring the server down.

3) from a performance perspective, fastcgi separates the processing of dynamic logic from the server,
Heavy-duty IO processing is still left to the host server, so that the host server can concentrate on Io,
For a normal Dynamic Web page, the logic processing may be only a small part, a large number of pictures and other static

Use fastcgi mode for servers that are more suitable for production environments.

3.CLI Command mode
In Linux, the frequently used PHP xx.php is the mode

4. Module Mode:
Module mode is integrated in the form of a MOD_PHP5 module, at which time the MOD_PHP5 module is receiving Apache
Pass over the PHP file request and process the requests, and then return the processed results to Apache.
If we have a PHP module (MOD_PHP5) configured in its configuration file before Apache starts,
The PHP module launches this module by registering the apache2 ap_hook_post_config Hook, which is launched at Apache
To accept requests for PHP files.
In addition to this boot-time loading mode, Apache modules can be loaded dynamically at runtime, which means that the server can be extended to function
You do not 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 notifies 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 dynamic-link libraries in Apache is done through 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
This mode of operation is often used in Windows environments where we use Apache servers, and in a modular (DLL) format,
PHP is started and run with the Web server.
(It is an extension of Apache based on CGI, which accelerates the efficiency of PHP operation)

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 a module while the server is running, you can send a signal hup
or ap_sig_graceful to the server, once the signal is received, Apache will reload the module without restarting the server.

PHP operating mode in Nginx (nginx+ php-fpm)
There are two types of stacks that are now common using the FastCGI method:
ligthttpd+spawn-fcgi; the other is nginx+php-fpm (can also be used with spawn-fcgi).

A, as mentioned above, the two structures are using fastcgi to PHP support, so httpserver completely liberated,
Better response and concurrency processing is possible. So lighttpd and Nginx have small, but powerful and efficient reputation.

B, the two can also distinguish a good or bad, spawn-fcgi because it is part of the LIGHTTPD,
As a result, installing LIGHTTPD will typically use spawn-fcgi for PHP support,
But now there are users who say that LIGTTPD's spwan-fcgi in high concurrent access
, there will be a memory leak mentioned above and even automatic restart fastcgi. That is: PHP script processor when the machine,
This time, if the user visits, there may be a white page (that is, PHP can not be parsed or error).

Another: First nginx is not like lighttpd itself with fastcgi (spawn-fcgi),
So it's completely lightweight and must be fastcgi by a third-party processor to parse PHP.
So in fact it looks like Nginx is very flexible and it can be provided with any third party
The parsed processor implements the connection to enable parsing of PHP (easy to set up in nginx.conf).
Nginx can use spwan-fcgi, but because spawn-fcgi has the flaw that the user of the above mentioned gradually discovers,
Now slowly reduce the use of nginx+spawn-fcgi combination.

C, due to the defects of spawn-fcgi, now appears the new third-party PHP fastcgi processor, called PHP-FPM.
Compared with spawn-fcgi, it has the following advantages:

Since it was developed as a patch for PHP, it needs to be compiled with the PHP source code when it is installed.
In other words, it's compiled into PHP core, so it's better in terms of performance.
It also outperforms spawn-fcgi in handling high concurrency, at least not automatically restarting the FASTCGI processor.
Therefore, as mentioned above, because of the light weight and flexibility of nginx, so the performance is superior,
More and more people are gradually using this combination: NGINX+PHP/PHP-FPM

Currently in

Httpserver this piece of basic can see there are three kinds of stacks are more popular:
(1) APACHE+MOD_PHP5

(2) lighttp+spawn-fcgi

(3) NGINX+PHP-FPM

After all, the performance may be slightly better, but Apache is still the boss because of its rich modules and functions. Some people test that NGINX+PHP-FPM may reach apache+mod_php5 5~10 times in high-concurrency situations, and now nginx+php-fpm use more and more.

PHP working mode

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.