LNMP架構四

php-fpm的pool(鏈接池)

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

[root@bogon linux.com]# vim /usr/local/php-fpm/etc/php-fpm.conf
[root@bogon linux.com]# 
[root@bogon linux.com]# ps aux|grep php
root       3068  0.0  0.0 227144   228 ?        Ss   2月11   0:10 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    3114  0.0  0.0 229228   124 ?        S    2月11   0:00 php-fpm: pool www

1.編輯php-fpm配置文件添加新pool命名爲linux.comphp

[linux.com]
listen = /tmp/linux.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

2.檢查php配置文件,從新加載配置文件,查看php進程發現有連個pool了css

爲何要創建多個池子呢?拿www和linux.com兩個池子來講,www池子最多能夠同時開50個進程,當www進程滿了就會報502錯誤,這個時候linux.com不受影響。html

[root@bogon linux.com]# /usr/local/php-fpm/sbin/php-fpm -t
[13-Feb-2018 10:28:14] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@bogon linux.com]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@bogon linux.com]# ps -aux|grep php-fpm
php-fpm   18117  0.0  0.5 229228  5076 ?        S    10:29   0:00 php-fpm: pool www
php-fpm   18118  0.0  0.5 229228  5076 ?        S    10:29   0:00 php-fpm: pool www
php-fpm   18119  0.0  0.5 229228  5076 ?        S    10:29   0:00 php-fpm: pool www
php-fpm   18120  0.0  0.5 229228  5072 ?        S    10:29   0:00 php-fpm: pool linux.com
php-fpm   18121  0.0  0.5 229228  5076 ?        S    10:29   0:00 php-fpm: pool linux.com
php-fpm   18122  0.0  0.5 229228  5076 ?        S    10:29   0:00 php-fpm: pool linux.com

3.新建pool,編輯aaa.com.conf配置文件添加支持php配置java

[root@bogon vhost]# ls
aaa.com.conf  load.conf  proxy.conf  ssl.conf  test.com.conf
[root@bogon vhost]# vim aaa.com.conf 
[root@bogon vhost]# 

  

server
{
    listen 80 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/linux.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
    }

}

4.查看nginx配置文件能夠看到有個include選項,php-fpm也是支持的linux

[root@bogon conf]# vim nginx.conf
[root@bogon conf]# 
    gzip_types text/plain application/x-javascript text/css text/htm
    application/xml;
    include vhost/*.conf;
}

5.編輯php-fpm.conf,在global標籤下添加include選項nginx

[root@bogon conf]# 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

6.將www配置和linux.com配置拆分開,在etc目錄下建立php-fpm.d目錄並建立兩個配置文件www.conf和linux.conf,在配置文件中分別將拆分出來的配置寫到裏面vim

[root@bogon conf]# cd /
[root@bogon /]# cd usr/local/php-fpm/etc/
[root@bogon etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php.ini
[root@bogon etc]# mkdir php-fpm.d
[root@bogon etc]# cd php-fpm.d/
[root@bogon php-fpm.d]# vi www.conf
[root@bogon php-fpm.d]# 
[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@bogon php-fpm.d]# vi linux.conf
[linux.com] listen = /tmp/linux.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

7.刪除php-fpm.conf配置文件中的www和linux.com配置瀏覽器

8.檢查配置,重啓php-fpm everyone ok架構

[root@bogon php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[13-Feb-2018 11:39:14] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@bogon php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@bogon php-fpm.d]# 

9.檢查服務是否啓動,發現一切ok

[root@bogon php-fpm.d]# ps -aux|grep php-fpm           
root      23264  1.8  0.4 227232  4984 ?        Ss   11:39   0:01 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   23284  0.1  0.5 229256  5080 ?        S    11:40   0:00 php-fpm: pool linux.com
php-fpm   23285  0.0  0.5 229256  5080 ?        S    11:40   0:00 php-fpm: pool linux.com
php-fpm   23286  0.0  0.5 229256  5076 ?        S    11:40   0:00 php-fpm: pool www
php-fpm   23287  0.0  0.5 229256  5076 ?        S    11:40   0:00 php-fpm: pool www

php-fpm慢執行日誌

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

1.針對www作實驗,編輯www的配置文件,添加配置

[root@bogon php-fpm.d]# vim www.conf 
[root@bogon php-fpm.d]# 
request_slowlog_timeout = 1                             (鏈接超過1秒則記錄日誌)
slowlog = /usr/local/php-fpm/var/log/www-slow.log               (日誌存放地址)

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

[root@bogon php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[13-Feb-2018 13:34:07] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

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

3.查看是否生成了www-slow.log

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

4.查看www.conf 中的php-fcgi.sock被誰用着,可見是被test.com.conf

[root@bogon /]# ls /usr/local/php-fpm/etc/php-fpm.conf        
/usr/local/php-fpm/etc/php-fpm.conf
[root@bogon /]# ls /usr/local/nginx/conf/vhost/test.com.conf    
/usr/local/nginx/conf/vhost/test.com.conf
[root@bogon /]#

 

文件  /usr/local/nginx/conf/vhost/test.com.conf 
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; }

 

文件 /usr/local/php-fpm/etc/php-fpm.d/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 request_slowlog_timeout = 1 slowlog = /usr/local/php-fpm/var/log/www-slow.log

5.建立sleep.php文件寫入sleep,休眠兩秒鐘

[root@bogon local]# vim /data/wwwroot/test.com/sleep.php
[root@bogon local]# 
<?php
echo "test slow log";
sleep(2);
echo "done";

6.測試成功

[root@bogon local]# curl -x127.0.0.1:80 test.com/sleep.php 
test slow logdone[root@bogon local]# 

7.在頁面查看php報錯信息能夠開啓php配置文件中的display_errors = On

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

8.查看慢日誌

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

[13-Feb-2018 14:14:50]  [pool www] pid 31212
script_filename = /data/wwwroot/test.com/sleep.php
[0x00007f0a9bf742f8] sleep() /data/wwwroot/test.com/sleep.php:3
[root@bogon local]# 

open_basedir(將 PHP 所能打開的文件限制在指定的目錄樹,包括文件自己。

1.編輯test的配置文件添加配置內容,故意將tese.com寫成111.test.com爲了測試報錯的狀況

[root@bogon local]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf 
[root@bogon local]# 

  

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

2.編輯php配置文件,定義php-fpm的錯誤日誌

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

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

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

error_reporting = E_ALL (定義日誌級別,記錄全部錯誤日誌,默認爲 ;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT) 

3.查看error_log 配置路徑 

[root@bogon etc]# grep error_log /usr/local/php-fpm/etc/php.ini 
; server-specific log, STDERR, or a location specified by the error_log
; Set maximum length of log_errors. In error_log information about the source is
;error_log = php_errors.log
;error_log = syslog
error_log = /usr/local/php-fpm/var/log/php_errors.log
; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=
[root@bogon etc]# 

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

[root@bogon etc]# touch /usr/local/php-fpm/var/log/php_errors.log     
[root@bogon etc]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log 
[root@bogon etc]# /etc/init.d/php-fpm restart                   
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@bogon etc]# 

5.訪問報404錯誤

[root@bogon etc]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Tue, 13 Feb 2018 07:38:46 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

[root@bogon etc]# 

6.查看錯誤日誌(意思是訪問的文件不在open_basedir限制的目錄)

[root@bogon etc]# tail -2 /usr/local/php-fpm/var/log/php_errors.log   
[13-Feb-2018 07:38:46 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/111test.com:/tmp/)
in Unknown on line 0 [13-Feb-2018 07:38:46 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0 [root@bogon etc]#

7.修改配置文件改爲正確的目錄名,編輯配置文件,將111test.com改成test.com

[root@bogon etc]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf 
[root@bogon etc]#

8.再次重啓php-fpm,測試成功

[root@bogon etc]# /etc/init.d/php-fpm restart                        
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@bogon etc]# curl -x127.0.0.1:80 test.com/sleep.php -I          
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 13 Feb 2018 07:49:46 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

[root@bogon etc]# 

php-fpm進程管理

1.打開一個php-fpm的配置文件。

[root@bogon etc]# vim php-fpm.d/www.conf 
[root@bogon etc]# 
[www]
listen = /tmp/php-fcgi.sock
;listen = 127.0.0.1:9000          
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/test.com:/tmp/
相關文章
相關標籤/搜索