Home >Backend Development >PHP Tutorial >4 common ways of running php_PHP tutorial

4 common ways of running php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:01:19925browse

Four common ways of running PHP

This article mainly introduces the four common ways of running PHP. This article explains the four common ways of running CGI, FastCGI, APACHE2HANDLER, and CLI. Method, friends in need can refer to it

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 a 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 the web page and the execution program in the WEB server. It passes the instructions received by HTML to the server. execute the program, and then return the result of the server 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. It 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 Apache server starts the system, it pre-generates multiple process copies to reside in the memory. Once a request appears, these spare sub-processes are immediately used for processing, so that there is no need to generate sub-processes. caused a delay. 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 "php -m" under Linux to find out which extensions PHP has installed, which is the PHP command line running mode. ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/971950.htmlTechArticle4 common ways of running php. This article mainly introduces the 4 common ways of running php. This article explains CGI, FastCGI, APACHE2HANDLER, CLI and other 4 operating modes, friends in need can refer to...
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