The basis of the difference between PHP CGI, FastCGI, PHP-CGI and PHP-FPM of

CGI

CGI stands for "Common Gateway Interface" (Common Gateway Interface), HTTP server for a tool to "talk" with the program on your or other machines, the program must be run on a network server.

CGI can be written in any language as long as such a standard language input, output and environmental variables. As php, perl, tcl like.

FastCGI

FastCGI like a permanent (long-live) type of CGI, it can always perform with, as long as the activation, will not always have to take the time to fork once (this is the most criticized CGI fork-and-execute mode ). It also supports distributed computing, namely FastCGI program can be executed on the host server and site other than to accept requests from the server to the other sites.

FastCGI is a language-independent, scalable, open extension framework CGI, the main behavior is to keep the CGI interpreter process in memory and therefore achieve higher performance. As we all know, CGI interpreter repeated loading is the main reason for poor performance CGI, CGI interpreter if kept in memory and accept scheduling FastCGI process manager, can provide good performance, scalability, Fail- Over characteristics and so on.

1, FastCGI Features

FastCGI has a language-independent.

FastCGI application process, independent of the core web server is running, providing a more secure environment than the API. APIs to link together the core code of the application's web server, which means that a wrong API applications may cause damage to other applications or core server. API malicious application code can even steal another key or core application server.

FastCGI technology is currently supported languages: C / C ++, Java, Perl, Tcl, Python, SmallTalk, Ruby and so on. Related modules on Apache, ISS, Lighttpd and other popular server is also available.

FastCGI does not depend on the internal structure of any Web server, so even if the changes server technology, FastCGI remained stable.

FastCGI works

Loading FastCGI Process Manager (IIS ISAPI or Apache Module) start Web Server

FastCGI process manager initializes itself, to start multiple CGI interpreter process (shows multiple php-cgi) and waits for a connection from the Web Server.

When a client request arrives Web Server, FastCGI Process Manager to select and connect to a CGI interpreter. Web server sends to the child FastCGI php-cgi CGI environment variables and the standard input.

FastCGI child process to complete the process after the standard output and error messages from the same connection returns Web Server. When the child FastCGI process closes the connection request will come to complete the process. FastCGI child process and then wait for treatment from under FastCGI Process Manager (running to Web Server) is a connection. In CGI mode, php-cgi this will quit.

In these cases, you can imagine how CGI is usually slow. Each Web request must be re-parse PHP php.ini, reload all extensions and try to initialize the entire data structure. Use FastCGI, all of which occur only once when the process started. An additional benefit is that persistent database connections (Persistent database connection) can work.

FastCGI deficiencies

Because it is a multi-process, multi-threaded so than CGI consumes more server memory, PHP-CGI interpreter process consumes every 7-25 megabytes of memory, this figure is multiplied by 50 or 100 it is a large amount of memory.

Nginx 0.8.46 + PHP 5.2.14 (FastCGI) server at 30,000 concurrent connections, open 10 Nginx process consumes memory 150M (15M * 10 = 150M), open 64 php-cgi process consumes memory 1280M (20M * 64 = 1280M), coupled with the system's own memory consumption, total consumption of less than 2GB of memory. If the server memory is small, you can only open 25 php-cgi process, so that the total amount of memory consumed by the php-cgi only 500M.

The above data is extracted from Nginx 0.8.x + PHP 5.2.13 (FastCGI) to build ten times better than the Apache Web server

PHP-CGI

PHP-CGI是PHP自带的FastCGI管理器。

PHP-CGI的不足

php-cgi变更php.ini配置后需重启php-cgi才能让新的php-ini生效,不可以平滑重启。

直接杀死php-cgi进程,php就不能运行了。(PHP-FPM和Spawn-FCGI就没有这个问题,守护进程会平滑从新生成新的子进程。)

PHP-FPM

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到。

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

现在我们可以在PHP 5.3.2的源码树里下载得到直接整合了PHP-FPM的分支,据说下个版本会融合进PHP的主分支去。相对Spawn-FCGI,PHP-FPM在CPU和内存方面的控制都更胜一筹,而且前者很容易崩溃,必须用crontab进行监控,而PHP-FPM则没有这种烦恼。

PHP5.3.3已经集成php-fpm了,不再是第三方的包了。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多有点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM。

Spawn-FCGI

Spawn-FCGI是一个通用的FastCGI管理服务器,它是lighttpd中的一部份,很多人都用Lighttpd的Spawn-FCGI进行FastCGI模式下的管理工作,不过有不少缺点。而PHP-FPM的出现多少缓解了一些问题,但PHP-FPM有个缺点就是要重新编译,这对于一些已经运行的环境可能有不小的风险(refer),在php 5.3.3中可以直接使用PHP-FPM了。

Spawn-FCGI目前已经独成为一个项目,更加稳定一些,也给很多Web 站点的配置带来便利。已经有不少站点将它与nginx搭配来解决动态网页。

最新的lighttpd也没有包含这一块了(http://www.lighttpd.net/search?q=Spawn-FCGI),但可以在以前版本中找到它。在lighttpd-1.4.15版本中就包含了(http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz),目前Spawn-FCGI的下载地址是http://redmine.lighttpd.net/projects/spawn-fcgi

注:最新的Spawn-FCGI可以到lighttpd.net网站搜索“Spawn-FCGI”找到它的最新版本发布地址。

PHP-FPM与spawn-CGI对比

PHP-FPM的使用非常方便,配置都是在PHP-FPM.ini的文件内,而启动、重启都可以从php/sbin/PHP-FPM中进行。更方便的是修改php.ini后可以直接使用PHP-FPM reload进行加载,无需杀掉进程就可以完成php.ini的修改加载

结果显示使用PHP-FPM可以使php有不小的性能提升。PHP-FPM控制的进程cpu回收的速度比较慢,内存分配的很均匀。

Spawn-FCGI控制的进程CPU下降的很快,而内存分配的比较不均匀。有很多进程似乎未分配到,而另外一些却占用很高。可能是由于进程任务分配的不均匀导致的。而这也导致了总体响应速度的下降。而PHP-FPM合理的分配,导致总体响应的提到以及任务的平均。

Guess you like

Origin blog.csdn.net/mybluesky1983/article/details/51711403