Home > Backend Development > PHP Tutorial > Detailed explanation of the operating modes of various web servers based on PHP

Detailed explanation of the operating modes of various web servers based on PHP

WBOY
Release: 2016-08-08 09:29:32
Original
913 people have browsed it

from:http://www.jb51.net/article/37759.htm

1. PHP running mode in apache

php has three working modes in apache: CGI mode, FastCGI mode, Apache module DLL
Compare the following:
1. Comparison between CGI mode and module mode:
The difference between the two working methods of php in apache (CGI mode, Apache module DLL)
Installation of these two working methods:
PHP’s CGI approach 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 module mode in Apache 2.0

Use these two lines for PHP 4:

LoadModule php4_module “c:/php/php4apache2.dll”

Don’t forget to copy php4apache2.dll from the sapi directory!

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”
The difference between these two 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 interpretation result to the client in the form of a web page;
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 CGI mode.
2. FastCGI operating mode analysis:
How FastCGI works is:
(1) Load the FastCGI process manager when the Web Server starts [PHP’s FastCGI process manager is PHP-FPM (php-FastCGI Process Manager)] (IIS ISAPI or Apache Module);
(2) The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi.exe can be seen in the task manager) and waits 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.

  1. Why use FastCGI instead of a multi-threaded CGI interpreter?
    This may be due to many considerations, such as:
    (1) You cannot stably use a multi-threaded CGI interpreter on the Windows platform anyway. Whether it is the IIS ISAPI method 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. The probability of such errors will be greatly increased when using FastCGI. of reduction. 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.

  2. Advantages of running PHP in FastCGI mode:
    There are several major benefits to 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.
    Copy the code The code is as follows:

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

使用上面的配置,访问 http://localhost/ 就使用 PHP5,而访问 http://localhost:8080/ 就使用 PHP4。所以只要合理配置,就可以让不同的虚拟主机使用不同版本的 PHP。
FastCGI 模式的一些缺点:
说完了好处,也来说说缺点。从我的实际使用来看,用 FastCGI 模式更适合生产环境的服务器。但对于开发用机器来说就不太合适。因为当使用 Zend Studio 调试程序时,由于 FastCGI 会认为 PHP 进程超时,从而在页面返回 500 错误。这一点让人非常恼火,所以我在开发机器上还是换回了 ISAPI 模式。
最后,在 Windows 中以 FastCGI 模式存在潜在的安

二、php 在nginx 中运行模式(nginx+PHP-FPM )目前理想选择

使用FastCGI方式现在常见的有两种stack:ligthttpd+spawn-fcgi; 另外一种是nginx+PHP-FPM(也可以用spawn-fcgi) 。
(1) 如上面所说该两种结构都采用FastCGI对PHP支持,因此HTTPServer完全解放出来,可以更好地进行响应和并发处理。因此lighttpd和nginx都有small, but powerful和efficient的美誉。

(2) 该两者还可以分出一个好坏来,spawn-fcgi由于是lighttpd的一部分,因此安装了lighttpd一般就会使用spawn-fcgi对php支持,但是目前有用户说ligttpd的spwan-fcgi在高并发访问的时候,会出现上面说的内存泄漏甚至自动重启fastcgi。即:PHP脚本处理器当机,这个时候如果用户访问的话,可能就会出现白页(即PHP不能被解析或者出错)。

另一个:首先nginx不像lighttpd本身含带了fastcgi(spawn-fcgi),因此它完全是轻量级的,必须借助第三方的FastCGI处理器才可以对PHP进行解析,因此其实这样看来nginx是非常灵活的,它可以和任何第三方提供解析的处理器实现连接从而实现对PHP的解析(在nginx.conf中很容易设置)。

nginx可以使用spwan-fcgi(需要一同安装lighttpd,但是需要为nginx避开端口,一些较早的blog有这方面安装的教程),但是由于spawn-fcgi具有上面所述的用户逐渐发现的缺陷,现在慢慢减少使用nginx+spawn-fcgi组合了。

c. 由于spawn-fcgi的缺陷,现在出现了新的第三方(目前还是,听说正在努力不久将来加入到PHP core中)的PHP的FastCGI处理器,叫做PHP-FPM(具体可以google)。它和spawn-fcgi比较起来有如下优点:

由于它是作为PHP的patch补丁来开发的,安装的时候需要和php源码一起编译,也就是说编译到php core中了,因此在性能方面要优秀一些;
同时它在处理高并发方面也优于spawn-fcgi,至少不会自动重启fastcgi处理器。具体采用的算法和设计可以google了解。

因此,如上所说由于nginx的轻量和灵活性,因此目前性能优越,越来越多人逐渐使用这个组合:nginx+PHP/PHP-FPM
三、IIS+ ISAPI模式这种模式适合开发环境中, 生产环境中用的较少。

四、总结
目前在HTTPServer这块基本可以看到有三种stack比较流行:
(1)Apache+mod_php5
(2)lighttp+spawn-fcgi
(3)nginx+PHP-FPM
三者后两者性能可能稍优,但是Apache由于有丰富的模块和功能,目前来说仍旧是老大。有人测试nginx+PHP-FPM在高并发情况下可能会达到Apache+mod_php5的5~10倍,现在nginx+PHP-FPM使用的人越来越多。

以上就介绍了基于php在各种web服务器的运行模式详解,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template