2

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.

shortcuts

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是干什么的?CGI是为了保证web server传递过来的数据是标准个数的,方便CGI程序的编写者。

web server(比如说nginx)只是内容的分发者。比如,如果请求/index.html,那么web
server会去文件系统中找到这个文件,发送给浏览器,这里分发的是静态数据。好了,如果现在的请求是index.php,根据配置文件,nginx知道这个不是静态文件,需要去找php解析器来处理,那么他会把这个请求简单处理后交给php解析器。nginx会传哪些数据给php解析器呢?url要有吧,查询字符串也得有吧,POST数据也需要有,HTTP
header不能少吧,好的,CGI就是规定要传哪些数据,以什么样的格式传递给后方处理这个请求的协议。仔细想想,你再PHP代码中使用的用户是从哪里来的。
当web server收到/index.php
这个请求后,会启动对应的CGI程序,这里就是PHP的解析器。接下来PHP解析器会解析php.ini文件,初始化执行环境,然后处理请求,再以规定的CGI规定的格式返回处理后的结果,退出进程。web
server再把结果返回给浏览器。

明白了CGI是个协议,跟进程什么的没有关系。那fastcgi又是什么呢?Fasecgi是用来提高CGI程序性能的。

提高性能,那么CGI程序的性能问题在哪呢?“PHP解析器会解析php.ini文件,初始化执行环境”,就是这里了。标准的CGI对每个请求文件都会执行这些步骤(不嫌累啊!启动进程很累的说!),所以处理每个请求的时间会比较长。这明显不合理嘛!那么Fastcgi是怎么做的呢?首先,Fastcgi会先启动一个master,解析配置环境,初始化执行环境,然后再启动多个worker。当请求过来时,master会传递给一个worker,然后立即可以接受下一个请求。这样就避免了重复的劳动,效率自然是高。而且当worker不够用时,master可以根据配置预先启动几个worker等着;当然空闲worker太多时,也会停掉一些,这样就提高了性能,也节约了资源,这就是fastcgi对进程的管理。

那PHP-FPM又是什么呢?是一个实现了Fastcgi的程序,被PHP官方收了。

大家都知道,PHP的解释器是php-cgi。php-cgi只是个CGI程序,他自己本身只能解析请求,返回结果,不会进程管理(皇上,臣妾真的做不到啊!)所以就出现了一些能够调度php-cgi进程的程序,比如说由lighthttpd分离出来的spawn-fcgi。好了PHP-FPM也是这么个东东,在长时间的发展后,逐渐得到了大家的认可(要知道,前几年大家可是抱怨php-fpm稳定性太差的),也越来越流行。

好了,最后来回答你的问题。网上有的说,fastcgi是一个协议,php-fpm实现了这个协议。

有的说,php-fpm是fastcgi进程的管理器,用来管理fastcgi进程的

对。php-fpm的管理对象是php-cgi。但不能说php-fpm是fastcgi进程的管理器,因为前面说了fastcgi是个协议,似乎没有这么个进程存在,就算存在php-fpm也管理不了他(至少目前是)。

有的说,php-fpm是php内核的一个补丁

以前是对的。因为最开始的时候php-fpm没有包含在php内核里面,要使用这个功能,需要找到与源码版本相同的php-fpm对内核打补丁,然后再编译。后来php内核集成了php-fpm之后就方便多了,使用--enable-fpm这个编译参数即可。

有的说,修改了php.ini配置文件后,没办法平滑重启,所以就诞生了php-fpm

是的,修改php.ini之后,php-cgi进程的确没办法平滑重启的。php-fpm对此的处理机制是新的worker用新的配置,已经存在的worker处理完手上的活就可以歇着了,通过这种机制来平滑过度。

还有的说php-cgi是php自带的FastCGI管理器,那这样的话干吗又弄出个php-fpm出来
不对。php-cgi只是解释php脚本的程序而已。

如何让php更好的支持php-fpm

php-fpm提供了更好的php进程管理方式,可以有效的控制内存和进程,可以平滑重载php配置。在./configure的时候带-enable-fpm参数即可开启php-fpm。
修改nginx配置文件已支持php-fpm

nginx安装完场以后,修改nginx配置文件为nginx.conf
其中server段增加如下配置,否则会出现No input file specified.错误 pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

启动php-fpm和nginx

/usr/local/php/sbin/php-fpm

手动打补丁的启动方式

/usr/local/php/sbin/php-fpm start

sudo /usr/local/nginx/nginx

IT小子
15 声望0 粉丝