php-fpm介绍及配置
php-fpm是什么
全稱是php fastcgi process manager即php fastcgi進程管理器,相比fastcgi靜態的喚起cgi,fpm能根據訪問的壓力動態的喚起cgi進程和銷毀以到達動態的調整cgi數量,這樣可以有效的使用內存。除此之外還有其它的一些優點,比如,fpm還可以平滑的重載php配置;由于fpm是使用Unix-Socket來和服務器通訊,所以也不用再配置cgi端口;fpm有更好的狀態輸出和slowlog日志,502的時候能給出更多的錯誤細節。
php-fpm配置
從php5.3.3版本開始就已經集成了fpm,不再是第三方的包了,如果使用的是php5.3.3以前的版本就得自己去安裝fpm包了,對于php5.2.x的版本,fpm的配置文件還是xml格式,php5.3.x開始fpm配置文件就已經支持ini格式了。
下面是一個ini格式的配置文件
[plain] view plaincopyprint?
;this configuration is for PHP 5.3.10 for heavy loaded servers
[global]
error_log = syslog
log_level = error
rlimit_files = 655360
rlimit_core = 0
[myapp]
user = nobody
group = nobody
listen = fpm.sock
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 1000
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 500
; system max load (request number) = max_children * max_requests = 500,000
pm.status_path = /fpm_status
ping.path = /status
ping.response = ok
slowlog = /data/phplogs/phpfpm_slow.log
request_slowlog_timeout = 3s
;listen.backlog = 262144
; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0
rlimit_files = 655360
rlimit_core = 0
security.limit_extensions = .php .html
php_admin_value['date.timezone'] = 'Asia/Shanghai'
;this configuration is for PHP 5.3.10 for heavy loaded servers [global] error_log = syslog log_level = error rlimit_files = 655360 rlimit_core = 0 [myapp] user = nobody group = nobody listen = fpm.sock listen.allowed_clients = 127.0.0.1 pm = dynamic pm.max_children = 1000 pm.start_servers = 20 pm.min_spare_servers = 10 pm.max_spare_servers = 30 pm.max_requests = 500 ; system max load (request number) = max_children * max_requests = 500,000 pm.status_path = /fpm_status ping.path = /status ping.response = ok slowlog = /data/phplogs/phpfpm_slow.log request_slowlog_timeout = 3s ;listen.backlog = 262144 ; The timeout for serving a single request after which the worker process will ; be killed. This option should be used when the 'max_execution_time' ini option ; does not stop script execution for some reason. A value of '0' means 'off'. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays) ; Default Value: 0 ;request_terminate_timeout = 0 rlimit_files = 655360 rlimit_core = 0 security.limit_extensions = .php .html php_admin_value['date.timezone'] = 'Asia/Shanghai'
參數說明:
error_log string
錯誤日志的位置. 默認: 安裝路徑#INSTALL_PREFIX#/log/php-fpm.log.
log_level string
錯誤級別. 可用級別為: alert(必須立即處理), error(錯誤情況), warning(警告情況), notice(一般重要信息), debug(調試信息). 默認: notice.
emergency_restart_threshold int
如果子進程在emergency_restart_interval設定的時間內收到該參數設定次數的SIGSEGV 或者 SIGBUS退出信息號,則FPM會重新啟動。 0 表示 ‘關閉該功能’. 默認值: 0 (關閉).
emergency_restart_interval mixed
emergency_restart_interval用于設定平滑重啟的間隔時間. 這么做有助于解決加速器中共享內存的使用問題. 可用單位: s(秒), m(分), h(小時), 或者 d(天). 默認單位: s(秒). 默認值: 0 (關閉).
process_control_timeout mixed
設置子進程接受主進程復用信號的超時時間. 可用單位: s(秒), m(分), h(小時), 或者 d(天) 默認單位: s(秒). 默認值: 0.
daemonize boolean
設置FPM在后臺運行. 設置 ‘no’ 將 FPM 保持在前臺運行用于調試. 默認值: yes.
運行配置區段
在FPM中,可以使用不同的設置來運行多個進程池。 這些設置可以針對每個進程池單獨設置。
listen string
設置接受FastCGI請求的地址. 可用格式為: ‘ip:port’, ‘port’, ‘/path/to/unix/socket’. 每個進程池都需要設置.
listen.backlog int
設置 listen(2) 的半連接隊列長度. ‘-1′ 表示無限制. 默認值: -1.
listen.allowed_clients string
設置允許連接到FastCGI的服務器IPV4地址. 等同于PHP FastCGI (5.2.2+)中的 FCGI_WEB_SERVER_ADDRS環境變量. 僅對TCP監聽起作用. 每個地址是用逗號分隔. 如果沒有設置或者為空,則允許任何服務器請求連接. 默認值: any.
listen.owner string
如果使用,表示設置Unix套接字的權限. 在Linux中,讀寫權限必須設置,以便用于WEB服務器連接. 在很多BSD派生的系統中可以忽略權限允許自由連接. 默認值: 運行所使用的用戶合租, 權限為0666.
listen.group string
參見 listen.owner.
listen.mode string
參見 listen.owner.
user string
FPM 進程運行的Unix用戶. 必須設置.
group string
FPM 進程運行的Unix用戶組. 如果沒有設置,則默認用戶的組被使用.
pm string
設置進程管理器如何管理子進程. 可用值: static, dynamic. 必須設置.
static – 子進程的數量是固定的 (pm.max_children).
dynamic – 子進程的數量在下面配置的基礎上動態設置: pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers.
pm.max_children int
子進程的數量,pm 設置為 static 時表示創建的, pm 設置為 dynamic 時表示最大可創建的. 必須設置.
該選項設置可以同時提供服務的請求數限制. 類似 Apache 的 mpm_prefork 中 MaxClients 的設置和 普通PHP FastCGI中的 PHP_FCGI_CHILDREN 環境變量.
pm.start_servers in
設置啟動時創建的子進程數目. 僅在 pm 設置為 dynamic 時使用. 默認值: min_spare_servers + (max_spare_servers – min_spare_servers) / 2.
pm.min_spare_servers int
設置空閑服務進程的最低數目. 僅在 pm 設置為 dynamic 時使用. 必須設置.
pm.max_spare_servers int
設置空閑服務進程的最大數目. 僅在 pm 設置為 dynamic 時使用. 必須設置.
pm.max_requests int
設置每個子進程重生之前服務的請求數. 對于可能存在內存泄漏的第三方模塊來說是非常有用的. 如果設置為 ’0′ 則一直接受請求. 等同于 PHP_FCGI_MAX_REQUESTS 環境變量. 默認值: 0.
pm.status_path string
FPM狀態頁面的網址. 如果沒有設置, 則無法訪問狀態頁面. 默認值: none.
ping.path string
FPM監控頁面的ping網址. 如果沒有設置, 則無法訪問ping頁面. 該頁面用于外部檢測FPM是否存活并且可以響應請求. 請注意必須以斜線開頭 (/).
ping.response string
用于定義ping請求的返回相應. 返回為 HTTP 200 的 text/plain 格式文本. 默認值: pong.
request_terminate_timeout mixed
設置單個請求的超時中止時間. 該選項可能會對php.ini設置中的’max_execution_time’因為某些特殊原因沒有中止運行的腳本有用. 設置為 ’0′ 表示 ‘Off’. Available units: s(econds)(default), m(inutes), h(ours), or d(ays). Default value: 0.
request_slowlog_timeout mixed
當一個請求該設置的超時時間后,就會將對應的PHP調用堆棧信息完整寫入到慢日志中. 設置為 ’0′ 表示 ‘Off’. 可用單位: s(秒)(默認), m(分), h(小時), 或者 d(天). 默認值: 0.
slowlog string
慢請求的記錄日志. 默認值: #INSTALL_PREFIX#/log/php-fpm.log.slow.
rlimit_files int
設置文件打開描述符的rlimit限制. 默認值: 系統定義值.
rlimit_core int
設置核心rlimit最大限制值. 可用值: ‘unlimited’ 、0或者正整數. 默認值: 系統定義值.
chroot string
啟動時的Chroot目錄. 所定義的目錄需要是絕對路徑. 如果沒有設置, 則chroot不被使用.
chdir string
設置啟動目錄,啟動時會自動Chdir到該目錄. 所定義的目錄需要是絕對路徑. 默認值: 當前目錄,或者/目錄(chroot時).
catch_workers_output boolean
重定向運行過程中的stdout和stderr到主要的錯誤日志文件中. 如果沒有設置, stdout 和 stderr 將會根據FastCGI的規則被重定向到 /dev/null . 默認值: 空.
nginx配置
如果使用unix-socket來與nginx配合,nginx中的配置也要做相應的更改
1. 在ngnix配置的server_name下面聲明全局的fpm變量
[plain] view plaincopyprint?
server {
       listen 80 ;
       server_name xxx;
       #在這里插入,設置為全局變量
       set    $fpm_sock 'unix:/var/run/myapp/fpm.sock';
        ...
}  
server {     
       listen 80 ;     
       server_name xxx;    
       #在這里插入,設置為全局變量     
       set    $fpm_sock 'unix:/var/run/myapp/fpm.sock';
        ...
}
2. 然后改造所有 fastcgi_pass 字段
[plain] view plaincopyprint?
location = / {
include fastcgi_params;
root xxx/apps/fnt;
fastcgi_pass $fpm_sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME xxx/apps/fnt/index.php ;
}
location = / {     
     include        fastcgi_params;     
     root           xxx/apps/fnt;     
     fastcgi_pass   $fpm_sock;     
     fastcgi_index  index.php;     
     fastcgi_param  SCRIPT_FILENAME  xxx/apps/fnt/index.php ;      
}
fpm status
在nginx.conf里配置
[plain] view plaincopyprint?
server {
        listen       8360;
        server_name  localhost;  
        location /fpm_status {
                if ( $arg_sid = '' ) {
                        rewrite ^ /fpm_list last;
                }
                include fastcgi_params;
                fastcgi_pass unix:/var/run/$arg_sid/fpm.sock;
        }
        #列出所有正在運行的fpm-sock
        location /fpm_list {
                default_type text/html;
                content_by_lua "
                    ngx.print('<h2>Runing FPM instances:</h2>')
                    local cmd = [[/bin/ls -1 /var/run/ | grep rgapp- | awk '{print "<p><a href=?sid="$1">"$1"</a>"}']]
                    local f = io.popen(cmd, 'r')
                    local log = f:read('*a')
                    f:close()
                    ngx.print(log)
                 ";
         }
         ...
}  
server {
        listen       8360;
        server_name  localhost;
        location /fpm_status {
                if ( $arg_sid = '' ) {
                        rewrite ^ /fpm_list last;
                }
                include fastcgi_params;
                fastcgi_pass unix:/var/run/$arg_sid/fpm.sock;
        }
        #列出所有正在運行的fpm-sock
        location /fpm_list {
                default_type text/html;
                content_by_lua "
                    ngx.print('<h2>Runing FPM instances:</h2>')
                    local cmd = [[/bin/ls -1 /var/run/ | grep rgapp- | awk '{print "<p><a href=?sid="$1">"$1"</a>"}']]
                    local f = io.popen(cmd, 'r')
                    local log = f:read('*a')
                    f:close()
                    ngx.print(log)
                 ";
         }
         ...
}
因為應用池里面會有多個應用,所以通過sid參數來參看指定應用的fpm狀態。
通過地址http://xxx:8360/fpm_status?sid=myapp查看fpm狀態
總結
以上是生活随笔為你收集整理的php-fpm介绍及配置的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 常见git使用
- 下一篇: HTTP和HTTPS的了解
