A detailed explanation of how PHP works

Source: Internet
Author: User
1. All of PHP's applications are performed through a Web server (such as Iis,nginx or Apache) and the PHP engine program to explain the execution of the work process:

(1) When the user enters the file name of the PHP page to be accessed in the browser address, then triggers a Web request and delivers the request to the Web server.

(2) The Web server accepts this request, and according to the suffix it is a PHP request, the Web server pulls the PHP application that the user wants to access from the hard disk or in memory and sends it to the PHP engine program.

(3) The PHP engine will scan files from the Web server and read from the background, process the data, and dynamically generate the corresponding HTML page.

(4) The PHP engine returns the generated HTML page to the Web server. The Web server then returns the HTML page to the client browser.

2. PHP operating mode:

1) CGI Universal Gateway Interface (Common Gateway Interface))

2) fast-cgi resident (long-live) type CGI

3) CLI command line run (Interface)

4) Web module mode (module mode for Web server running like Apache)

Additional explanations:

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

2) PHP operating mode in Nginx (nginx+ php-fpm)

For details, please see the Nginx + PHP principles section

Add:

1. CGI, fast-cgi protocol

The history of CGI

The CGI name is a "public Gateway Interface" (Common Gateway Interface), a tool that the HTTP server "chats" with programs on your or other machines, and its programs must be run on a network server. CGI can be written in any language, as long as the language has standard input, output, and environment variables. such as PHP,PERL,TCL and so on.

The early webserver only processed static files such as HTML, but with the development of technology, dynamic languages such as PHP appeared. Webserver can't handle it, how to do? Then give it to the PHP interpreter to handle it! But how does the PHP interpreter communicate with webserver?

In order to solve the communication between different language interpreters (such as PHP, Python interpreter) and webserver, a CGI protocol was present. As long as you follow the CGI protocol to write the program, you can implement the language interpreter and webserver communication. such as the PHP-CGI program.

The improvement of fast-cgi

With the CGI protocol, the PHP interpreter and webserver communication problems, Webserver finally can handle the dynamic language. However, every request received by Webserver will fork a CGI process and kill the process at the end of the request. With 10,000 requests, you need the fork, kill php-cgi process 10,000 times. Have you found a waste of resources? So, there was a modified version of CGI, fast-cgi.

Fast-cgi each time the request is processed, the process is not killed, but the process is preserved so that the process can process multiple requests at once. In this way, it is not necessary to fork a process again, which greatly improves the efficiency. FastCGI is a language-independent, extensible architecture for CGI open extensions whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. As we all know, the repeated loading of CGI interpreter is the main reason of poor CGI performance, if the CGI interpreter remains in memory and accepts the FASTCGI process manager scheduling, it can provide good performance, scalability, fail-over characteristics and so on.

2. What is PHP-FPM?

PHP-FPM is a PHP fastcgi manager that is used only for PHP. PHP-FPM is actually a patch of PHP source code designed to integrate FASTCGI process management into a PHP package. PHP-FPM provides a better way to manage PHP processes, effectively controlling memory and processes, and smoothing the overloading of PHP configurations.
The process contains both the master process and worker process processes.
The master process has only one, which is responsible for listening to the port, receiving requests from the Web Server, while the worker process is typically multiple (the exact number is configured according to actual needs), and each process embeds a PHP interpreter inside, which is where the PHP code really executes.

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.