如果設定成dynamic,則進程數是動態,最開始是pm.start_servers指定的數量,如果請求較多,則會自動增加,但不超過pm.max_children指定的數量,同時保證閒置進程數不小於pm.min_spare_servers,如果進程數較多,也會進行相應清理,保證多餘的進程數不多於pm.max_spare_servers。
當php-fpm啟動後,一個php-cgi進程約戰3M記憶體,但是當它們處理過一些請求後,有些記憶體是釋放不掉的,佔用的記憶體能達到20M-30M不等。
對於記憶體比較吃緊,同時並發量不是很大的應用,可以考慮採用static的方式,這樣可以很好的控制php-fpm的所消耗的總記憶體數,讓系統更加平穩運行。另外由於並發量很小,可以適當的把設定pm.max_requests小一些,以便讓php-fpm進程有機會重啟,從而釋放其佔用的記憶體。
使用PHP-FPM來控制PHP-CGI的FastCGI進程
/usr/local/php/sbin/php-fpm{start|stop|quit|restart|reload|logrotate}
--start 啟動php的fastcgi進程
--stop 強制終止php的fastcgi進程
--quit 平滑終止php的fastcgi進程
--restart 重啟php的fastcgi進程
--reload 重新平滑載入php的php.ini
--logrotate 重新啟用log檔案
php-fpm目前主要又兩個分支,分別對應於php-5.3.3以前的版本和php-5.3.3以後的版本。在5.2.x的版本中,php-fpm.conf使用的是xml格式,而在新的5.3.3以後的版本中,則是php.ini一樣的配置風格。對於5.5.3以後的版本中存在兩種php-fpm進程的管理方式——static和dynamic。具體/usr/local/php/conf/php-fpm.conf.default中的配置如下:
; 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. With this process management, there will be
; always at least 1 children.
; 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.
; ondemand - no children are created at startup. Children will be forked when
; new requests will connect. The following parameter are used:
; pm.max_children - the maximum number of children that
; can be alive at the same time.
; pm.process_idle_timeout - The number of seconds after which
; an idle process will be killed.
; Note: This value is mandatory.
pm = dynamic
從上面可以看到預設是啟用的動態管理方式。而php-fpm進程數是動態,最開始是pm.start_servers指定的數量,如果請求較多,則會自動增加,保證閒置進程數不小於pm.min_spare_servers,如果進程數較多,也會進行相應清理,保證多餘的進程數不多於pm.max_spare_servers。在上面的設定檔中也可以看到其中涉及到四個參數的設定,其作用分別如下:
pm.max_children:靜態方式下開啟的php-fpm進程數量。
pm.start_servers:動態方式下的起始php-fpm進程數量。
pm.min_spare_servers:動態方式下的最小php-fpm進程數量。
pm.max_spare_servers:動態方式下的最大php-fpm進程數量。
如果dm設定為static,那麼其實只有pm.max_children這個參數生效。系統會開啟設定數量的php-fpm進程。
如果dm設定為dynamic,那麼pm.max_children參數失效,後面3個參數生效。系統會在php-fpm運行開始的時候啟動pm.start_servers個php-fpm進程,然後根據系統的需求動態在pm.min_spare_servers和pm.max_spare_servers之間調整php-fpm進程數。
那麼,對於我們的伺服器,選擇哪種執行方式比較好呢?事實上,跟Apache一樣,啟動並執行PHP程式在執行完成後,或多或少會有記憶體泄露的問題。這也是為什麼開始的時候一個php-fpm進程只佔用3M左右記憶體,運行一段時間後就會上升到20-30M的原因了。
對於記憶體大的伺服器(比如8G以上)來說,指定靜態max_children實際上更為妥當,因為這樣不需要進行額外的進程數目控制,會提高效率。因為頻繁開關php-fpm進程也會有時滯,所以記憶體夠大的情況下開靜態效果會更好。比如8GB記憶體可以設定為100,那麼php-fpm耗費的記憶體就能控制在 2G-3G的樣子。,如果資料庫和web應用在不同的伺服器上,該機器只跑web應用,大可開到300左右。如果記憶體稍微小點,比如1G那麼指定靜態進程數量更加有利於伺服器的穩定。這樣可以保證php-fpm只擷取夠用的記憶體,將不多的記憶體配置給其他應用去使用,會使系統的運行更加暢通。
對於小記憶體的伺服器來說,比如256M記憶體的VPS,即使按照一個20M的記憶體量來算,10個php-cgi進程就將耗掉200M記憶體,那系統的崩潰就應該很正常了。因此應該盡量地控制php-fpm進程的數量,大體明確其他應用佔用的記憶體後,給它指定一個靜態小數量,會讓系統更加平穩一些。或者使用動態方式,因為動態方式會結束掉多餘的進程,可以回收釋放一些記憶體,所以推薦在記憶體較少的伺服器或VPS上使用。比如說512M的VPS,建議pm.max_spare_servers設定為20。至於pm.min_spare_servers,則建議根據伺服器的負載情況來設定,比較合適的值在5~10之間。
這兩種不同的進程管理方式,可以根據伺服器的實際需求來進行調整。
這裡先說一下涉及到這個的幾個參數,他們分別是pm、pm.max_children、pm.start_servers、pm.min_spare_servers和pm.max_spare_servers。
pm表示使用那種方式,有兩個值可以選擇,就是static(靜態)或者dynamic(動態)。在更老一些的版本中,dynamic被稱作apache-like。這個要注意看設定檔的說明。
下面4個參數的意思分別為:
pm.max_children:靜態方式下開啟的php-fpm進程數量。
pm.start_servers:動態方式下的起始php-fpm進程數量。
pm.min_spare_servers:動態方式下的最小php-fpm進程數量。
pm.max_spare_servers:動態方式下的最大php-fpm進程數量。
如果dm設定為static,那麼其實只有pm.max_children這個參數生效。系統會開啟設定數量的php-fpm進程。
如果dm設定為dynamic,那麼pm.max_children參數失效,後面3個參數生效。系統會在php-fpm運行開始的時候啟動pm.start_servers個php-fpm進程,然後根據系統的需求動態在pm.min_spare_servers和pm.max_spare_servers之間調整php-fpm進程數。
那麼,對於我們的伺服器,選擇哪種執行方式比較好呢?事實上,跟Apache一樣,啟動並執行PHP程式在執行完成後,或多或少會有記憶體泄露的問題。這也是為什麼開始的時候一個php-fpm進程只佔用3M左右記憶體,運行一段時間後就會上升到20-30M的原因了。
對於記憶體大的伺服器(比如8G以上)來說,指定靜態max_children實際上更為妥當,因為這樣不需要進行額外的進程數目控制,會提高效率。因為頻繁開關php-fpm進程也會有時滯,所以記憶體夠大的情況下開靜態效果會更好。數量也可以根據 記憶體/30M 得到,比如8GB記憶體可以設定為100,那麼php-fpm耗費的記憶體就能控制在 2G-3G的樣子。如果記憶體稍微小點,比如1G,那麼指定靜態進程數量更加有利於伺服器的穩定。這樣可以保證php-fpm只擷取夠用的記憶體,將不多的記憶體配置給其他應用去使用,會使系統的運行更加暢通。
對於小記憶體的伺服器來說,比如256M記憶體的VPS,即使按照一個20M的記憶體量來算,10個php-cgi進程就將耗掉200M記憶體,那系統的崩潰就應該很正常了。因此應該盡量地控制php-fpm進程的數量,大體明確其他應用佔用的記憶體後,給它指定一個靜態小數量,會讓系統更加平穩一些。或者使用動態方式,因為動態方式會結束掉多餘的進程,可以回收釋放一些記憶體,所以推薦在記憶體較少的伺服器或VPS上使用。具體最大數量根據 記憶體/20M 得到。比如說512M的VPS,建議pm.max_spare_servers設定為20。至於pm.min_spare_servers,則建議根據伺服器的負載情況來設定,比較合適的值在5~10之間。