LNMP(php-fpm的pool,慢執行日誌,定義open_bashdir,php-fpm進程管理

1、php-fpm的pool(鏈接池)php

咱們查看php的進程時,會發現,在最後一個pool的選項,而這個就是咱們在php-fpm配置文件裏寫的一個鏈接池。html

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

root      2158  0.0  0.4 227148  4952 ?        Ss   15:57   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)vim

php-fpm   2164  0.0  0.4 227148  4708 ?        S    15:57   0:00 php-fpm: pool www瀏覽器

php-fpm   2165  0.0  0.4 227148  4708 ?        S    15:57   0:00 php-fpm: pool wwwbash

php-fpm   2166  0.0  0.4 227148  4708 ?        S    15:57   0:00 php-fpm: pool www服務器


當多個服務器使用同一池子時,一個鏈接池的資源可能耗盡時,可能別的服務器鏈接時報錯502,這個時候就須要創建多個鏈接池供不一樣的服務器使用,每一個鏈接池監聽不通的sock,或者tcp/ip。架構

咱們能夠學nginx的虛擬主機同樣,將鏈接池從主配置文件裏拿出來,放到一個目錄裏,並將他們分開,方便管理app

配置以下:運維

1.在配置文件裏[global]加下面一行,並將以前的鏈接池刪掉。

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

include = etc/php-fpm.d/*.conf

2.建立鏈接池文件

[root@lnmp ~]# mkdir /usr/local/php-fpm/etc/php-fpm.d

[root@lnmp ~]# cd /usr/local/php-fpm/etc/php-fpm.d

[root@lnmp php-fpm.d]# vim www.conf                    (將以前在配置文件裏刪除的鏈接池寫到這裏)

[www]

listen = /tmp/www.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.添加新的鏈接池 (注意鏈接池的名字不要同樣,監聽的sock和tcp/ip不同,而且要到nginx的虛擬配置文件裏修改php要監聽的sock)           

[root@lnmp ~]# vim /usr/local/php-fpm/etc/php-fpm.d/lty.conf

[lty]

listen = /tmp/lty.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@lnmp ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf 

server

{

location ~ \.php$

    {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/lty.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;

    }


    listen 80 default_server;

    server_name aaa.com;

    index index.html index.htm index.php;

    root /data/wwwroot/default;

}

4.檢查php配置文件以及從新啓動php-fpm

[root@lnmp ~]# /usr/local/php-fpm/sbin/php-fpm -t

[18-Dec-2017 17:56:11] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@lnmp ~]# /etc/init.d/php-fpm restart

Gracefully shutting down php-fpm . done

Starting php-fpm  done


5.ps aux查看進程        (由於太長的緣故,我只複製鏈接池的前2行)

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

root      2779  1.0  0.4 227236  4980 ?        Ss   17:56   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)

php-fpm   2780  0.0  0.4 227176  4716 ?        S    17:56   0:00 php-fpm: pool lty

php-fpm   2781  0.0  0.4 227176  4716 ?        S    17:56   0:00 php-fpm: pool lty

..........................................................................................

php-fpm   2818  0.0  0.4 227176  4728 ?        S    17:56   0:00 php-fpm: pool www

php-fpm   2819  0.0  0.4 227176  4728 ?        S    17:56   0:00 php-fpm: pool www

root      2821  0.0  0.0 112684   972 pts/0    S+   17:56   0:00 grep --color=auto php-fpm


2、php-fpm慢執行日誌

若是要作一個php網站,會推薦你用lnmp架構,緣由就是能夠分析php-fpm慢執行日誌。由於在作運維工程師的生涯中,老闆或者客戶會常常反饋用戶網站訪問慢,慢執行日誌則會記錄爲何網站訪問會慢。對運維工做是一個很大的幫助

配置:

  1. 首先在php的配置文件裏修改          (上一次咱們講鏈接池的時候已經將不一樣的池分開,以修改www.conf爲例)

[root@lnmp ~]# 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               (日誌存放地址)


2.檢查配置文件語法錯誤已經重啓服務。

[root@lnmp ~]# /usr/local/php-fpm/sbin/php-fpm -t

[18-Dec-2017 18:11:25] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@lnmp ~]# /etc/init.d/php-fpm restart

Gracefully shutting down php-fpm . done

Starting php-fpm  done

3.配置主機test.com,讓它使用的是www鏈接池

[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~ \.php$

    {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/www.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;

    }


[root@lnmp ~]# vim /data/wwwroot/test.com/sleep.conf

<?php

echo "test slow log";

sleep(2);

echo "done";

?>


4.檢查語法錯誤,而且從新加載nginx

[root@lnmp test.com]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful

[root@lnmp test.com]# /usr/local/nginx/sbin/nginx  -s reload


5.咱們在test.com的頁面裏寫等待鏈接,以便觀察結果

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

test slow logdone


[root@lnmp test.com]# cat /usr/local/php-fpm/var/log/www-slow.log  (查看日誌)

[18-Dec-2017 19:15:13]  [pool www] pid 3344

script_filename = /data/wwwroot/test.com/sleep.php         (這裏顯示那個文件會訪問慢)

[0x00007f1ae63661f8] sleep() /data/wwwroot/test.com/sleep.php:3   (這裏顯示文件第幾行慢)


3、php-fpm定義open_bashdir (將 PHP 所能打開的文件限制在指定的目錄樹,包括文件自己。

[root@lnmp ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf    (修改配置文件添加如下一列)

php_admin_value[open_basedir]=/data/wwwroot/wwtest.com:/tmp/      (爲了方便後期效果,把地址寫成錯誤的)


定義php-fpm的錯誤日誌

[root@lnmp ~]# vim /usr/local/php-fpm/etc/php.ini 

446:   display_errors = Off     (這一行的意思是將你的錯誤信息是否顯示到瀏覽器上,不能讓用戶看到,因此通常是off)

575:  error_log = /usr/local/php-fpm/var/log/php_errors.log (添加一行,定義記錄錯誤日誌的路徑。)

449:    error_reporting = E_ALL     (定義日誌級別,記錄全部錯誤日誌)


建立錯誤日誌文件,修改權限,防止不能正常寫入。

[root@lnmp ~]# touch /usr/local/php-fpm/var/log/php_errors.log

[root@lnmp ~]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log


重啓php配置文件而且訪問,查看錯誤日誌

[root@lnmp ~]# /etc/init.d/php-fpm restart

Gracefully shutting down php-fpm .^[[A done

Starting php-fpm  done


[root@lnmp ~]# curl -x127.0.0.1:80 test.com/1.php -I          (報錯404)

HTTP/1.1 404 Not Found

Server: nginx/1.8.0

Date: Mon, 18 Dec 2017 12:35:09 GMT

Content-Type: text/html; charset=UTF-8

Connection: keep-alive

X-Powered-By: PHP/5.6.30


[root@lnmp ~]# cat /usr/local/php-fpm/var/log/php_errors.log    (查看錯誤日誌)

[18-Dec-2017 12:33:51 UTC] PHP Warning:  phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /data/wwwroot/test.com/1.php on line 2

[18-Dec-2017 12:34:11 UTC] PHP Warning:  phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /data/wwwroot/test.com/1.php on line 2

[18-Dec-2017 12:35:09 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/1.php) is not within the allowed path(s): (/data/wwwroot/wwtest.com:/tmp/) in Unknown on line 0

[18-Dec-2017 12:35:09 UTC] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0


4、php-fpm進程管理

咱們打開一個php-fpm的配置文件。

[root@lnmp ~]# cat /usr/local/php-fpm/etc/php-fpm.d/www.conf 

[www]

listen = /tmp/www.sock

listen.mode=666

user = php-fpm

group = php-fpm

pm = dynamic  (進程以動態的形式啓動,如下是他的配置,能夠用靜態static,那下面只有pm.max_children一行生效,一直保持50個子進程)

pm.max_children = 50           (最大子進程數)

pm.start_servers = 20           (一開始啓動20個子進程)

pm.min_spare_servers = 5         (定義空閒時段,最低不能低於5個子進程,若是達不到,會自動派生新的子進程)

pm.max_spare_servers = 35         (定義空閒時段,最大值不能超過35個,若是高於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/wwtest.com:/tmp/

相關文章
相關標籤/搜索