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.php
the main process of running FastCGI:html
php-cgi
of FastCGI.php.ini
, loading extensions and initiating all the data structures.
- 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 -b 127.0.0.1:9000
php.ini
, you should reboot PHP-CGI to make the new php.ini
work./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
CGI(Common Gateway Interface)nginx
最初,CGI 是在 1993 年由美国国家超级电脑应用中心(NCSA)为 NCSA HTTPd Web 服务器开发的。程序员
这个 Web 服务器使用了 UNIX shell 环境变量 来保存从 Web 服务器传递出去的参数,而后生成一个运行 CGI 的独立进程。CGI的第一个实现是 Perl 写的[1]。正则表达式
正由于这些问题,在CGI诞生后的很长一段时间,各类Web Server都仍是采用API这种强绑定的方式去支持Web开发,其中Apache的mod_php就属于这种方式。因此后面就有大神提出了FastCGI标准。shell
FastCGI(Fast Common Gateway Interface)编程
FastCGI使用进程/线程池来处理一连串的请求。这些进程/线程由FastCGI服务器管理,而不是Web服务器。 当进来一个请求时,Web服务器把环境变量和这个页面请求经过一个Socket长链接传递给FastCGI进程。因此FastCGI有以下的优势:后端
void main(void)
{
int count = 0;
while(FCGI_Accept() >= 0) {
printf(“Content-type: text/html\r\n”);
printf(“\r\n”);
printf(“Hello world!\r\n”);
printf(“Request number %d.”, count++);
}
exit(0);
}
因此FastCGI一推出就几乎得到了全部主流Web Server的支持,Apache、Lighttpd、IIS、Cherokee……bash
题主说的php-fpm就是一种FastCGI的后端实现。服务器
But,事情老是还有改进的余地的,FastCGI这套工做模式实际上没有什么太大缺陷,可是有些不安分的Python程序猿以为,FastCGI标准下写异步的Web服务仍是不太方便,若是可以收到请求后CGI端去处理,处理完毕后经过Callback回调来返回结果,那样岂不是很Coooool?!因此WSGI就被创造出来了:
WSGI(Web Server Gateway Interface)
Web服务器网关接口(Web Server Gateway Interface,缩写为WSGI)是为Python语言定义的Web服务器和Web应用程序或框架之间的一种简单而通用的接口。
当Web Server收到一个请求后,能够经过Socket把环境变量和一个Callback回调函数传给后端Web应用,Web应用在完成页面组装后经过Callback把内容返回给Web Server。这样作的优势有不少: