The knowledge about FastCGI and mod_php on the Internet is relatively messy and not comprehensive, so I have compiled it here for the convenience of beginners.
Background
The most common way for PHP is to run in Apache as a module (mod_php), which is also the default way for Apache to run PHP; but in Nginx, Nginx uses PHP-FPM, but what exactly is PHP-FPM? Dongdong? What does it have to do with php? Let’s explore it together today.
PHP handlers
The first thing to remember is that any kind of web server (Apache, Nginx, etc.) is designed to send static resources such as HTML and pictures to users. The web server itself cannot interpret any dynamic scripts (PHP, Python, etc.) wait).
The PHP processor is used to interpret the PHP code in the web application, interpret it as HTML or other static resources, and then pass the parsed results to the web server, and finally the web server sends it to the user.
Most web servers cannot parse PHP code, so it needs a program that can parse PHP code, which is a PHP processor.
Now we know that both Apache and Nginx require PHP processor to process PHP code, so how to connect the server and PHP processor? In other words, how does the server communicate with the php processor?
The answer is through SAPIServer Application Programming Interface (Server-side Application Programming Interface). Simply put, SAPI refers to the programming interface for specific PHP applications. Just like a PC, no matter which operating system is installed, as long as it meets the interface specifications of the PC All can run normally on a PC. There are many ways to execute PHP scripts, through a web server, or directly under the command line, or embedded in other programs. If you are interested, you can study the PHP kernel.
Here we continue to discuss the two connection methods provided by PHP’s most commonly used SAPI: mod_php and mod_fastcgi.
mod_php mode
Let’s review, how can Apache identify PHP code? Is it necessary to add or modify the following sentences in Apache’s configuration file httpd.conf:
<ol class="linenums"><li class="L0"><p><code><span class="com">//添加</span></code></p></li><li class="L1"><p><code><span class="typ">LoadModule</span><span class="pln"> php5_module modules</span><span class="pun">/</span><span class="pln">libphp5</span><span class="pun">.</span><span class="pln">so</span></code></p></li><li class="L2"><p><code><span class="typ">AddType</span><span class="pln"> application</span><span class="pun">/</span><span class="pln">x</span><span class="pun">-</span><span class="pln">httpd</span><span class="pun">-</span><span class="pln">php </span><span class="pun">.</span><span class="pln">php</span></code></p></li><li class="L3"><p><code><span class="com">//修改</span></code></p></li><li class="L4"><p><code><span class="pun"><</span><span class="typ">IfModule</span><span class="pln"> dir_module</span><span class="pun">></span></code></p></li><li class="L5"><p><code><span class="typ">DirectoryIndex</span><span class="pln"> index</span><span class="pun">.</span><span class="pln">php index</span><span class="pun">.</span><span class="pln">html index</span><span class="pun">.</span><span class="pln">htm index</span><span class="pun">.</span><span class="pln">html</span></code></p></li><li class="L6"><p><code><span class="pun"></</span><span class="typ">IfModule</span><span class="pun">></span></code></p></li></ol>
That is, php runs as a sub-module of Apache. When a php file is accessed through the web, Apache will call php5_module to parse the php code.
After configuring to load the mod_php module, php is part of the Apahce process itself, and each new Apache child process will load this module.
mod_fastcgi mode
Let’s first look at the instructions on the PHP-FPM official website:
PHP-FPM - A simple and robust FastCGI Process Manager for PHP
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.
PHP-FPM is a FastCGI process manager for PHP, which is explained very simply. This shows that PHP-FPM assists mod_fastcgi mode to work, but what is FastCGI? What processes are managed?
What is CGI?
CGI (Common Gateway Interface) is one of the most important technologies in WWW technology and has an irreplaceable important position.
CGI is an interface standard between external applications (CGI programs) and Web servers. It is a procedure for transferring information between CGI programs and Web servers.
The CGI specification allows Web servers to execute external programs and send their output to Web browsers. CGI turns the Web's set of simple static hypermedia documents into a complete new interactive media.
To put it bluntly, CGI is a protocol between an external application (CGI program) and the web server. CGI is to ensure that the data passed by the server is in a standard format.
What is FastCGI?
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 (this is the most criticized fork-and of CGI). -execute mode). It also supports distributed computing, that is, FastCGI programs can be executed on hosts other than the website server and accept requests from other website servers.
FastCGI is a language-independent, scalable architecture CGI open extension. Its main behavior is to keep the CGI interpreter process in memory and thus obtain higher performance. As we all know, repeated loading of the CGI interpreter is the main reason for low CGI performance. If the CGI interpreter remains in memory and accepts FastCGI process manager scheduling, it can provide good performance, scalability, Fail-Over features, etc.
Generally, the entire workflow of FastCGI is like this:
In other words, FastCGI is an upgraded version of CGI, a language-independent protocol used to communicate between programs (such as PHP, Python, Java) and web servers (Apache2, Nginx). In theory, programs written in any language can be Provide Web services through FastCGI.
The characteristic of FastCGI is that it will complete multiple requests in one process in order to improve efficiency. Most FastCGI implementations will maintain a process pool.
Popular explanation: FastCGI needs to be started in advance, and multiple CGI modules can be started. It runs there and waits for the web to send requests, and then parses and operates PHP. After completion, it generates html and returns it to the web, but after completion it It will not exit, but will continue to wait for the next web request.
PHP-FPM
PHP-FPM is an implementation of FastCGI for PHP. It is responsible for managing a process pool to handle requests from the web server.
But PHP-FPM is just a "PHP FastCGI Process Manager", it will still call the PHP interpreter itself to process the request. The PHP interpreter (under Windows) is php-cgi.exe.
Conclusion
Having said so much, I don’t know if I have expressed it clearly. Please correct me if there is anything incorrect. Thank you.
Original text: http://article.gitos.cn/2015/Aurthur/PHP-Mod-PHP-And-Fast-CGI-Explain.html Author: Aurthur