天天看點

【深度】一文讀懂cgi,fast-cgi,php-cgi,php-fpm

這幾個概念,總是不是太清晰,索性寫篇文章整理一下,讓模糊的清晰起來,讓清楚的更清楚。

一、是什麼

cgi,fastcgi是協定,協定是規定資料傳輸格式的

php-cgi,php-fpm是程式,程式完成某一種功能的代碼塊

CGI

Common Gateway Interface, is a prootocal 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

it is a kind of CGI which is long-live, which will always be running. With FastCGI, it’ll take less time to 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 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.

PHP-CGI

one kind of the Process Manager of FastCGI, which is within php itself.

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

another kind of the Process Manager of FastCGI, 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:

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和FastCGI差別

我們都知道,web_server(apache,nginx等)隻是内容分發者,如果是index.html之類的靜态檔案,server會在檔案系統中找到這個檔案并傳回,但是對于動态語言,如index.php,server會把這個請求轉發給php解釋器來處理,那麼從server到php解釋器,需要轉發哪些資料呢?這就是由CGI協定規定的,如下圖所示[2]。

【深度】一文讀懂cgi,fast-cgi,php-cgi,php-fpm

FastCGI也是一種協定,是為了提升CGI的性能而生的,是CGI協定的更新版。怎麼做到的呢?

當server收到請求後,會啟動對應的CGI程式,此處是PHP解析器,然後PHP解析器會加載配置檔案,初始化執行環境,然後處理請求,再以CGI規定的資料格式傳回結果到server,關閉程序,CGI的性能瓶頸就在于每次請求都要重複加載配置,初始化環境,退出程序。

fastCGI會先啟動一個master,解析配置檔案,初始化執行環境,然後再啟動多個worker。當請求過來時,master會把請求傳遞給其中的一個空閑worker,然後可以繼續接受下一個請求。這樣就避免了重複的加載配置,初始化執行環境等操作。而且當worker不夠用時,master可以根據配置預先啟動幾個worker等着;當然空閑worker太多時,也會停掉一些,這樣就提高了性能,也節約了資源。這就是fastcgi的對程序的管理。

fastCGI 針對php,python,java等都有實作,如下圖所示[2]。

【深度】一文讀懂cgi,fast-cgi,php-cgi,php-fpm

三、php-cgi和php-fpm差別

php-cgi:官方FastCGI管理器。PHP-CGI的不足:

php-cgi變更php.ini配置後需重新開機php-cgi才能讓新的php-ini生效,不可以平滑重新開機。直接殺死php-cgi程序,php就不能運作了。(PHP-FPM和Spawn-FCGI沒有這個問題,守護程序會平滑從新生成新的子程序)

php-fpm:是實作了FastCGI協定的接口程式,額外還提供了相對智能一些任務管理,最開始隻是一個更新檔,後來融合到php主分支上了,是以現在的php版本都自帶php-fpm。

參考文檔

1 https://help.superhosting.bg/en/cgi-common-gateway-interface-fastcgi.html

2 https://www.cnblogs.com/xjnotxj/p/7511778.htm

繼續閱讀