Nginx使用的php-fpm的兩種進程管理方式及優(yōu)化
PS:前段時間配置php-fpm的時候,無意中發(fā)現(xiàn)原來它還有兩種進程管理方式。與Apache類似,它的進程數(shù)也是可以根據(jù)設置分為動態(tài)和靜態(tài)的。
php-fpm目前主要又兩個分支,分別對應于php-5.2.x的版本和php-5.3.x的版本。在5.2.x的版本中,php-fpm.conf使用的是xml格式,而在新的5.3.x版本中,則是和php.ini一樣的配置風格。
在5.2.x版本中,php-fpm.conf中對于進程管理號稱是有兩種風格,一種是靜態(tài)(static)的,一種是類似于apache風格(apache-like)的。
Process manager settings
按照文檔的說明,如果pm的style采用apache-like,啟動的進程數(shù)應該是和StartServers指定的一樣。不過經(jīng)過數(shù)次的嘗 試,會發(fā) 現(xiàn),實際上在這里將pm的style配置成apache-like沒有起任何作用。也就是說,這里的apache-like并沒有被實現(xiàn)。
不過,在***的5.3.x的配套php-fpm中,apache風格的進程管理已經(jīng)被實現(xiàn)了。
; Choose how the process manager will control the number of child processes.
; Possible Values:
; static - a fixed number (pm.max_children) of child processes;
; dynamic - the number of child processes are set dynamically based on the
; following directives:
; pm.max_children - the maximum number of children that can
; be alive at the same time.
; pm.start_servers - the number of children created on startup.
; pm.min_spare_servers - the minimum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is less than this
; number then some children will be created.
; pm.max_spare_servers - the maximum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is greater than this
; number then some children will be killed.
; Note: This value is mandatory.
;pm = dynamic
pm = static
由上面一段文字可知,對于進程的管理存在兩種風格——static和dynamic。和之前的版本的進程管理其實還是一樣的,只是將apache-like改成了dynamic,這樣更容易理解。
如果設置成static,php-fpm進程數(shù)自始至終都是pm.max_children指定的數(shù)量,不再增加或減少。如果設置成 dynamic,則php-fpm進程數(shù)是動態(tài)的,最開始是pm.start_servers指定的數(shù)量,如果請求較多,則會自動增加, 保證空閑的進程數(shù)不小于pm.min_spare_servers,如果進程數(shù)較多,也會進行相應清理,保證多余的進程數(shù)不多于 pm.max_spare_servers。
這兩種不同的進程管理方式,可以根據(jù)服務器的實際需求來進行調(diào)整。
這里先說一下涉及到這個的幾個參數(shù),他們分別是pm、pm.max_children、pm.start_servers、pm.min_spare_servers和pm.max_spare_servers。
pm表示使用那種方式,有兩個值可以選擇,就是static(靜態(tài))或者dynamic(動態(tài))。在更老一些的版本中,dynamic被稱作apache-like。這個要注意看配置文件的說明。
下面4個參數(shù)的意思分別為:
pm.max_children:靜態(tài)方式下開啟的php-fpm進程數(shù)量。
pm.start_servers:動態(tài)方式下的起始php-fpm進程數(shù)量。
pm.min_spare_servers:動態(tài)方式下的最小php-fpm進程數(shù)量。
pm.max_spare_servers:動態(tài)方式下的***php-fpm進程數(shù)量。
如果dm設置為static,那么其實只有pm.max_children這個參數(shù)生效。系統(tǒng)會開啟設置數(shù)量的php-fpm進程。如果dm設置為 dynamic,那么pm.max_children參數(shù)失效,后面3個參數(shù)生效。系統(tǒng)會在php-fpm運行開始 的時候啟動pm.start_servers個php-fpm進程,然后根據(jù)系統(tǒng)的需求動態(tài)在pm.min_spare_servers和 pm.max_spare_servers之間調(diào)整php-fpm進程數(shù)。
那么,對于我們的服務器,選擇哪種執(zhí)行方式比較好呢?事實上,跟Apache一樣,運行的PHP程序在執(zhí)行完成后,或多或少會有內(nèi)存泄露的問題。這也是為什么開始的時候一個php-fpm進程只占用3M左右內(nèi)存,運行一段時間后就會上升到20-30M的原因了。
對于內(nèi)存大的服務器(比如8G以上)來說,指定靜態(tài)的max_children實際上更為妥當,因為這樣不需要進行額外的進程數(shù)目控制,會提高效 率。因為頻繁開關php-fpm進程也會有時滯,所以內(nèi)存夠大的情況下開靜態(tài)效果會更好。數(shù)量也可以根據(jù) 內(nèi)存/30M 得到,比如8GB內(nèi)存可以設置為100,那么php-fpm耗費的內(nèi)存就能控制在 2G-3G的樣子。如果內(nèi)存稍微小點,比如1G,那么指定靜態(tài)的進程數(shù)量更加有利于服務器的穩(wěn)定。這樣可以保證php-fpm只獲取夠用的內(nèi)存,將不多的 內(nèi)存分配給其他應用去使用,會使系統(tǒng)的運行更加暢通。
對于小內(nèi)存的服務器來說,比如256M內(nèi)存的VPS,即使按照一個20M的內(nèi)存量來算,10個php-cgi進程就將耗掉200M內(nèi)存,那系統(tǒng)的崩 潰就應該很正常了。因此應該盡量地控制php-fpm進程的數(shù)量,大體明確其他應用占用的內(nèi)存后,給它指定一個靜態(tài)的小數(shù)量,會讓系統(tǒng)更加平穩(wěn)一些?;蛘呤褂脛討B(tài)方式,因為動態(tài)方式會結束掉多余的進程,可以回收釋放一些內(nèi)存,所以推薦在內(nèi)存較少的服務器或VPS上使用。具體***數(shù)量根據(jù) 內(nèi)存/20M 得到。比如說512M的VPS,建議pm.max_spare_servers設置為20。至于pm.min_spare_servers,則建議根據(jù)服 務器的負載情況來設置,比較合適的值在5~10之間。