十三周一次课

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十三周一次课相关的知识,希望对你有一定的参考价值。

十三周一次课

12.21php-fpm的pool

12.22php-fpm的慢执行日志

12.23open_basedir

12.24php-fpm进程管理

12.21php-fpm的pool

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

1.php-fpm有一个概念,叫做pool。 使用ps aux |grep php-fpm最右侧看到的那一列就是pool,也就是它的池子

[[email protected] vhost]# ps aux |grep php-fpm
root       1056  0.0  0.4 227204  4952 ?        Ss   08:39   0:01 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    1057  0.0  0.4 227204  4716 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1058  0.0  0.4 227204  4716 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1059  0.0  0.4 227204  4716 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1060  0.0  0.4 227204  4716 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1061  0.0  0.4 227204  4720 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1062  0.0  0.4 227204  4720 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1063  0.0  0.4 227204  4720 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1064  0.0  0.4 227204  4720 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1065  0.0  0.4 227204  4720 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1066  0.0  0.4 227204  4720 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1067  0.0  0.4 227204  4720 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1068  0.0  0.4 227204  4720 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1069  0.0  0.4 227204  4724 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1070  0.0  0.4 227204  4724 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1071  0.0  0.4 227204  4724 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1072  0.0  0.4 227204  4724 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1073  0.0  0.4 227204  4724 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1074  0.0  0.4 227204  4724 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1075  0.0  0.4 227204  4724 ?        S    08:39   0:00 php-fpm: pool www
php-fpm    1076  0.0  0.4 227204  4724 ?        S    08:39   0:00 php-fpm: pool www
root       5360  0.0  0.0 112664   980 pts/0    S+   21:50   0:00 grep --color=auto php-fpm
[[email protected] vhost]# 

2.切换目录,会看到我们只定义了一个www的pool

[[email protected] vhost]# cd /usr/local/php-fpm/etc/
[[email protected] etc]# ls

pear.conf  php-fpm.conf  php-fpm.conf.default  php.ini
[[email protected] 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]        //这里只定义了一个www的pool
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
[[email protected] etc]# 

3.其实是支持定义多个池子的,每一个池子都可以监听不同的sock或者TCP/IP,nginx如果有多个站点,那么每个站点都能去使用一个独立的pool。这样做的优势就是,当其中一个php发生502了(502很有可能是php资源不够了),如果所有的网站使用了同一个池子,那其中一个网站发生了故障(比如写的程序有问题),那么就会把你的php资源耗尽,这样就会造成其他的站点也会502,所以有必要把每一个站点隔离开来,每一个站点使用独立的pool。

4.设置多个pool

方法一

  • 在/usr/local/php-fpm/etc/php-fpm里面,继续增加pool

    • 写第二个pool,名称为tianqi.com,listen = /tmp/tianqi.sock

    • 配置中的其他信息可根据自己的需求来配置

[[email protected] etc]# vim !$
vim 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
[tianqi.com]
listen = /tmp/tianqi.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

保存退出

5.检查是否存在语法错误

[[email protected] etc]# /usr/local/php-fpm/sbin/php-fpm -t
[16-Mar-2018 21:58:38] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[[email protected] etc]# 

6.若没有语法错误,做一个php-fpm重启

  • /etc/init.d/php-fpm restart 或使用 /etc/init.d/php-fpm reload

[[email protected] etc]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[[email protected] etc]# 

7.这里再来ps查看,会看到最右侧多了一个tianqi.com,这个就是两个不同的池子

[[email protected] etc]# ps aux |grep php-fpm
root       5389  0.2  0.4 227264  4976 ?        Ss   21:59   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    5390  0.0  0.4 227204  4712 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5391  0.0  0.4 227204  4712 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5392  0.0  0.4 227204  4712 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5393  0.0  0.4 227204  4712 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5394  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5395  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5396  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5397  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5398  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5399  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5400  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5401  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5402  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5403  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5404  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5405  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5406  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5407  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5408  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5409  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool www
php-fpm    5410  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5411  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5412  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5413  0.0  0.4 227204  4716 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5414  0.0  0.4 227204  4720 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5415  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5416  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5417  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5418  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5419  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5420  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5421  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5422  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5423  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5424  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5425  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5426  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5427  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5428  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
php-fpm    5429  0.0  0.4 227204  4724 ?        S    21:59   0:00 php-fpm: pool tianqi.com
root       5447  0.0  0.0 112660   984 pts/0    R+   22:02   0:00 grep --color=auto php-fpm
[[email protected] etc]# 

8.在nginx中使用新的pool

[[email protected] conf]# cd /usr/local/nginx/conf/vhost
[[email protected] vhost]# ls

aaa.com.conf  load.conf  proxy.conf  ssl.conf  test.com.conf
[[email protected] vhost]# 

9.可以把test.com.conf定义成fastcgi_pass unix:/tmp/php-fcgi.sock;

  • 这是www池子中的php-fcgi.sock

[[email protected] vhost]# vim test.com.conf

server
{
    listen 80;
    server_name test.com test2.com test3.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 127.0.0.1;
    allow 192.168.11.136;
    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;
}
10.再把另一个aaa.com.conf中 fastcgi_pass unix:/tmp/tianqi.sock;

  • 这是tianqi.com池子中的tianqi.sock

  • 路径/data/wwwroot/default$fastcgi_script_name;

[[email protected] 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/tianqi.sock;   
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
    }
}

保存退出

11.这样这两个站点就隔离开了

12.假设现在test.com请求量很大,最终将进程耗尽了,比如php-fpm耗尽了,这时候又有其他请求来了,就会报502,因为没有多余的php给进程提供服务了,而另一个站点则不受影响,因为使用的是另一个池子,另一个pool,这就是定义多个池子的原因

方法二

1.在vhost目录下是多个.conf文件,在nginx.conf里面加了一条 include vhost/*.conf;

[[email protected] vhost]# ls
aaa.com.conf  load.conf  proxy.conf  ssl.conf  test.com.conf
[[email protected] vhost]# tail ../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;
}
[[email protected] vhost]# 

2.在php-fpm同样是支持 include vhost/*.conf;

3.在主配置文件php-fpm中的 [global] 加入include = etc/php-fpm.d/*.conf ,并将池子拆分出来,将其中的池子pool删除

  • 快速删除内容方法,100dd就会给删除

[[email protected] vhost]# cd /usr/local/php-fpm/etc/
[[email protected] etc]# ls

pear.conf  php-fpm.conf  php-fpm.conf.default  php.ini
[[email protected] etc]# vim 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        //添加一条语句,删除下面的语句

保存退出

4.因为include = etc/php-fpm.d/,所以需要创建/php-fpm.d/的目录

[[email protected] etc]# mkdir php-fpm.d
[[email protected] etc]# 

5.切换路径,并创建www.conf,然后将之前php-fpm.conf中的www部分写入到其中

[[email protected] etc]# cd !$
cd php-fpm.d
[[email protected] 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

保存退出

6.然后再创建tianqi.conf

[[email protected] php-fpm.d]# vim tianqi.conf

[tianqi.com]
listen = /tmp/tianqi.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.这样看着就会很干净,类似于nginx的vhost

8.检查是否有错误

[[email protected] php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[16-Mar-2018 22:24:33] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[[email protected] php-fpm.d]# 

9.然后重启查看

[[email protected] php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[[email protected] php-fpm.d]# !ps
ps aux |grep php-fpm

root       5511  2.0  0.4 227292  4984 ?        Ss   22:25   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    5512  0.0  0.4 227232  4720 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5513  0.0  0.4 227232  4720 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5514  0.0  0.4 227232  4720 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5515  0.0  0.4 227232  4720 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5516  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5517  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5518  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5519  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5520  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5521  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5522  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5523  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5524  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5525  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5526  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5527  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5528  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5529  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5530  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5531  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool tianqi.com
php-fpm    5532  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5533  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5534  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5535  0.0  0.4 227232  4724 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5536  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5537  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5538  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5539  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5540  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5541  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5542  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5543  0.0  0.4 227232  4728 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5544  0.0  0.4 227232  4732 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5545  0.0  0.4 227232  4732 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5546  0.0  0.4 227232  4732 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5547  0.0  0.4 227232  4732 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5548  0.0  0.4 227232  4732 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5549  0.0  0.4 227232  4732 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5550  0.0  0.4 227232  4732 ?        S    22:25   0:00 php-fpm: pool www
php-fpm    5551  0.4  0.4 227232  4732 ?        S    22:25   0:00 php-fpm: pool www
root       5553  0.0  0.0 112660   980 pts/0    S+   22:25   0:00 grep --color=auto php-fpm
[[email protected] php-fpm.d]# 

10.这就是多个池子的配置方法

12.22php-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的慢执行日志

  • 查询方法:

  • 1.系统负载高,可以通过各种工具查,查出是哪个进程导致

  • 2.PHP网站访问慢,通过查看慢执行日志

1.配置www.conf 在配置文件中加入

request_slowlog_timeout = 1 //超过一秒钟就要记录日志

slowlog = /usr/local/php-fpm/var/log/www-slow.log //日志放到该路径下

[[email protected] 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

保存退出

2.查看语法错误,并重新加载

[[email protected] php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[16-Mar-2018 22:34:21] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[[email protected] php-fpm.d]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[[email protected] php-fpm.d]# 

3.修改虚拟主机配置文件

[[email protected] etc]# vim /usr/local/nginx/conf/vhost/test.com.conf
# 修改sock文件
fastcgi_pass unix:/tmp/www.sock;
...

  • 1

  • 2

  • 3

  • 4

4.重启nginx服务

[[email protected] etc]# /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
[[email protected] etc]# /usr/local/nginx/sbin/nginx -s reload

  • 1

  • 2

  • 3

  • 4

5.设置测试php文件

[[email protected] etc]# vim /data/www/test.com/sleep.php 
<?php
echo "test slow log";
sleep(2);
echo "done";
?>

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

说明:要查看具体的错误信息,可以开启php.ini内的display_error参数

6.查看慢执行日志

[[email protected] etc]# cat /usr/local/php-fpm/var/log/www-slow.log 

[05-Jan-2018 21:13:15]  [pool www] pid 3430
script_filename = /data/www/test.com/sleep.php
[0x00007f616ec772d8] sleep() /data/www/test.com/sleep.php:3


12.23open_basedir

open_basedir 的作用是限制php在指定的目录里活动。

相关配置代码

[[email protected] etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf 
# 添加如下代码
php_admin_value[open_basedir]=/data/www/test.com:/tmp/

  • 1

  • 2

  • 3

效果测试

先测试错误配置时php脚本的效果

[[email protected] etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf 
# 将添加的代码内站点错误书写为aaa.com
php_admin_value[open_basedir]=/data/www/aaa.com:/tmp/

  • 1

  • 2

  • 3

重启php-fpm服务

[[email protected] etc]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 18:35:50] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[[email protected] etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

使用curl测试网页访问

# 由于已经设置了open_basedir,网页无法访问
[[email protected] etc]# curl -x 127.0.0.1:80 test.com/test.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 10:38:45 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

修改正确后验证

[[email protected] etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf 
# 将添加的代码错误书写为
php_admin_value[open_basedir]=/data/www/test.com:/tmp/

  • 1

  • 2

  • 3

重启服务并验证

[[email protected] etc]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Jan-2018 18:39:10] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[[email protected] etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

# 成功访问
[[email protected] etc]# curl -x 127.0.0.1:80 test.com/test.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 10:39:20 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

php错误日志设置

修复php.ini配置文件,关闭display_errors参数

# 不在浏览器上显示错误信息
display_errors = Off

# 开启错误日志记录
log_errors = On
# 设置存放路径
error_log = /usr/local/php-fpm/var/log/php_errors.log
# 设置错误记录级别
error_reporting = E_ALL

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

为了防止错误日志无法创建并修改,最好先行创建并给予权限。

[[email protected] etc]# touch /usr/local/php-fpm/var/log/php_errors.log
[[email protected] etc]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log

  • 1

  • 2

为了产生错误信息,先故意写错php代码

[[email protected] etc]# touch /usr/local/php-fpm/var/log/php_errors.log
[[email protected] etc]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log 
php_admin_value[open_basedir]=/data/www/aaa.com:/tmp/

  • 1

  • 2

  • 3

访问网页,其错误信息如下

[[email protected] etc]# curl -x 127.0.0.1:80 test.com/test.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 10:52:01 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

# 这里明确指出所要访问的网页不在aaa.com站点
[[email protected] etc]# cat /usr/local/php-fpm/var/log/php_errors.log 
[09-Jan-2018 10:52:01 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/www/test.com/test.php) is not within the allowed path(s): (/data/www/aaa.com:/tmp/) in Unknown on line 0
[09-Jan-2018 10:52:01 UTC] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

修改正确后再查看日志(php文件已被解析)

[[email protected] etc]# cat /usr/local/php-fpm/var/log/php_errors.log 
...
[09-Jan-2018 10:53:18 UTC] PHP Warning:  phpinfo(): It is not safe to rely on the syst

12.24php-fpm进程管理

进程管理

# 动态进程管理
pm = dynamic

# 静态进程管理
# 设置了静态进程管理的配置项只有紧跟该行的那项配置生效
pm = static

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

最大子进程数

# 可以使用ps aux进行查看
pm.max_children = 50

  • 1

  • 2

设置启动时开启的PHP进程数

# 值可以根据需要进行修改
pm.start_servers = 20

  • 1

  • 2

在空闲时段,子进程数的最小值

# 如果达到这个数值,php-fpm服务会自动派生新的子进程
pm.min_spare_servers = 5

  • 1

  • 2

定义在空闲时段,子进程数的最大值

# 如果高于这个数值,就开始清理空闲的子进程
pm.max_spare_servers = 35

  • 1

  • 2

定义一个子进程最多处理的请求数

# 对于该项而言,超出该值的话进程会因此而退出
pm.max_requests = 500

  • 1

  • 2

说明:们需要根据实际应用时的需要对上述参数的值进行配置,配置后重启php-fpm服务使其生效即可。


以上是关于十三周一次课的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习笔记十三周一次课(5月2日)

-考研第二十三周总结-

十三周五次课

十三周四次课

十三周四次课(3月22日

十三周五次课(3月23日)