php-fpm的pool慢執行日誌 ,open_basedir,php-fpm進程管理

6月13日任務

12.21 php-fpm的pool
12.22 php-fpm慢執行日誌
12.23 open_basedir
12.24 php-fpm進程管理javascript

php-fpm的pool目錄概要

  • vim /usr/local/php/etc/php-fpm.conf//在[global]部分增長
  • include = etc/php-fpm.d/*.conf
  • mkdir /usr/local/php/etc/php-fpm.d/
  • cd /usr/local/php/etc/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
  • 繼續編輯配置文件
  • vim aming.conf //內容以下
[aming]
listen = /tmp/aming.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
  • /usr/local/php/sbin/php-fpm –t
  • /etc/init.d/php-fpm restart

php-fpm的pool

  • php-fpm有一個概念,叫作pool。 使用ps aux |grep php-fpm最右側看到的那一列就是pool,也就是它的池子
[root@yong-01 ~]# ps aux |grep php-fpm
root      1489  0.0  0.2 227204  4952 ?        Ss   21:19   0:00 php-fpm: master process (/usr/localphp-fpm/etc/php-fpm.conf)
php-fpm   1490  0.0  0.2 227204  4708 ?        S    21:19   0:00 php-fpm: pool www
php-fpm   1491  0.0  0.2 227204  4708 ?        S    21:19   0:00 php-fpm: pool www
php-fpm   1492  0.0  0.2 227204  4708 ?        S    21:19   0:00 php-fpm: pool www
  • 切換目錄,會看到咱們只定義了一個www的pool
[root@yong-01 ~]# cd /usr/local/php-fpm/etc/
[root@yong-01 etc]# cat 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
[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
  • 實際上是支持定義多個池子的,每個池子均可以監聽不一樣的sock或者TCP/IP,nginx若是有多個站點,那麼每一個站點都能去使用一個獨立的pool,這樣作的優點就是,當其中一個php發生502了(502頗有多是資源不夠了),若是全部的網站使用了同一個池子,那其中一個網站發生了故障(好比寫的程序有問題),那麼就會把你的php資源耗盡,這樣就會形成其餘的站點也會502,因此有必要把每個站點隔離開來,每個站點使用獨立的pool
  • 設置多個pool,方法一以下
  • 在/usr/local/php-fpm/etc/php-fpm裏面,繼續增長pool
    • 寫第二個pool,名稱爲yueyong.com,listen = /tmp/yueyong.sock
    • 配置中的其餘信息可根據本身的需求來配置

  • 檢查是否存在語法錯誤
[root@yong-01 etc]# /usr/local/php-fpm/sbin/php-fpm -t
[13-Jun-2018 21:34:08] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
  • 若沒有語法錯誤,作一個php-fpm重啓
    • /etc/init.d/php-fpm restart 或使用 /etc/init.d/php-fpm reload
[root@yong-01 etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
  • 這時再來ps-aux|grep fpm查看,會看到最右側多了一個yueyong.com,這個就是兩個不一樣的池子

  • 在nginx中使用新的pool
[root@yong-01 etc]# cd /usr/local/nginx/conf/vhost/
[root@yong-01 vhost]# ls
aaa.com.conf  load.conf  proxy.conf  ssl.conf  test.com.conf
  • 能夠把test.com.conf定義成fastcgi_pass unix:/tmp/php-fcgi.sock;
    • 這是www池子中的php-fcgi.sock
[root@yong-01 vhost]# vim test.com.conf 

server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
    #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    #{
    #      expires      7d;
    #      access_log off;
    #}
    location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
    {
          expires 7d;
          valid_referers none blocked server_names  *.test.com ;
          if ($invalid_referer) {
             return 403;
          }
          access_log off;
    }

    location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }
    location /admin/
    {
    allow 192.168.180.134;
    allow 127.0.0.1;
    deny all;
    }
    location ~ .*(upload|image)/.*\.php$
    {
        deny all;
    }
    if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')
    {
      return 403;
    }
    location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    }
    access_log /tmp/test.com.log combined_realip;
}
  • 再把另外一個aaa.com.conf中 fastcgi_pass unix:/tmp/yueyong.sock;
    • 這是yueyong.com池子中的yueyong.sock
    • 路徑/data/wwwroot/default$fastcgi_script_name;
[root@yong-01 vhost]# vim aaa.com.conf 

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/yueyong.sock;  
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
    }
  • 這樣這兩個站點就隔離開了
  • 假設如今test.com請求量很大,最終將進程耗盡了,好比php-fpm耗盡了,這時候又有其餘請求來了,就會報502,由於沒有多餘的php給進程提供服務了,而另外一個站點則不受影響,由於使用的是另外一個池子,另外一個pool,這就是定義多個池子的緣由

方法二

  • 在vhost目錄下是多個.conf文件,在nginx.conf裏面加了一條 include vhost/*.conf;
[root@yong-01 vhost]# ls
aaa.com.conf  load.conf  proxy.conf  ssl.conf  test.com.conf
[root@yong-01 vhost]# tail /usr/local/nginx/conf/nginx.conf
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    include vhost/*.conf;
}
  • 在php-fpm一樣是支持 include vhost/*.conf;
  • 在主配置文件php-fpm中的 [global] 加入include = etc/php-fpm.d/*.conf ,並將池子拆分出來,將其中的池子pool刪除
    • 快速刪除內容方法,100dd就會刪除後面的100行
[root@yong-01 conf]# cat /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
[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

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

最終以下php

[root@yong-01 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
  • 由於include = etc/php-fpm.d/,因此須要建立/php-fpm.d/的目錄
[root@yong-01 etc]# mkdir php-fpm.d
[root@yong-01 etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d  php.ini
  • 切換路徑,並建立www.conf,而後將以前php-fpm.conf中的www部分寫入到其中
[root@yong-01 etc]# cd php-fpm.d/
[root@yong-01 php-fpm.d]# vim 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
  • 而後在建立yueyong.conf
[root@yong-01 php-fpm.d]# vim yueyong.conf

[yueyong.com]
listen = /tmp/yueyong.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
  • 這樣看着就會很乾淨,相似於nginx的vhost
  • 檢查是否有錯誤
[root@yong-01 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[13-Jun-2018 22:05:59] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
  • 而後重啓查看
[root@yong-01 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@yong-01 php-fpm.d]# ps aux |grep php-fpm
root      1987  0.0  0.2 227292  4988 ?        Ss   22:06   0:00 php-fpm: master process (/usr/localphp-fpm/etc/php-fpm.conf)
php-fpm   1988  0.0  0.2 227232  4724 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1989  0.0  0.2 227232  4724 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1990  0.0  0.2 227232  4724 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1991  0.0  0.2 227232  4724 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1992  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1993  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1994  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1995  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1996  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1997  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1998  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   1999  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2000  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2001  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2002  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2003  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2004  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2005  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2006  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2007  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool www
php-fpm   2008  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2009  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2010  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2011  0.0  0.2 227232  4728 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2012  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2013  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2014  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2015  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2016  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2017  0.0  0.2 227232  4732 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2018  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2019  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2020  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2021  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2022  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2023  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2024  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2025  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2026  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
php-fpm   2027  0.0  0.2 227232  4736 ?        S    22:06   0:00 php-fpm: pool yueyong.com
root      2029  0.0  0.0 112676   980 pts/0    R+   22:06   0:00 grep --color=auto php-fpm
  • 這就是多個池子的配置方法

php-fpm慢執行日誌目錄概要

  • vim /usr/local/php-fpm/etc/php-fpm.d/www.conf//加入以下內容
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
  • 配置nginx的虛擬主機test.com.conf,把unix:/tmp/php-fcgi.sock改成unix:/tmp/www.sock
  • 從新加載nginx服務
  • vim /data/wwwroot/test.com/sleep.php//寫入以下內容
<?php 
echo 「test slow log」;
sleep(2);echo 「done」;
?>
  • curl -x127.0.0.1:80 test.com/sleep.php
  • cat /usr/local/php-fpm/var/log/www-slow.log

php-fpm慢執行日誌

  • 在作php網站的時候,建議使用lnmp,由於咱們能夠分析php的慢執行
  • 查詢方法:
    • 系統負載,能夠經過各類工具查,查出是哪一個進程致使
    • PHP網站訪問慢,經過查看慢日誌
  • 配置www.conf 在配置文件中加入
request_slowlog_timeout = 1    //超過一秒鐘就要記錄日誌
slowlog = /usr/local/php-fpm/var/log/www-slow.log    //日誌放到該路徑下

最終以下css

[root@yong-01 php-fpm.d]# vim 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
slowlog = /usr/local/php-fpm/var/log/www-slow.log
  • 查看語法錯誤,並從新加載
[root@yong-01 php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[13-Jun-2018 22:19:19] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@yong-01 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
  • 查看/usr/local/php-fpm/var/log/www-slow.log日誌是否生成
[root@yong-01 php-fpm.d]# ls /usr/local/php-fpm/var/log/
php-fpm.log  www-slow.log
[root@yong-01 php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log
  • 查看日誌會看到沒有內容,由於沒有超過一秒php執行的過程
  • 模擬一個慢執行的php,寫一個腳本,因爲php-fcgi它是被在test這個站點用着,因此須要在這個/data/wwwroot/test.com目錄下作一些操做
[root@yong-01 php-fpm.d]# vim /data/wwwroot/test.com/sleep.php
<?php
echo 「test slow log」;
sleep(2);echo 「done」;
?>
  • php能夠不換行,寫成一行
  • 運行php腳本,可是未輸出信息,檢測會看到狀態碼爲500,說明有錯誤
[root@yong-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
[root@yong-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 500 Internal Server Error
Server: nginx/1.4.7
Date: Wed, 13 Jun 2018 14:22:56 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
  • 能夠查看錯誤日誌,但有更簡單的方法就是打開它的配置文件
[root@yong-01 php-fpm.d]# vim /usr/local/php-fpm/etc/php.ini 

搜索 /display ,找到display_errors

將display_errors = Off 改成display_errors = On
  • 打開display_errors以後,就能夠在瀏覽器上查看到具體的錯誤是什麼
  • 重啓或者從新加載php-fpm
[root@yong-01 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
  • 檢查錯誤,會看到語法錯誤,有多是逗號,分號寫的不對
[root@yong-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
<br />
<b>Parse error</b>:  syntax error, unexpected 'slow' (T_STRING), expecting ',' or ';' in <b>/data/wwwroot/test.com/sleep.php</b> on line <b>2</b><br />
  • 檢查sleep.php文件,發現裏面使用的中文標點,更改後重啓配置
[root@hf-01 php-fpm.d]# vim /data/wwwroot/test.com/sleep.php
<?php
echo "test slow log";
sleep(2);
echo "done";
?>
[root@yong-01 php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
  • 再來測試,訪問成功,會看到訪問的時候停頓了幾秒
[root@yong-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
test slow logdone
  • 查看是否生成了慢日誌
[root@yong-01 php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log 

[13-Jun-2018 22:31:01]  [pool www] pid 2362
script_filename = /data/wwwroot/test.com/sleep.php     //提示:是哪一個腳本慢
[0x00007fa3e9694270] sleep() /data/wwwroot/test.com/sleep.php:3   //提示:是腳本的第三慢

[13-Jun-2018 22:32:24]  [pool www] pid 2364
script_filename = /data/wwwroot/test.com/sleep.php
[0x00007fa3e9694528] sleep() /data/wwwroot/test.com/sleep.php:3
  • 這個就是慢執行日誌的功效
  • 在遇到php網站訪問慢的時候,就能夠去查看慢日誌
  • 查看www.conf    request_slowlog_timeout = 1  這個就是控制訪問慢的時間
[root@yong-01 php-fpm.d]# cat 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  
slowlog = /usr/local/php-fpm/var/log/www-slow.log

php-fpm定義open_basedir目錄概要

  • vim /usr/local/php-fpm/etc/php-fpm.d/test.conf//加入以下內容
php_admin_value[open_basedir]=/data/wwwroot/aming.com:/tmp/
  • 建立測試php腳本,進行測試
  • 再次更改aming.conf,修改路徑,再次測試
  • 配置錯誤日誌
  • 再次測試
  • 查看錯誤日誌

php-fpm定義open_basedir

  • open_basedir 的做用是限制php在指定的目錄裏活動。
  • 由於若是服務器管理多個網站,在php.ini裏定義多個open_basedir就不合適了,因此要麼在apache虛擬主機配置文件裏面定義,要麼在php-fpm配置文件裏面定義,咱們能夠針對不一樣的池子(pool)定義對應的open_basedir
    • 只須要加 下面一行配置便可
    php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
    - 若是路徑出錯,就會沒法正常訪問,致使404頁面 curl直接連會提示「No input file specified.」
  • 針對www的pool進行配置
  • 在最後一行加入配置
[root@yong-01 php-fpm.d]# vim /usr/local/php-fpm/etc/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
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
  • 重啓php-fpm
[root@yong-01 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
  • 訪問測試
[root@yong-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Wed, 13 Jun 2018 14:48:59 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
  • 若open_basedir定義錯誤,就會致使沒法訪問,造成404狀態碼,輸出curl直接連會提示「No input file specified.」
  • 定義錯誤日誌
[root@yong-01 php-fpm.d]# vim /usr/local/php-fpm/etc/php.ini 

搜索 /display_errors
將display_errors = Off
搜索 /error_log,添加error_log
error_log = /usr/local/php-fpm/var/log/php_errors.log   //這一段定義錯誤日誌路徑
搜索error_reporting
註釋掉自帶的error_reporting
;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL  //定義錯誤日誌的級別全部
  • display_errors = Off 正常狀況下,在線上這個是off的,別人不能經過瀏覽器看到你的錯誤信息,而是把你的錯誤信息記錄到服務器的某一個文件裏
  • 查看設置的錯誤日誌文件,並是否生成
[root@yong-01 php-fpm.d]# ls /usr/local/php-fpm/var/log/
php-fpm.log  www-slow.log
  • 手動生成錯誤日誌文件,並設置權限爲777,防止不可以正常寫入
[root@yong-01 php-fpm.d]# touch /usr/local/php-fpm/var/log/php_errors.log
[root@yong-01 php-fpm.d]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log
  • 這裏爲了方便測試,故意將/usr/local/php-fpm/etc/php-fpm.d/www.conf 中的open_basedir路徑寫錯,來方便測試
  • 重啓php-fpm
  • 訪問測試,會看到顯示404
[root@yong-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.4.7
Date: Wed, 13 Jun 2018 15:00:11 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@yong-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php
No input file specified.
  • 查看錯誤日誌,會看到訪問地址有限制
[root@yong-01 php-fpm.d]# cat /usr/local/php-fpm/var/log/php_errors.log 
[13-Jun-2018 14:59:36 UTC] PHP Deprecated:  Comments starting with '#' are deprecated in Unknown on line 1 in Unknown on line 0
[13-Jun-2018 15:00:11 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/3.php) is not within the allowed path(s): (/data/wwwroot/test22.com:/tmp/) in Unknown on line 0
[13-Jun-2018 15:00:11 UTC] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[13-Jun-2018 15:00:27 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/3.php) is not within the allowed path(s): (/data/wwwroot/test22.com:/tmp/) in Unknown on line 0
[13-Jun-2018 15:00:27 UTC] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0
  • 再將配置文件的open_basedir路徑修改過來,從新加載php-fpm,再來訪問,會看到正常顯示
[root@yong-01 php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Wed, 13 Jun 2018 15:02:20 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

php-fpm進程管理

[root@yong-01 php-fpm.d]# cat 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
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
  • pm = dynamic //表示進程以什麼形式啓動,dynamic就是動態,動態就是一開始爲一個數值,根據需求再自動生成,服務器比較閒的時候還會去銷燬,銷燬到必定程度還有自動生成
  • 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的子進程最多能夠處理這麼多請求,當達到這個數值時,它會自動退出。
  • 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/

dynamic和static

  • pm = dynamic //表示進程以什麼形式啓動,dynamic就是動態,動態就是一開始爲一個數值,根據需求再自動生成,服務器比較閒的時候還會去銷燬,銷燬到必定程度還有自動生成;根據下面的設置去進行設定「start_servers 」、「min_spare_servers 」、「max_spare_servers 」、「max_requests 」
  • pm = static 以後,一旦選擇這個設置,下面的pm的各類設置只會有一個max_children生效,並啓動的時候就生成50個
相關文章
相關標籤/搜索