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.
复制代码 代码如下:
LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP13.dll
ScriptAlias /fcgi-php5/ "d:/usr/local/php-5.0.4/"
FastCgiServer "d:/usr/local/php-5.0.4/php-cgi.exe" -processes 3
ScriptAlias /fcgi-php4/ "d:/usr/local/php-4.3.11/"
FastCgiServer "d:/usr/local/php-4.3.11/php.exe"
Listen 80
NameVirtualHost *:80
DocumentRoot d:/www
Options Indexes FollowSymlinks MultiViews
ServerName php5.localhost
AddType application/x-httpd-fastphp5 .php
Action application/x-httpd-fastphp5 "/fcgi-php5/php-cgi.exe"
IndexOptions FancyIndexing FoldersFirst
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
Listen 8080
NameVirtualHost *:8080
DocumentRoot d:/www
Options Indexes FollowSymlinks MultiViews
ServerName php4.localhost
AddType application/x-httpd-fastphp4 .php
Action application/x-httpd-fastphp4 "/fcgi-php4/php.exe"
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
Using the above configuration, accessing http://localhost/ uses PHP5, and accessing http://localhost:8080/ uses PHP4. So as long as it is properly configured, different virtual hosts can use different versions of PHP.
Some disadvantages of FastCGI mode:
After talking about the advantages, let’s talk about the disadvantages. From my actual use, FastCGI mode is more suitable for servers in production environments. But it is not suitable for development machines. Because when using Zend Studio to debug the program, FastCGI will think that the PHP process has timed out and return a 500 error on the page. This was so annoying that I switched back to ISAPI mode on my development machine.
Finally, there are potential security issues in FastCGI mode in Windows
2. PHP running mode in nginx (nginx+PHP-FPM) is currently the ideal choice
There are two common stacks using FastCGI: lighthttpd+spawn-fcgi; the other is nginx+PHP-FPM (spawn-fcgi can also be used).
(1) As mentioned above, both structures use FastCGI to support PHP, so HTTPServer is completely liberated and can respond better and handle concurrently. Therefore, both lighttpd and nginx have the reputation of being small, but powerful and efficient.
(2) The two can also be divided into good and bad. Since spawn-fcgi is part of lighttpd, if lighttpd is installed, spawn-fcgi will generally be used to support PHP. However, some users currently say When ligttpd's spwan-fcgi has high concurrent access, the memory leak mentioned above will occur and even fastcgi will automatically restart. That is: the PHP script processor crashes. If the user accesses it at this time, a white page may appear (that is, PHP cannot be parsed or an error occurs).
Another: First of all, nginx does not include fastcgi (spawn-fcgi) like lighttpd itself, so it is completely lightweight and must use a third-party FastCGI processor to parse PHP, so In fact, it seems that nginx is very flexible. It can connect to any third-party parsing processor to realize the parsing of PHP (it is easy to set up in nginx.conf).
nginx can use spwan-fcgi (lighttpd needs to be installed together, but the port needs to be avoided for nginx. Some older blogs have tutorials on this installation), but since spawn-fcgi has the users mentioned above The defects gradually discovered, and now the use of nginx+spawn-fcgi combination is gradually reduced.
c. Due to the defects of spawn-fcgi, a new third-party (currently still, I heard that they are working hard to add it to PHP core in the near future) FastCGI processor for PHP has appeared, called PHP-FPM ( You can google for details). Compared with spawn-fcgi, it has the following advantages:
Since it is developed as a PHP patch, it needs to be compiled together with the PHP source code during installation, which means that it is compiled into PHP core, so It is better in terms of performance;
At the same time, it is better than spawn-fcgi in handling high concurrency, at least it will not automatically restart the fastcgi processor. The specific algorithms and designs used can be found on Google.
Therefore, as mentioned above, due to the lightweight and flexibility of nginx, its current performance is superior, and more and more people are gradually using this combination: nginx+PHP/PHP-FPM
3. IIS+ ISAPI mode This mode is suitable for development environments and is rarely used in production environments.
4. Summary
At present, there are basically three popular stacks in HTTPServer:
(1) Apache+ mod_php5
(2) lighttp+spawn-fcgi
(3) nginx+PHP-FPM
The performance of the last two of the three may be slightly better, but due to Apache’s rich modules and functions, currently He is still the boss. Some people have tested that nginx+PHP-FPM may reach 5 to 10 times that of Apache+mod_php5 under high concurrency conditions. Now more and more people are using nginx+PHP-FPM.
http://www.bkjia.com/PHPjc/327257.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327257.htmlTechArticle1. PHP running mode in apache PHP has three working modes in apache: CGI mode, FastCGI mode , Apache module DLL are compared respectively below: 1. Comparison between CGI mode and module mode:...