1. 程式人生 > >LNMP(php-fpm的pool,慢執行日誌,定義open_bashdir,php-fpm進程管理

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

max 連接池 per hit sele files 註意 配置文件 strong

一、php-fpm的pool(連接池)

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

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

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)

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 www

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


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

我們可以學nginx的虛擬主機一樣,將連接池從主配置文件裏拿出來,放到一個目錄裏,並將他們分開,方便管理

配置如下:

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


二、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 (這裏顯示文件第幾行慢)


三、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


四、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/


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