天天看點

php-fpm配置檔案詳解

第一部分:FPM 配置

參數          | 說明

 -p            | 指令行中動态修改--prefix  

;include=etc/fpm.d/*.conf  | 用于包含一個或多個檔案,如果glob(3)存在(glob()函數傳回比對指定模式的檔案名或目錄)

第二部分:全局配置

由标志[global]開始:

;pid = run/php-fpm.pid      設定pid檔案的位置,預設目錄路徑 /usr/local/php/var
;error_log = log/php-fpm.log  記錄錯誤日志的檔案,預設目錄路徑 /usr/local/php/var
;syslog.facility = daemon    用于指定什麼類型的程式日志消息。
;syslog.ident = php-fpm      用于FPM多執行個體甄别
;log_level = notice        記錄日志的等級,預設notice,可取值alert, error, warning, notice, debug
;emergency_restart_threshold = 0 如果子程序在這個時間段内帶有IGSEGV或SIGBUS退出,則重新開機fpm,預設0表示關閉這個功能
;emergency_restart_interval = 0 設定時間間隔來決定服務的初始化時間(預設機關:s秒),可選s秒,m分,h時,d天
;process_control_timeout = 0    子程序等待master程序對信号的回應(預設機關:s秒),可選s秒,m分,h時,d天
; process.max = 128         控制最大程序數,使用時需謹慎
; process.priority = -19      處理nice(2)的程序優先級别-19(最高)到20(最低)
;rlimit_files = 1024        設定主程序檔案描述符rlimit的數量
;rlimit_core = 0           設定主程序rlimit最大核數
;events.mechanism = epoll     使用處理event事件的機制
  ; - select     (any POSIX os)
  ; - poll       (any POSIX os)
  ; - epoll      (linux >= 2.5.44)
  ; - kqueue     (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
  ; - /dev/poll  (Solaris >= 7)
  ; - port       (Solaris >= 10)
;daemonize = yes           将fpm轉至背景運作,如果設定為“no”,那麼fpm會運作在前台
;systemd_interval = 10
      

第三部分:程序池的定義

通過監聽不同的端口和不用管理選擇可以定義多個不同的子程序池,程序池被用與記錄和統計,對于fpm能夠處理程序池數目的多少并沒有限制

其中$pool變量可以在任何指令中使用,他将會替代相應的程序池名字。例如:這裡的[www]

[root@test ~]# ps -ef | grep php-fpm
root      3028     1  0 20:33 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nobody    3029  3028  0 20:33 ?        00:00:00 php-fpm: pool www          
nobody    3030  3028  0 20:33 ?        00:00:00 php-fpm: pool www      
[www]
; It only applies on the following directives:
; - 'access.log'
; - 'slowlog'
; - 'listen' (unixsocket)
; - 'chroot'
; - 'chdir'
; - 'php_values'
; - 'php_admin_values'

;prefix = /path/to/pools/$pool   如果沒有制定,将使用全局prefix替代
user = nobody             程序的發起使用者和使用者組,使用者user是必須設定,group不是
group = nobody
listen = 127.0.0.1:9000       監聽ip和端口
;listen.backlog = 65535       設定listen(2)函數backlog
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0660
;listen.acl_users =
;listen.acl_groups =
;listen.allowed_clients = 127.0.0.1 允許FastCGI用戶端連接配接的IPv4位址,多個位址用','分隔,為空則允許任何位址發來連結請求
; process.priority = -19
pm = dynamic              選擇程序池管理器如何控制子程序的數量
  static:  對于子程序的開啟數路給定一個鎖定的值(pm.max_children)
  dynamic:  子程序的數目為動态的,它的數目基于下面的指令的值(以下為dynamic适用參數)
    pm.max_children:  同一時刻能夠存貨的最大子程序的數量
    pm.start_servers: 在啟動時啟動的子程序數量
    pm.min_spare_servers: 處于空閑"idle"狀态的最小子程序,如果空閑程序數量小于這個值,那麼相應的子程序會被建立
    pm.max_spare_servers: 最大空閑子程序數量,空閑子程序數量超過這個值,那麼相應的子程序會被殺掉。
  ondemand: 在啟動時不會建立,隻有當發起請求連結時才會建立(pm.max_children, pm.process_idle_timeout)

pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
;pm.process_idle_timeout = 10s;  空閑程序逾時時間
;pm.max_requests = 500        在派生新的子程序前,每一個子程序應該處理的請求數目,在第三方庫中解決記憶體溢出很有用,設定為0則不會限制
;pm.status_path = /status        配置一個URI,以便檢視fpm狀态頁
狀态頁描述:
  accepted conn: 該程序池接受的請求數量
  pool: 程序池的名字
  process manager: 程序管理,就是配置中pm指令,可以選擇值static,dynamic,ondemand
  idle processes: 空閑程序數量
  active processes: 目前活躍的程序數量
  total processes: 總的程序數量=idle+active
  max children reached: 達到最大子程序的次數,達到程序的限制,當pm試圖開啟更多的子程序的時候(僅當pm工作在dynamic時)
;ping.path = /ping    該ping URI将會去調用fpm監控頁面,如果這個沒有設定,那麼不會有URI被做為ping頁
;ping.response = pong  用于定制平請求的響應,響應的格式text/plain(對200響應代碼)
;access.log = log/$pool.access.log
;access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
  ; The following syntax is allowed
  ;  %%: the '%' character
  ;  %C: %CPU used by the request
  ;      it can accept the following format:
  ;      - %{user}C for user CPU only
  ;      - %{system}C for system CPU only
  ;      - %{total}C  for user + system CPU (default)
  ;  %d: time taken to serve the request
  ;      it can accept the following format:
  ;      - %{seconds}d (default)
  ;      - %{miliseconds}d
  ;      - %{mili}d
  ;      - %{microseconds}d
  ;      - %{micro}d
  ;  %e: an environment variable (same as $_ENV or $_SERVER)
  ;      it must be associated with embraces to specify the name of the env
  ;      variable. Some exemples:
  ;      - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e
  ;      - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e
  ;  %f: script filename
  ;  %l: content-length of the request (for POST request only)
  ;  %m: request method
  ;  %M: peak of memory allocated by PHP
  ;      it can accept the following format:
  ;      - %{bytes}M (default)
  ;      - %{kilobytes}M
  ;      - %{kilo}M
  ;      - %{megabytes}M
  ;      - %{mega}M
  ;  %n: pool name
  ;  %o: output header
  ;      it must be associated with embraces to specify the name of the header:
  ;      - %{Content-Type}o
  ;      - %{X-Powered-By}o
  ;      - %{Transfert-Encoding}o
  ;      - ....
  ;  %p: PID of the child that serviced the request
  ;  %P: PID of the parent of the child that serviced the request
  ;  %q: the query string
  ;  %Q: the '?' character if query string exists
  ;  %r: the request URI (without the query string, see %q and %Q)
  ;  %R: remote IP address
  ;  %s: status (response code)
  ;  %t: server time the request was received
  ;      it can accept a strftime(3) format:
  ;      %d/%b/%Y:%H:%M:%S %z (default)
  ;  %T: time the log has been written (the request has finished)
  ;      it can accept a strftime(3) format:
  ;      %d/%b/%Y:%H:%M:%S %z (default)
  ;  %u: remote user
;slowlog = log/$pool.log.slow   用于記錄慢請求
;request_slowlog_timeout = 0    慢日志請求逾時時間,對一個php程式進行跟蹤。
;request_terminate_timeout = 0  終止請求逾時時間,在worker程序被殺掉之後,提供單個請求的逾時間隔。由于某種原因不停止腳本執行時,應該使用該選項,0表示關閉不啟用
  (在php.ini中,max_execution_time 一般設定為30,表示每一個腳本的最大執行時間)
;rlimit_files = 1024        設定打開檔案描述符的限制
;rlimit_core = 0           設定核心對資源的使用限制,用于核心轉儲
;chroot =               設定chroot路徑,程式一啟動就将其chroot放置到指定的目錄下,該指令值必須是一個絕對路徑
;chdir = /var/www          在程式啟動時将會改變到指定的位置(這個是相對路徑,相對目前路徑或chroot後的“/”目錄)    
;catch_workers_output = yes    将worker的标準輸出和錯誤輸出重定向到主要的錯誤日志記錄中,如果沒有設定,根據FastCGI的指定,将會被重定向到/dev/null上
;clear_env = no            清理環境
;security.limit_extensions = .php .php3 .php4 .php5  限制FPM執行解析的擴充名
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

; Additional php.ini defines, specific to this pool of workers. These settings
; overwrite the values previously defined in the php.ini. The directives are the
; same as the PHP SAPI:
;   php_value/php_flag             - you can set classic ini defines which can
;                                    be overwritten from PHP call 'ini_set'.
;   php_admin_value/php_admin_flag - these directives won't be overwritten by
;                                     PHP call 'ini_set'
; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.

; Defining 'extension' will load the corresponding shared extension from
; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
; overwrite previously defined php.ini values, but will append the new value
; instead.

;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f [email protected]
;php_flag[display_errors] = off
;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M
        

總結

1) 在php-fpm的配置檔案中,有兩個指令非常重要,就是"pm.max_children" 和 "request_terminate_timeout"

 第一個指令"pm.max_children" 确定了php-fpm的處理能力,原則上時越多越好,但這個是在記憶體足夠打的前提下,每開啟一個php-fpm程序要占用近30M左右的記憶體

 如果請求通路較多,那麼可能會出現502,504錯誤。對于502錯誤來說,屬于繁忙程序而造成的,對于504來說,就是客戶發送的請求在限定的時間内沒有得到相應,過多的請求導緻“504  Gateway  Time-out”。這裡也有可能是伺服器帶寬問題。

    另外一個需要注意的指令"request_terminate_timeout",它決定php-fpm程序的連接配接/發送和讀取的時間,如果設定過小很容易出現"502 Bad Gateway" 和 “504  Gateway  Time-out”,預設為0,就是說沒有啟用,不加限制,但是這種設定前提是你的php-fpm足夠健康,這個需要根據實際情況加以限定。