我在网上查fastcgi与php-fpm的关系,查了快一周了,基本看了个遍,真是众说纷纭,没一个权威性的定义。
网上有的说,fastcgi是一个协议,php-fpm实现了这个协议; 有的说,php-fpm是fastcgi进程的管理器,用来管理fastcgi进程的; 有的说,php-fpm是php内核的一个补丁; 有的说,修改了php.ini配置文件后,没办法平滑重启,所以就诞生了php-fpm; 还有的说PHP-CGI是PHP自带的FastCGI管理器,那这样的话干吗又弄个php-fpm出来,我就更晕了;
发个贴,想听听大家的理解,网上的我都已经看了个遍,因为我查了一周了,哈哈,所以想听听原创的理解。
写的非常好 ,浅显易懂
CGI: Common Gateway Interface 通用网管协议
FastCGI: 一种CGI的实现方式 快
php-fpm: php FastCGI Process Manager(FastCGI进程管理器)
为何不去用C程序实现一个Web服务器的原型呢(简单的那种,但是能够帮助说明和理解原理)?我说认真的,并不难,实现一个Web服务器的原型出来,那时候你就会明白了。
https://aleen42.gitbooks.io/p...
CGI
CGI, Common Gateway Interface, is a tool for HTTP server to contact with programs on other servers, which can be used into any languages with standard input, standard output and environmental variables, such as PHP, Perl, or Tcl.
FastCGI
FastCGI is a kind of CGI which is long-live, which will always be running. With FastCGI, it'll take less time t fork(which is a problem of fork-and-execute mode in CGI). In additional, FastCGI also supports for distributed computing.
It is also not language related, which is an opened extension of CGI, which is used to keep CGI running in the memory. It's well-known that loading of CGI has been the main reason of low performance.
the main process of running FastCGI:
Loading the Process Manager of FastCGI when a Web server has booted(IIS ISAPI or Apache Module)
The Process Manager of FastCGI will initiate itself to create several CGI processes, which are used to wait for connection of Web servers.
When requests from clients have reached the Web server, the Process Manager of FastCGI will select a CGI set up before to connect, whose environmental variables and standard input will be sent to the sub process php-cgi of FastCGI.
This sub process will return standard output and error info to the Web server with the same connection. Requests will be finished when it closes the connection.
Therefore, FastCGI only set once for parsing php.ini, loading extensions and initiating all the data structures.
Because of multi-processes, FastCGI will cost more memory than CGI, whose each process(PHP-CGI) will cost about 7Mb to 25Mb memory.
Data from the article: Nginx 0.8x + PHP 5.2.13(FastCGI) is 10 times better than Apache(Edition 6)
when 30k connection happens in parallel, 10 Nginx processes will only cost 150Mb Mem(15Mb 10), and 64 PHP-CGI will only cost about 1280Mb(20Mb 64).
PHP-CGI
PHP-CGI is one kind of the Process Manager of FastCGI, which is within php itself.
The command to boot is as follow:
php-cgi -b 127.0.0.1:9000
shortcuts
After changing php.ini, you should reboot PHP-CGI to make the new php.ini work.
When a PHP-CGI process is killed, all the PHP code will cannot run.(PHP-FPM and Spawn-FCGI do not have the same problem)
PHP-FPM
PHP-FPM is another kind of the Process Manager of FastCGI, which can be downloaded here.
It's actually a patch for PHP, which is used to integrate the Process Manager of FastCGI into PHP, which should be make into PHP before version 5.3.2.
PHP-FPM can be used to control sub processes of PHP-CGI:
/usr/local/php/sbin/php-fpm [options]
# options
# --start: start a fastcgi process of php
# --stop: force to kill a fastcgi process of php
# --quit: smooth to kill a fastcgi process of php
# --restart: restart a fastcgi process of php
# --reload: smooth to reload php.ini
# --logrotate: enable log files again
Spawn-FCGI
Spawn-FCGI is a general kind of the Process Manager of FastCGI, which is one part of lighttpd.
CGI: 通信协议 server<->cgi<->php解析器
php-cgi实现了cgi,所以server<->php-cgi<->php解析器
fastcgi:比cgi更优秀的通信协议server<->fastcgi<->php解析器
php-fpm实现了fastcgi,所以server<->php-fpm<->php解析器
这下子应该明白了,php-fpm主打了worker管理功能,但是他还是fastcgi协议的一种实体。
何不看看这个呢?《CGI、FastCGI和PHP-FPM关系图解》:http://www.awaimai.com/371.html,解释得很清晰了。
补充前面大牛的回答,执行 man php-cgi:
我认为
php-cgi
是PHP解释器
+CGI规范
的实现。CGI规范: CGI规范定义了一系列环境变量。 https://www.zhihu.com/questio...
下面代码,用node承担web服务器的责任,解析 http header为环境变量后通过管道转发到 php-cgi:
PS:配置
所以综上所述: php-cgi = php + cgi 实现。
刚开始对这个问题我也挺纠结的,看了《HTTP权威指南》后,感觉清晰了不少。
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者。
好了,CGI是个协议,跟进程什么的没关系。那fastcgi又是什么呢?Fastcgi是用来提高CGI程序性能的。
那PHP-FPM又是什么呢?是一个实现了Fastcgi的程序,被PHP官方收了。
好了,最后来回来你的问题。
网上有的说,fastcgi是一个协议,php-fpm实现了这个协议
有的说,php-fpm是fastcgi进程的管理器,用来管理fastcgi进程的
有的说,修改了php.ini配置文件后,没办法平滑重启,所以就诞生了php-fpm
还有的说PHP-CGI是PHP自带的FastCGI管理器,那这样的话干吗又弄个php-fpm出
你(PHP)去和爱斯基摩人(web服务器,如 Apache、Nginx)谈生意
你说中文(PHP代码),他说爱斯基摩语(C代码),互相听不懂,怎么办?那就都把各自说的话转换成英语(FastCGI 协议)吧。
怎么转换呢?你就要使用一个翻译机(PHP-FPM)
(当然对方也有一个翻译机,那个是他自带的)
我们这个翻译机是最新型的,老式的那个(PHP-CGI)被淘汰了。不过它(PHP-FPM)只有年轻人(Linux系统)会用,老头子们(Windows系统)不会摆弄它,只好继续用老式的那个。
我看了上面三位的答案,但我总觉得他们描述得还不够贴切。
Fastcgi是CGI的升级版,一种语言无关的协议,用来沟通程序(如PHP, Python, Java)和Web服务器(Apache2, Nginx), 理论上任何语言编写的程序都可以通过Fastcgi来提供Web服务。
Fastcgi的特点是会在一个进程中依次完成多个请求,以达到提高效率的目的,大多数Fastcgi实现都会维护一个进程池。
而PHP-fpm就是针对于PHP的,Fastcgi的一种实现,他负责管理一个进程池,来处理来自Web服务器的请求。目前,PHP-fpm是内置于PHP的。
但是PHP-fpm仅仅是个“PHP Fastcgi 进程管理器”, 它仍会调用PHP解释器本身来处理请求,PHP解释器(在Windows下)就是php-cgi.exe.