Detailed summary of PHP operating modes

藏色散人
Release: 2023-04-10 20:32:01
forward
4727 people have browsed it

PHP running mode

SAPI

The PHP running mode mentioned here actually refers to SAPI (Server Application Programming Interface, server application programming port). SAPI provides an interface for PHP to communicate with the outside world. PHP uses this interface to interact with other applications. For different application scenarios, PHP also provides a variety of different SAPIs. Common ones include: apache, apache2filter, apache2handler, cli, cgi, embed, fast-cgi, isapi, etc.

Detailed summary of PHP operating modes

php_sapi_name() — Returns the type of interface between the web server and PHP. Possible returned values ​​include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, cli-server, continuity, embed, fpm-fcgi, isapi, litespeed, milter, nsapi, phttpd , pi3web, roxen, thttpd, tux and webjames.

Many of the SAPI implementations built into PHP are no longer maintained or have become somewhat non-mainstream. The PHP community is currently considering moving some SAPIs out of the code base. The community considers many features to be in the PECL library unless they are really necessary, or some features are almost universal.

Next, five of the more common operating modes will be explained.

CLI mode

CLI (Command Line Interface), which is the command line interface, PHP will be installed by default. Through this interface, you can interact with PHP in a shell environment. Enter php -v in the terminal, you will get a result similar to the picture below (assuming PHP is installed):

Detailed summary of PHP operating modes

Because of the existence of CLI, we can run PHP scripts directly in the terminal command line, just like using shell or Python, without relying on the WEB server. For example, the Artisan command line tool in the Laravel framework is actually a PHP script used to help us quickly build Laravel applications.

CGI mode

CGI (Common Gateway Interface, Common Gateway Interface) is an important Internet technology that allows a client to go from a web browser to a program executing on a network server Request data. CGI describes a standard for transferring data between servers and request handlers.

WEB server is just a distributor of content. For example, Nginx, if the client requests /index.html, then Nginx will find this file in the file system and send it to the browser. What is distributed here is static data; if the client now requests /index.php, according to the configuration file, Nginx knows that this is not a static file and needs to be processed by a PHP parser, so it will hand the request to the PHP parser after simple processing. What data will Nginx pass to the PHP parser? The URL must be present, the query string must be present, the POST data must be present, and the HTTP request header must not be missing. Well, CGI is a protocol that stipulates what data is to be transmitted and in what format it is passed to the backend for processing the request. .

CGI mode operating principle: When Nginx receives the request from the browser /index.php, it will first create a process corresponding to the CGI protocol, here is php-cgi (PHP parser). Next, php-cgi will parse the php.ini file, initialize the execution environment, process the request, return the processed result in the format specified by CGI, and exit the process. Finally, Nginx returns the results to the browser. The entire process is a Fork-And-Execute pattern. When the number of user requests is very large, a large amount of system resources such as memory and CPU time will be occupied, resulting in low performance. Therefore, under a CGI server, there will be as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance.

Detailed summary of PHP operating modes

#The advantage of CGI mode is that it is completely independent of any server and only acts as an intermediary: providing interfaces to WEB servers and scripting languages ​​or completely independent programming languages. They are wired through the CGI protocol to complete data transfer. The advantage of this is to minimize the correlation between them, making each more independent and independent of each other.

CGI mode is already a relatively old mode and has rarely been used in recent years.

FastCGI mode

FastCGI (Fast Common Gateway Interface, Fast Common Gateway Interface) is a protocol that allows interactive programs to communicate with Web servers. FastCGI is an enhanced version of the earlier Common Gateway Interface (CGI). FastCGI is committed to reducing the interaction overhead between the web server and CGI programs, so that the server can handle more web page requests at the same time.

According to the definition, FastCGI is also a protocol. A program that implements the FastCGI protocol is more like a resident (long-live) CGI protocol program. As long as it is activated, it can always be executed. , it won’t take time to fork every time.

FastCGI mode operating principle: After the FastCGI process manager is started, it will first parse the php.ini file, initialize the execution environment, and then start multiple CGI protocol interpreter daemons (multiple CGI protocol interpreter daemons can be seen in the process management php-cig or php-cgi.exe) and wait for the connection from the WEB server; when the client request reaches the WEB server, the FastCGI process manager will select and connect to a CGI interpreter, and the WEB server will CGI environment variables and standard The input is sent to the sub-process php-cgi of FastCGI; after the php-cgi sub-process completes the processing, it returns the standard output and error information to the WEB server; at this time, the php-cgi sub-process will close the connection and the request will be processed. Then continue to wait and process the next request connection from the FastCGI process manager.

Detailed summary of PHP operating modes

FastCGI mode adopts a C/S structure, which can separate the WEB server and the script parsing server, and start one or more script parsing daemons on the script parsing server at the same time. Every time the WEB server encounters a dynamic program, it can be delivered directly to the FastCGI process for execution, and then the result is returned to the browser. This method allows the WEB server to exclusively process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

In addition, in CGI mode, after php-cgi changes the php.ini configuration, the php-cgi process needs to be restarted for the new php-ini configuration to take effect, and smooth restart is not possible. In FastCGI mode, PHP-FPM can achieve smooth restart after php.ini modification by generating a new child process.

PHP-FPM (PHP-FastCGI Process Manager) is a process manager that implements the FastCGI protocol in the PHP language. It was written and implemented by Andrei Nigmatulin. It has been officially included by PHP and integrated into the kernel.

Advantages of FastCGI mode:

  • From a stability point of view, FastCGI mode uses an independent process pool to run CGI protocol programs. If a single process dies, the system It can be easily discarded and then reassigned to a new process to run the logic;

  • From a security perspective, FastCGI mode supports distributed computing. The FastCGI program is completely independent from the host server. Even if the FastCGI program hangs up, it will not affect the server; The IO processing is still left to the host server, so that the host server can handle the IO wholeheartedly. For an ordinary dynamic web page, there may only be a small part of the logical processing, and a large number of static images and other images.

  • FastCGI mode is currently the mainstream WEB service operating mode of PHP. It has efficient and reliable performance and is recommended for everyone to use.

Module modePHP is often paired with the Apache server to form a LAMP supporting operating environment. Integrating PHP as a submodule into Apache is the Module mode. The common configuration in Apache is as follows:

LoadModule php5_module modules/mod_php5.so
Copy after login

This uses the

LoadModule

command. The first parameter of the command is the module The name can be found in the source code of the module implementation. The second option is the path where the module is located. If you need to load the module while the server is running, you can send the signal

HUP

or AP_SIG_GRACEFUL to the server. Once the signal is received, Apache will reload the module without restarting the server. By registering to the ap_hook_post_config hook of apache2, this module is started when Apache starts to accept requests for PHP files. For example, when the client accesses a PHP file, Apache will call php5_module to parse the PHP script. Every time Apache receives a request, it will create a process to connect to PHP to complete the request. In Module mode, sometimes PHP is compiled into Apache as a module, making it difficult to determine whether a problem occurs with PHP or Apache.

In the past, with its rich modules and functions, enterprises often used Apache as a WEB server, so the combination of PHP and Apache running in Module mode was very common. In recent years, with the rise of asynchronous event-driven, high-performance Nginx servers, the market share has grown rapidly. The PHP Nginx combination running in FastCGI mode has better performance and has a tendency to catch up with Apache.

ISAPI mode

ISAPI (Internet Server Application Program Interface) is a set of API interfaces for Internet services provided by Microsoft. An ISAPI DLL can reside in memory after being activated by a user request. , waiting for another request from the user, and multiple user request processing functions can be set in one DLL. In addition, the ISAPI DLL application and the WEB server are in the same process, and the efficiency is significantly higher than that of CGI. Due to Microsoft's exclusivity, it can only run in the Windows environment.

is rarely used and will not be introduced in detail here.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Detailed summary of PHP operating modes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!