1. 程式人生 > >php-fpm的pool,慢執行日誌,定義open_basedir,程序管理

php-fpm的pool,慢執行日誌,定義open_basedir,程序管理

php-fpm的pool

主要用來隔離各個站點,相互不影響。當某個網站出問題時,不影響到其他的站點。
增加池子配置,例如增加 test.com

[root@test-a etc]# cat php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[test.com]
listen = /tmp/test-com.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

[root@test-a etc]# ../sbin/php-fpm -t
[03-Dec-2018 15:57:20] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm                                          .conf test is successful
[root@test-a etc]# /etc/init.d/php-fpm reload
Reload service php-fpm  done

[root@test-a etc]# ps aux|grep php-fpm
root      3108  1.4  0.4 228216  4908 ?        Ss   15:58   0:00 php-fpm: master process (/usr/loca                       l/php-fpm/etc/php-fpm.conf)
php-fpm   3109  0.0  0.4 228156  4652 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3110  0.0  0.4 228156  4652 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3111  0.0  0.4 228156  4652 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3112  0.0  0.4 228156  4656 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3113  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3114  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3115  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3116  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3117  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3118  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3119  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3120  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3121  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3122  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3123  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3124  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3125  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3126  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3127  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3128  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool www
php-fpm   3129  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3130  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3131  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3132  0.0  0.4 228156  4660 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3133  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3134  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3135  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3136  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3137  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3138  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3139  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3140  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3141  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3142  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3143  0.0  0.4 228156  4664 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3144  0.0  0.4 228156  4668 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3145  0.0  0.4 228156  4668 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3146  0.0  0.4 228156  4668 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3147  0.0  0.4 228156  4668 ?        S    15:58   0:00 php-fpm: pool test.com
php-fpm   3148  0.0  0.4 228156  4668 ?        S    15:58   0:00 php-fpm: pool test.com

# 這樣站點(test.com)就可以獨自使用自己的pool,對應的socket配置為/tmp/test-com.sock

上面是在php-fpm全域性配置檔案進行配置,現在拆分配置,針對每個pool進行配置

[root@test-a etc]# mkdir php-fpm.d
[root@test-a etc]# cd php-fpm.d/
[root@test-a php-fpm.d]# vi www.conf
[root@test-a php-fpm.d]# cat www.conf
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[root@test-a php-fpm.d]# vi test.conf
[root@test-a php-fpm.d]# cat test.conf
[test.com]
listen = /tmp/test-com.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[root@test-a php-fpm.d]# vi ../php-fpm.conf
[root@test-a php-fpm.d]# cat ../php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
include = etc/php-fpm.d/*.conf
[root@test-a php-fpm.d]# ../../sbin/php-fpm -t
[03-Dec-2018 16:10:01] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@test-a php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@test-a php-fpm.d]# ps aux|grep php-fpm
root      3283  0.1  0.4 228244  4912 ?        Ss   16:10   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   3284  0.0  0.4 228184  4664 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3285  0.0  0.4 228184  4664 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3286  0.0  0.4 228184  4664 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3287  0.0  0.4 228184  4668 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3288  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3289  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3290  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3291  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3292  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3293  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3294  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3295  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3296  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3297  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3298  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3299  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3300  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3301  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3302  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3303  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool test.com
php-fpm   3304  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3305  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3306  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3307  0.0  0.4 228184  4672 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3308  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3309  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3310  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3311  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3312  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3313  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3314  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3315  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3316  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3317  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3318  0.0  0.4 228184  4676 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3319  0.0  0.4 228184  4680 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3320  0.0  0.4 228184  4680 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3321  0.0  0.4 228184  4680 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3322  0.0  0.4 228184  4680 ?        S    16:10   0:00 php-fpm: pool www
php-fpm   3323  0.0  0.4 228184  4680 ?        S    16:10   0:00 php-fpm: pool www

php-fpm的慢執行日誌

增加配置
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log

[root@test-a php-fpm.d]# vim www.conf
[root@test-a php-fpm.d]# cat www.conf
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log

[root@test-a php-fpm.d]# ../../sbin/php-fpm -t
[03-Dec-2018 16:15:34] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@test-a php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done

測試

[root@test-a php-fpm.d]# vim /data/wwwroot/abc.com/2.php
[root@test-a php-fpm.d]# cat /data/wwwroot/abc.com/2.php
<?php
sleep(3);
?>

[root@test-a php-fpm.d]# curl -x127.0.0.1:80 abc.com/2.php -I
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Mon, 03 Dec 2018 08:21:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32

[root@test-a php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log

[03-Dec-2018 16:21:48]  [pool www] pid 3373
script_filename = /data/wwwroot/abc.com/2.php
[0x00007f68a91ff270] sleep() /data/wwwroot/abc.com/2.php:2

定義open_basedir

[root@test-a php-fpm.d]# vim www.conf
[root@test-a php-fpm.d]# cat www.conf #先配置錯誤
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
[root@test-a php-fpm.d]# ../../sbin/php-fpm -t
[03-Dec-2018 16:27:23] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@test-a php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@test-a php-fpm.d]# curl -x127.0.0.1:80 abc.com/index.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.1
Date: Mon, 03 Dec 2018 08:27:44 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32

# 正確配置,測試
[root@test-a php-fpm.d]# vim www.conf
[root@test-a php-fpm.d]# cat www.conf
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/abc.com:/tmp/
[root@test-a php-fpm.d]# ../../sbin/php-fpm -t
[03-Dec-2018 16:29:00] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@test-a php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@test-a php-fpm.d]# curl -x127.0.0.1:80 abc.com/index.php -I
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Mon, 03 Dec 2018 08:29:06 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32

程序管理

  • pm = dynamic ;動態程序管理,也可以是static
  • pm.max_children = 50 ;最大子程序數,ps aux可以檢視
  • pm.start_servers = 20 ;啟動服務時會啟動的程序數
  • pm.min_spare_servers = 5 ;定義在空閒時段,子程序數的最少數量,如果達到這個數值時,php-fpm服務會自動派生新的子程序。
  • pm.max_spare_servers = 35 ;定義在空閒時段,子程序數的最大值,如果高於這個數值就開始清理空閒的子程序。
  • pm.max_requests = 500 ;定義一個子程序最多處理的請求數,也就是說在一個php-fpm的子程序最多可以處理這麼多請求,當達到這個數值時,它會自動退出。

測試

[root@test-a php-fpm.d]# vim test.conf
[root@test-a php-fpm.d]# cat test.conf
[test.com]
listen = /tmp/test-com.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 3
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500
rlimit_files = 1024

[root@test-a php-fpm.d]# ../../sbin/php-fpm -t
[03-Dec-2018 16:33:17] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@test-a php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@test-a php-fpm.d]# ps aux|grep php-fpm #可以看到只有2個test.com
root      3734  0.1  0.4 228188  4876 ?        Ss   16:33   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   3735  0.0  0.4 228128  4656 ?        S    16:33   0:00 php-fpm: pool test.com
php-fpm   3736  0.0  0.4 228128  4656 ?        S    16:33   0:00 php-fpm: pool test.com
php-fpm   3737  0.0  0.4 228184  4656 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3738  0.0  0.4 228184  4660 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3739  0.0  0.4 228184  4660 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3740  0.0  0.4 228184  4660 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3741  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3742  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3743  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3744  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3745  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3746  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3747  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3748  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3749  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3750  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3751  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3752  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3753  0.0  0.4 228184  4664 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3754  0.0  0.4 228184  4668 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3755  0.0  0.4 228184  4668 ?        S    16:33   0:00 php-fpm: pool www
php-fpm   3756  0.0  0.4 228184  4668 ?        S    16:33   0:00 php-fpm: pool www