The following column of workerman usage tutorial will introduce you to several operating modes in PHP. I hope it will be helpful to friends in need!
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", which allows a client to connect from a web browser to a program running on a web server. The program requests data, which describes a standard for transmitting data between the client and the 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 It will take time to parse php.ini, reload all dll extensions and reinitialize all data structures every time.
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 and starts multiple CGI interpretations server process and waits for a connection from the Web Server;
When a client request arrives at 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 specific commands are available.
In php-cli mode we can directly start a php file and execute it, just like in workererman
php index.php start
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 use php as apache. A submodule to run. 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 process of apache calling php is as follows:
apache -> httpd -> php5_module -> sapi -> php
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 Apache/Nginx FastCGI PHP-FPM (PHP-CGI) method , don’t use Module loading or CGI method anymore
The above is the detailed content of workerman's talk about several operating modes in PHP. For more information, please follow other related articles on the PHP Chinese website!