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 www@my.domain.com
;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足夠健康,這個需要根據實際情況加以限定。