php-fpm的pool

  • 为了避免因多站点使用同一个pool时,如果一个站点访问量过大导致资源耗尽出现问题,进而影响使用同一个pool的其他站点的正常运行,就需要对每个站点配置一个单独的pool。实现方式和nginx的vhost差不多
  • 编辑主配置文件
    [root@akuilinux01 aming.com]# vim /usr/local/php-fpm/etc/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
  • 重建多个pool
    [root@akuilinux01 aming.com]# mkdir /usr/local/php-fpm/etc/php-fpm.d
    [root@akuilinux01 aming.com]# cd !$
    cd /usr/local/php-fpm/etc/php-fpm.d
    [root@akuilinux01 php-fpm.d]# vim www.conf
    [www]
    listen = /tmp/php-fcgi.sock
    #listen = 127.0.0.1:9000
    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@akuilinux01 php-fpm.d]# vim aming.conf
    [aming]
    listen = /tmp/aming.sock
    #listen = 127.0.0.1:9000
    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@akuilinux01 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
    [18-Jun-2018 23:31:06] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
    [root@akuilinux01 php-fpm.d]# /etc/init.d/php-fpm restart
    Gracefully shutting down php-fpm . done
    Starting php-fpm  done
  • 查看
    [root@akuilinux01 php-fpm.d]# ps aux |grep php-fpm
    root      5306  0.2  0.2 227292  4984 ?        Ss   23:31   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
    php-fpm   5307  0.0  0.2 227232  4724 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5308  0.0  0.2 227232  4724 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5309  0.0  0.2 227232  4724 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5310  0.0  0.2 227232  4724 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5311  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5312  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5313  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5314  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5315  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5316  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5317  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5318  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5319  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5320  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5321  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5322  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5323  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5324  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5325  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5326  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool aming
    php-fpm   5327  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5328  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5329  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5330  0.0  0.2 227232  4728 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5331  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5332  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5333  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5334  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5335  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5336  0.0  0.2 227232  4732 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5337  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5338  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5339  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5340  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5341  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5342  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5343  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5344  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5345  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    php-fpm   5346  0.0  0.2 227232  4736 ?        S    23:31   0:00 php-fpm: pool www
    root      5350  0.0  0.0 112680   984 pts/1    S+   23:32   0:00 grep --color=auto php-fpm

    PHP慢执行日志

  • 配置文件中加入下面两行
    [root@akuilinux01 ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
    request_slowlog_timeout = 1
    #当请求超过1秒开始记录日志
    slowlog = /usr/local/php-fpm/var/log/www-slow.log
    #日志存放地址
  • 测试
    [root@akuilinux01 ~]# vim /data/wwwroot/test.com/sleep.php
    <?php
    echo "test slow log";
    sleep(2);
    echo "done";
    ?>
    [root@akuilinux01 ~]# curl -x127.0.0.1:80 test.com/sleep.php
    test slow logdone
    [root@akuilinux01 ~]# cat /usr/local/php-fpm/var/log/www-slow.log 
    [19-Jun-2018 08:58:20]  [pool www] pid 1451
    script_filename = /data/wwwroot/test.com/sleep.php
    [0x00007f4cadc922e0] sleep() /data/wwwroot/test.com/sleep.php:3

    open_basedir

  • 当一台服务器跑多个站点时,可以使用openbasedir限定各个站点所能访问的服务器上的目录的范围,就需要每个pool设定open basedir
    [root@akuilinux01 ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
    php_admin_value[open_basedir]=/data/wwwroot/eetest.com:/tmp/
    #故意写错/data/wwwroot/eetest.com,正确是/data/wwwroot/test.com
  • 进行测试
    [root@akuilinux01 ~]# curl -x127.0.0.1:80 test.com/sleep.php -I
    HTTP/1.1 404 Not Found
    Server: nginx/1.14.0
    Date: Tue, 19 Jun 2018 01:21:13 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    X-Powered-By: PHP/5.6.30
  • 再次更改www.conf,修改路径,再次测试
    [root@akuilinux01 ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf 
    [root@akuilinux01 ~]# /etc/init.d/php-fpm restart
    Gracefully shutting down php-fpm . done
    Starting php-fpm  done
    [root@akuilinux01 ~]# curl -x127.0.0.1:80 test.com/sleep.php
    test slow logdone
  • 配置错误日志
    [root@akuilinux01 ~]# vim /usr/local/php-fpm/etc/php.ini
    error_reporting = E_ALL
    display_errors = Off
    ;error_log = syslog
    error_log = /usr/local/php-fpm/var/log/php_errors.log
    [root@akuilinux01 ~]# touch /usr/local/php-fpm/var/log/php_errors.log
    [root@akuilinux01 ~]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log
    [root@akuilinux01 ~]# /etc/init.d/php-fpm restart
    Gracefully shutting down php-fpm . done
    Starting php-fpm  done
  • 再次测试,测试前故意改错
    [root@akuilinux01 ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
    php_admin_value[open_basedir]=/data/wwwroot/eetest.com:/tmp/
    #故意写错/data/wwwroot/eetest.com,正确是/data/wwwroot/test.com
    [root@akuilinux01 ~]# curl -x127.0.0.1:80 test.com/sleep.php -I
    HTTP/1.1 404 Not Found
    Server: nginx/1.14.0
    Date: Tue, 19 Jun 2018 01:34:14 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    X-Powered-By: PHP/5.6.30
  • 查看日志
    [root@akuilinux01 ~]# cat /usr/local/php-fpm/var/log/php_errors.log
    [19-Jun-2018 01:32:53 UTC] PHP Deprecated:  Comments starting with '#' are deprecated in Unknown on line 1 in Unknown on line 0
    [19-Jun-2018 01:32:53 UTC] PHP Deprecated:  Comments starting with '#' are deprecated in Unknown on line 1 in Unknown on line 0
    [19-Jun-2018 01:34:14 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/sleep.php) is not within the allowed path(s): (/data/wwwroot/eetest.com:/tmp/) in Unknown on line 0
    [19-Jun-2018 01:34:14 UTC] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0

    php-fpm进程管理

    [root@akuilinux01 ~]# cat /usr/local/php-fpm/etc/php-fpm.d/www.conf
    [www]
    listen = /tmp/www.sock
    ;listen = 127.0.0.1:9000
    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/
  • pm = dynamic //动态进程管理,也可以是static,这时下面的配置不生效启动就生成50个子进程不会改变
  • 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的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出