Detailed explanation of PHP's CGI, FastCGI, APACHE2HANDLER, and CLI operating modes

*文
Release: 2023-03-18 13:42:01
Original
3822 people have browsed it

What are the differences between CGI, FastCGI, APACHE2HANDLER and CLI? This article mainly introduces the four common running methods of PHP, and explains CGI, FastCGI, APACHE2HANDLER, and CLI in detail. I hope to be helpful.

SAPI: Server Application Programming Interface server application programming port. It is the interface for PHP to interact with other applications. PHP scripts can be executed in many ways, through the web server, directly on the command line, or embedded in other programs. SAPI provides an interface for external communication. Common SAPIs include: cgi, fast-cgi, cli, Apache module dll, etc.

1. CGI

CGI is the common gateway interface. It is a program. In layman’s terms, CGI is like a bridge that connects web pages and The execution program in the WEB server is connected, it passes the instructions received by the HTML to the server's execution program, and then returns the results of the server's execution program to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system.

When the CGI method encounters a connection request (user request), it must first create a cgi sub-process, activate a CGI process, then process the request, and end the sub-process after processing. This is the fork-and-execute pattern. Therefore, a server using CGI will have as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be occupied, resulting in low performance.

2. FastCGI

fast-cgi is an upgraded version of cgi. FastCGI is like a long-live CGI that can be executed all the time. , as long as it is activated, it will not take time to fork every time. PHP uses PHP-FPM (FastCGI Process Manager), the full name of PHP FastCGI Process Manager, for management.

Load the FastCGI process manager (IIS ISAPI or Apache Module) when the Web Server starts. The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (visible multiple php-cgi) 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 (running in the Web Server). In CGI mode, php-cgi exits at this point.

In the above case, you can imagine how slow CGI usually is. Every web request to PHP must reparse php.ini, reload all extensions, and reinitialize all data structures. With FastCGI, all of this happens only once, when the process starts. An added benefit is that persistent database connections work.

3. APACHE2HANDLER
PHP is an Apache module. After the system starts, the Apache server pre-generates multiple process copies to reside in the memory. Once a request appears, it will be used immediately. These idle sub-processes are processed so that there is no delay caused by spawning sub-processes. These server copies do not exit immediately after processing an HTTP request, but stay in the computer waiting for the next request. The response to client browser requests is faster and the performance is higher.

4. CLI

cli is the command line running mode of PHP. You often use it, but you may not notice it (for example: we often use it under Linux) Use "php -m" to find out which extensions PHP has installed, which is the PHP command line running mode;

Related recommendations:

php fpm and fast -What does CGI have to do with it? And there is another question, that is, PHP runs in CGI mode. What does it mean to run in CGI mode?

##php fpm? Installation: php php-fpm installation record

How to build php apache mysql environment under windows and Linux environments_PHP tutorial

The above is the detailed content of Detailed explanation of PHP's CGI, FastCGI, APACHE2HANDLER, and CLI operating modes. 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
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!