cig和fast-cgi的關系,fast-cgi和php-cgi、php-fpm、spawn-fcgi的區別和聯系
首先貼上原版的英文解釋: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服務器的一部分,不過現在一個是一個獨立的項目。
自己理解
cgi是一種接口標準,fast-cgi是一種更外先進的cgi標準。
php-cgi是php實現的fast-cgi接口標準的進程管理器。但是它不能平滑重啟。
php-fpm只是php之前版本中的一個補丁,也是實現了fast-cgi接口標準?,F在php主干版本中的一部分,可以平滑啟動。
spawn-fcgi 是一個通用的fast-cgi接口,是lighttpd的一部分。
php-fpm和spawn-fcgi 都是php-cgj的進程管理器,不過php-fpm占cup多,內存分布均勻,spawn-fcgi 不占cpu,內存分布不均勻, 總體而言 php-fpm 更為優秀。
cgi接口每次調用就要初始化一次資源,fast-cgi 只初始化一次資源
他人的理解
FASTCGI:WEB服務器與處理程序之間通信的一種協議,是CGI的改進方案。
CGI程序反復加載是CGI性能低下的主要原因,如果CGI程序保持在內存中并接受FastCGI進程管理器調度,則可以提供良好的性能、伸縮性、Fail-Over特性等。
FASTCGI是常駐型的CGI,它可以一直運行,在請求到達時,不會花費時間去fork一個進程來處理。
FastCGI是語言無關的、可伸縮架構的CGI開放擴展,將CGI解釋器進程保持在內存中,以此獲得較高的性能。
一般情況下,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