Home >Backend Development >PHP Tutorial >Detailed explanation of the operating modes of various web servers based on PHP_PHP tutorial
1. PHP running mode in apache
php has three working modes in apache: CGI mode, FastCGI mode, Apache Module DLL
Compare the following respectively:
1. Comparison between CGI mode and module mode:
Two ways php works in apache The difference (CGI mode, Apache module DLL)
Installation of these two working methods:
PHP CGI mode in Apache 2.0
ScriptAlias /php / "c:/php/"
AddType application/x-httpd-php .php
# For PHP 4 use this line
Action application/x-httpd-php "/ php/php.exe"
# For PHP 5 use this line
Action application/x-httpd-php "/php/php-cgi.exe"
PHP in Apache 2.0 Module mode in
# For PHP 4, use these two lines:
LoadModule php4_module "c:/php/php4apache2.dll"
# Don’t forget to download it from the sapi directory Copy php4apache2.dll out!
AddType application/x-httpd-php .php
# For PHP 5 use these two lines:
LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php
# Configure the path of php.ini
PHPIniDir "C:/php"
Both of these The difference in working methods:
In CGI mode, if the client requests a php file, the web server calls php.exe to interpret the file, and then returns the result of the interpretation in the form of a web page To the client;
And in modular (DLL), PHP is started and run together with the web server.
So from a certain perspective, PHP4 installed in the apache module mode has better security and better execution efficiency and speed than the CGI mode.
2. Analysis of FastCGI operating mode:
The working principle of FastCGI is:
(1) Load the FastCGI process manager [PHP] when the Web Server starts The FastCGI process manager is PHP-FPM (php-FastCGI Process Manager)] (IIS ISAPI or Apache Module);
(2), FastCGI process manager initializes itself and starts multiple CGI interpreter processes (in task management There are multiple php-cgi.exe visible in the server) and waiting for the connection from the Web Server.
(3). When the 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.exe.
(4). 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 WebServer). In normal CGI mode, php-cgi.exe exits here.
In the above case, you can imagine how slow CGI usually is. Every web request to PHP must re-parse php.ini, reload all dll extensions and re-initialize all data structures. With FastCGI, all of this happens only once, when the process starts. An added bonus is that persistent database connections work.
3. Why use FastCGI instead of a multi-threaded CGI interpreter?
This may be due to various considerations, such as:
(1). You cannot stably use a multi-threaded CGI interpreter on the Windows platform anyway, whether it is IIS ISAPI mode Or the APACHE Module method, they always crash after running for a while. Is it strange? But such a situation does exist!
Of course, there are many times when you can use a multi-threaded CGI interpreter stably. However, you may find that the web page sometimes has errors, and you can't find the reason anyway. This error occurs when you switch to FastCGI. The probability will be greatly reduced. I don't know why this is, I think the CGI interpreter with independent address space may be a little more stable than the shared address space form after all.
(2), performance! performance? Is it possible, is FastCGI faster than a multi-threaded CGI interpreter? But sometimes it is true, and you can only make a final conclusion by testing your website. The reason, I think it's hard to say, but there is information that in the era of Zend WinEnabler, Zend originally recommended using FastCGI instead of IIS ISAPI or Apache Module under the Windows platform, but now Zend no longer makes this product.
4. Advantages of running PHP in FastCGI mode:
There are several major benefits of running PHP in FastCGI mode. The first is that when PHP goes wrong, it will not bring down Apache, but PHP's own process will crash (but FastCGI will immediately restart a new PHP process to replace the crashed process). Secondly, the performance of running PHP in FastCGI mode is better than that of ISAPI mode (I originally used ApacheBench to test, but forgot to save the results. If you are interested, you can test it yourself).
Finally, you can run PHP5 and PHP4 at the same time. Referring to the configuration file below, two virtual hosts were created, one using PHP5 and the other using PHP4.