php-fpm的pool、php-fpm慢執行日誌、open_basedir、php-fpm程式管理

科技小能手發表於2017-11-12

php-fpm的pool

當很多php站點共用一個池的時候,若是其中一個站點影響了這個pool,會導致其他站點也出現問題,為避免這個問題發生,需要對每個站點新建一個pool,進行隔離,保障服務安全


1、[root@centos7 ~]# vim /usr/local/php-fpm/etc/php-fpm.conf  

新增:

[test.com]

listen = /tmp/test.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


2、[root@centos7 ~]# /usr/local/php-fpm/sbin/php-fpm -t

3、[root@centos7 ~]# /etc/init.d/php-fpm reload

4、[root@centos7 ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf

5、新增如下:

[root@centos7 ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf.bak 


server

{

    listen 80 default_server;

    #有`default_server`標記的就是預設虛擬主機

    server_name aaa.com;

    index index.html index.htm index.php;

    root /data/wwwroot/default;

    location ~ .php$

    {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/test.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;

    }

}

6、[root@centos7 ~]# /etc/init.d/nginx reload

7、可以看出有兩個pool了

[root@centos7 ~]# ps aux |grep php-fpm

php-fpm    8403  0.0  0.2 227144  4808 ?        S    17:22   0:00 php-fpm: pool www

php-fpm    8423  0.0  0.2 227144  4812 ?        S    17:22   0:00 php-fpm: pool test.com


新增php-fpm.conf子配置檔案

為了方便管理php-fpm.conf上配置檔案,類似於nginx上的虛擬主機conf,也可以分開填寫檔案

1、

[root@centos7 php-fpm.d]# vi /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 = /usr/local/php-fpm.d/*.conf

2、

[root@centos7 php-fpm.d]# vim /usr/local/php-fpm.d/test.conf


[test.com]

listen = /tmp/test.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

3、

[root@centos7 php-fpm.d]# vim /usr/local/php-fpm.d/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

4、

[root@centos7 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t

5、

[root@centos7 php-fpm.d]# /etc/init.d/php-fpm reload

驗證:是否有2個pool

[root@centos7 php-fpm.d]# ps -ef | grep php-fpm


php-fpm慢執行日誌

當php訪問變慢時,可以通過慢日誌來檢視是哪個php指令碼導致,類似mysql的慢日誌

1、新增內容

[root@centos7 php-fpm.d]# vim /usr/local/php-fpm.d/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

#請求超過1秒就記錄到日誌裡

slowlog = /usr/local/php-fpm/var/log/www-slow.log

2、實驗指令碼編寫

[root@centos7 php-fpm.d]# vim /data/wwwroot/test.com/sleep.php

#輸出test slow log,sleep2秒,結束

<?php

echo “test slow log”;

sleep(2);

echo “done”;

?>


[root@centos7 php-fpm.d]# /etc/init.d/php-fpm reload


3、驗證:

[root@centos7 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php 

test slow logdone[root@cen

[root@centos7 php-fpm.d]# tail /usr/local/php-fpm/var/log/www-slow.log 

#記錄了日誌

[16-Nov-2017 17:41:59]  [pool www] pid 8669

script_filename = /data/wwwroot/test.com/sleep.php

[0x00007f15620aa2e8] sleep() /data/wwwroot/test.com/sleep.php:3


open_basedir設定

當一臺伺服器跑多個站點時,使用open_basedir限定各個站點所能訪問的伺服器上的目錄的範圍。在php-fpm服務中,可以針對每個pool設定open _ basedir。

1、新增內容

[root@centos7 php-fpm.d]# vim /usr/local/php-fpm.d/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

#請求超過1秒就記錄到日誌裡

slowlog = /usr/local/php-fpm/var/log/www-slow.log

php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/

2、

[root@centos7 php-fpm.d]# vim /data/wwwroot/test.com/1.php


<?php

echo “This is  open_basedir”;

3、驗證:

[root@centos7 php-fpm.d]# /etc/init.d/php-fpm reload

Reload service php-fpm  done

[root@centos7 php-fpm.d]# curl -x127.0.0.1:80 test.com/1.php

This is  open_basedir


php-fpm程式管理

php-fpm中pool配置引數解析:

[root@adailinux php-fpm.d]# vim www.conf

[www]

listen = /tmp/php-fcgi.sock

listen.mode = 666

user = php-fpm

group = php-fpm

pm = dynamic

;定義程式啟動方式(dynamic表示動態,static表示靜態)

;只有此處設定為dynamic,下面的配置才生效,若是選擇靜態,會預先生產設定的程式,不會動態變化

pm.max_children = 50

;最多可啟動的子程式數量

pm.start_servers = 20

;設定初始啟動的程式數量

pm.min_spare_servers = 5

;表示php-fpm空閒時最少要有幾個子程式

pm.max_spare_servers = 35

;表示php-fpm空閒時最多要有幾個子程式

pm.max_requests = 500

;表示一個子程式最多可接受多少個請求

rlimit_files = 1024

;表示每個子程式開啟的多少個檔案控制程式碼

request_slowlog_timeout = 1

;當請求超過1秒開始記錄日誌

slowlog = /usr/local/php-fpm/var/log/www-slow.log

;日誌存放地址

php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/

本文轉自 jiekegz  51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/1982498


相關文章