CGI,FastCGI,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.

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.php

  • the main process of running FastCGI:html

    1. Loading the Process Manager of FastCGI when a Web server has booted(IIS ISAPI or Apache Module)
    2. The Process Manager of FastCGI will initiate itself to create several CGI processes, which are used to wait for connection of Web servers.
    3. 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.
    4. 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.

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(Fast-CGI Process Manager)

  • 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(5.3.2之後已經集成於php中).
  • 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.

 

 

做者:auxten
連接:https://www.zhihu.com/question/30672017/answer/49817021
來源:知乎
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。

CGI(Common Gateway Interface)nginx

最初,CGI 是在 1993 年由美國國家超級電腦應用中心(NCSA)爲 NCSA HTTPd Web 服務器開發的。程序員

這個 Web 服務器使用了 UNIX shell 環境變量 來保存從 Web 服務器傳遞出去的參數,而後生成一個運行 CGI 的獨立進程。CGI的第一個實現是 Perl 寫的[1]。正則表達式

  • 效率低下:每個鏈接 fork 一個進程處理。
  • 功能十分有限:CGI只能收到一個請求,輸出一個響應。很難在CGI體系去對Web請求的控制,例如:用戶認證等。

正由於這些問題,在CGI誕生後的很長一段時間,各類Web Server都仍是採用API這種強綁定的方式去支持Web開發,其中Apache的mod_php就屬於這種方式。因此後面就有大神提出了FastCGI標準。shell

FastCGI(Fast Common Gateway Interface)編程

FastCGI使用進程/線程池來處理一連串的請求。這些進程/線程由FastCGI服務器管理,而不是Web服務器。 當進來一個請求時,Web服務器把環境變量和這個頁面請求經過一個Socket長鏈接傳遞給FastCGI進程。因此FastCGI有以下的優勢:後端

  • 性能:經過進程/線程池規避了CGI開闢新的進程的開銷。
  • 兼容:很是容易改造現有CGI標準的程序。
  • 語言無關:FastCGI是一套標準,理論上講只要能進行標準輸出(stdout)的語言均可以做爲FastCGI標準的Web後端。
    下面是一個簡單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);
}

  • Web Server隔離:FastCGI後端和Web Server運行在不一樣的進程中,後端的任何故障不會致使Web Server掛掉。
  • 專利:沒有Apache mod_php之類的私有API的知識產權問題。
  • 擴展:FastCGI後端和Web Server經過Socket進行通訊,二者能夠分佈式部署並方便進行橫向擴展。

因此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。這樣作的優勢有不少:

  • 異步化,經過Callback將Web請求的工做拆解開,能夠很方便的在一個線程空間裏同時處理多個Web請求。
  • 方便進行各類負載均衡和請求轉發,不會形成後端Web應用阻塞。

  1. Web開發有3P:Perl、Python、PHP。Perl是1987年發佈的,Python是1989年,PHP是1995年。CGI標準提出的時候正是Perl如日中天的時候,因此CGI的提出當時也是主要爲了解決Perl做爲Web編程語言的需求。熟悉正則(regex)的程序員可能知道正則的事實標準叫作pcre(Perl兼容正則表達式,Perl Compatible Regular Expressions),這也從一個側面體現了Perl做爲一個古老的語言在當時對各類業界標準的影響。
相關文章
相關標籤/搜索