Several operating modes in PHP - CSDN Blog

不言
Release: 2023-03-23 09:56:01
Original
4189 people have browsed it

We know that the workerman program needs to run in php-cli mode, which is the command line mode. We need to understand this.

It is said that PHP currently has 4 operating modes, namely CGI, FastCGI, CLI and Web module mode.

CGI

The full name is "Common Gateway Interface" (Common Gateway Interface), which allows a client to request data from a web browser to a program executing on a web server, as described It is a standard for transmitting data between the client and this program. In addition, CGI is independent of any language, so it can be written in any language, as long as the language has standard input, output and environment variables. Such as php, perl, tcl, etc.

CGI needs to open a separate sub-process for maintenance for each user request, so performance problems will occur when the number is large, and it has been rarely used in recent years.

FastCGI

An upgraded version of CGI, FastCGI is like a long-live CGI. It can be executed all the time. As long as it is activated, it will not cost every time. Time to parse php.ini, reload all dll extensions and reinitialize all data structures.

PHP uses PHP-FPM (FastCGI Process Manager), the full name of which is PHP FastCGI Process Manager, for management.

FastCGI working principle

  • The FastCGI process manager is loaded when the Web Server starts;

  • The FastCGI process manager initializes itself, starts multiple CGI interpreter processes and waits for connections from the Web Server;

  • When a client request reaches 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.

  • After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits for and handles the next connection from the FastCGI process manager.

  • In normal CGI mode, this is the end and you have to start over again next time. But in FastCGI, all this happens only once, when the process starts. An added bonus is that persistent database connections work.

Cli

PHP-CLI is the abbreviation of PHP Command Line Interface, which is the interface for PHP to run on the command line, which is different from the PHP environment running on the Web server ( PHP-CGI, etc.).

We often use "php -m" under Linux to find out which extensions PHP has installed, which is the PHP command line running mode. You can type php -h to see what the specific commands are.

In php-cli mode we can directly start a php file and execute it, just like in workererman

php index.php start
Copy after login

It should be noted that there is no statement about php running timeout in php-cli mode .

Module loading

is generally for apache. In this way, their common essence is to use LoadModule to load phpX_module, which is to run php as a sub-module of apache. When accessing a php file through the web, apache will call phpX_module to parse the php code. So how does phpX_module pass the data to the php parser to parse the php code? The answer is through sapi.

So, the above apache call php execution process is as follows:

apache -> httpd -> php5_module -> sapi -> php
Copy after login

Every time apache receives a request, it will generate a process to connect to php to complete the request through sapi. As you can imagine, if Once there are too many users and too many concurrent users, the server will be unable to bear it.

Moreover, when mod_php is compiled into apache, it is difficult to determine whether it is a problem with php or apache when a problem occurs.

Summary

If you want to build a high-performance PHP WEB server, the best way currently is the Apache/Nginx FastCGI PHP-FPM (PHP-CGI) method. Do not use Module loading anymore. Or CGI way

Related recommendations:

Detailed explanation of four PHP operating modes

Five major PHP operating modes

The above is the detailed content of Several operating modes in PHP - CSDN Blog. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template