cig和fast-cgi的关系,fast-cgi和php-cgi、php-fpm、spawn-fcgi的区别和联系

来源:互联网 发布:好看的网络剧 编辑:程序博客网 时间:2024/05/20 06:04

cig和fast-cgi的关系,fast-cgi和php-cgi、php-fpm、spawn-fcgi的区别和联系


  1. 首先贴上原版的英文解释:
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.   意思就是:CGI是公共网关接口,是http服务器和其他服务器上的程序交互的工具,只要程序语言提供符合CGI标准的输入、输出和环境变量即可,如像php、perl等都可以和http服务器交互。
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).意思就是:    Fast-cgi是一种常驻内存的cgi,用Fast-cgi接口可以花更少的时间起一个进程,类似于设计模式中的原型模式,实例化对象消耗更少的资源,而且Fast-cgi支持分布式计算。    Fast-cgi的进程管理器会初始化自己和一些cgi进程,就是起一个master和几个worker进程。当客户端有请求发送到web 服务器上时,Fast-cgi的进程管理器会提前选择一个cgi进程,并把环境变量和标准输入发送给Fast-cgi的子进程php-cgi。子进程会返回标准输出和错误信息给web 服务器通过和web服务器之间的连接,当请求结束后子进程会断开和web服务器之间的连接。    Fast-cgi 仅仅初始化一次对php.ini的解析,仅加载一次扩展和初始化一次所有的数据结构。    由于是多进程的(类似于一个master和几个worker),Fast-cgi会比cgi消耗更多的内存,每一个php-cgi进程消耗7M~25M的内存。 
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    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-cgi是Fast-cgi进程管理器的一种,是php自带的,启动方式如下:        php-cgi -b 127.0.0.1:9000    当更改php.ini配置文件后必须重启php-cgi。结束php-cgi的进程时,所有的php代码都不能执行。意思就是不能平滑重启,php-fpm和spawn-fcgi可以平滑重启。
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意思就是:    php-fpm是另外一种Fast-cgi的进程管理器,php5.3.2之前是作为php分支中的Fast-cgi进程管理的,需要通过编译的方式使用,php5.3.2之后已经合并到主干中可以直接使用。
Spawn-FCGI:    Spawn-FCGI is a general kind of the Process Manager of FastCGI, which is one part of lighttpd.意思就是; spawn-fcgi 是一种通用的fast-cgi的进程管理器,是lighttpd服务器的一部分,不过现在一个是一个独立的项目。

自己理解

  1. cgi是一种接口标准,fast-cgi是一种更外先进的cgi标准。

  2. php-cgi是php实现的fast-cgi接口标准的进程管理器。但是它不能平滑重启。

  3. php-fpm只是php之前版本中的一个补丁,也是实现了fast-cgi接口标准。现在php主干版本中的一部分,可以平滑启动。

  4. spawn-fcgi 是一个通用的fast-cgi接口,是lighttpd的一部分。

  5. php-fpm和spawn-fcgi 都是php-cgj的进程管理器,不过php-fpm占cup多,内存分布均匀,spawn-fcgi 不占cpu,内存分布不均匀, 总体而言 php-fpm 更为优秀。

  6. cgi接口每次调用就要初始化一次资源,fast-cgi 只初始化一次资源


他人的理解

  1. FASTCGI:WEB服务器与处理程序之间通信的一种协议,是CGI的改进方案。

  2. CGI程序反复加载是CGI性能低下的主要原因,如果CGI程序保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail-Over特性等。

  3. FASTCGI是常驻型的CGI,它可以一直运行,在请求到达时,不会花费时间去fork一个进程来处理。

  4. FastCGI是语言无关的、可伸缩架构的CGI开放扩展,将CGI解释器进程保持在内存中,以此获得较高的性能。

  5. 一般情况下,FastCGI的整个工作流程是这样的:

    1、Web Server启动时载入FastCGI进程管理器(IIS ISAPI或Apache Module)2、FastCGI进程管理器自身初始化,启动多个CGI解释器进程(可见多个php-cgi)并等待WebServer的连接。3、当客户端请求到达Web Server时,FastCGI进程管理器选择并连接到一个CGI解释器。 Web server将CGI环境变量和标准输入发送到FastCGI子进程php-cgi。4、FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回Web Server。当FastCGI子进程关闭连接时,请求便告处理完成。FastCGI子进程接着等待并处理来自FastCGI进程管理器(运行在Web Server中)的下一个连接。在CGI模式中,php-cgi在此便退出了。

基本上cgi、fast-cgi、php-cgi、php-fpm、spawn-fcgi的概念、关系、区别就这些吧。

欢迎大家指正、参考、分享。


2017/02/21 15:03

0 0
原创粉丝点击