What is the difference between CGI, FastCGI, Apache2handler, CLI? This article mainly introduces 4 kinds of common operation modes of PHP, and details the CGI, FastCGI, Apache2handler, CLI. We hope to help you.
Sapi:server application Programming Interface Service-side application programming port. He is the interface between PHP and other applications, PHP scripts to execute in a number of ways, through the Web server, or directly under the command line, can also be embedded in other programs. SAPI provides an interface to external communication, common SAPI are: CGI, fast-cgi, CLI, Apache module DLL, and so on.
1. CGI
CGI is a universal Gateway Interface (Common Gatewag interface), it is a program, the popular CGI is like a bridge, the Web page and the Web server to connect the execution program, it takes the HTML received instructions to the server's execution program, The results of the server execution program are then returned to the HTML page. CGI has excellent cross-platform performance and can be implemented on almost any operating system.
The CGI method encounters a connection request (user request) to create a CGI subprocess, activate a CGI process, then process the request, and end the subprocess after processing. This is the Fork-and-execute mode. So how many CGI sub-processes are there for the CGI server, and the sub-process loading is the main reason for the poor CGI performance. will be when the user requests a very long time, will be a lot of crowding out the system resources such as memory, CPU times, resulting in low performance.
2, FastCGI
FAST-CGI is an upgraded version of CGI, fastcgi is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it does not have to take a moment to fork it every time. PHP uses PHP-FPM (FastCGI process Manager), the full name of the PHP FastCGI process Manager to manage.
Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) when Web server starts. The FASTCGI process Manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) 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. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.
The FASTCGI child process returns standard output and error information from the same connection to the Web Server after processing is complete. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here.
In the above scenario, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all extensions, and initialize all data structures. With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.
3, Apache2handler
PHP as an Apache module, the Apache server after the system startup, pre-generated multiple copies of the process resides in memory, once a request appears, immediately use these spare child processes to process, so there is no generation of child process caused by the delay. These server replicas do not exit immediately after processing an HTTP request, but instead stay on the computer waiting for the next request. Requests for customer browsers respond faster and have higher performance.
4. CLI
CLI is the command-line run mode of PHP, you will often use it, but may not notice (for example: we often use "php-m" under Linux to find PHP installed those extensions is the PHP command line running mode;
Related recommendations:
What is the relationship between PHP fpm and fast-cgi? And there is also a question, that is, PHP running in CGI mode, what is the meaning of running CGI?
PHP fpm Installation: PHP PHP-FPM installation record
Tutorial _php tutorial on building PHP Apache MySQL environment in Windows and Linux environments