Home  >  Article  >  Backend Development  >  How PHP works

How PHP works

远方*
远方*Original
2022-04-06 13:32:52209browse

1. The operating principle of PHP

Typical question: The operating principle of Nginx PHP - FPM
CGI: Some early Web Servers can only process simple HTML static files, but with the advancement of technology With the development, dynamic languages ​​(such as PHP, Python) appeared. In this, if we want to process PHP, we have to hand over a PHP parser for processing, but after PHP is processed, how to communicate with our Web Server? this is a problem. In order to solve the communication between different language processors and Web Server, the CGI protocol emerged. As long as the program is written according to the CGI protocol, the communication between the language parser and the Web Server can be realized. (For example: PHP's CGI program) In this process, CGI is a protocol bridge between the PHP parser and the Web Server.
FastCGI: Although CGI solves the problem of communication between PHP and Web Server, its efficiency is very low because every time the Web Server receives a request, it will open a new CGI process, and then terminate this process when the request ends. Processes, if we have 10,000, 100,000, or 1 million such requests at this time, we will open 100,000 or 1 million new processes, and then terminate them. In itself, , a huge waste of our resources. At this time, FastCGI appeared. It mainly appeared as an improved version of CGI. After each request is processed, the process will not be terminated, but the process will be retained so that the process can handle multiple requests at one time. In this case, There is no need to reopen a process every time, which greatly improves our efficiency.
PHP-FPM: (FastCGI Process Manager: FastCGI process manager), FPM is an implementation of FastCGI and provides process management functions. The process includes two processes: master process and worker process. There is only one master process, which is responsible for listening to the port and receiving requests from the Web Server. There are generally multiple worker processes, and the specific number will be defined in the FPM configuration. Each process A PHP parser is embedded inside each process (that is where the PHP code is actually executed). In other words, the worker processes the PHP code, while the master mainly listens on the port and receives requests from the Web Server. In itself, the master listening port is 9000 by default, and the 9000 port is proxied through Nginx's reverse proxy, so here we can complete FPM related processing.

2. Briefly describe the differences between CGI, FastCGI and PHP-FPM.

CGI: In order to contact a protocol in the Web Server and the PHP parser, it acts as a bridge.
FastCGI: It is an improved version of CGI.
PHP-FPM: FastCGI Process Manager, FastCGI process manager.

The above is the detailed content of How PHP works. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn