16. php-fpm配置相关

Posted

tags:

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

[toc]

php-fpm配置相关

一、php-fpm配置

和LAMP不同的是,在LNMP架构中,php-fpm作为独立的一个服务存在,既然是独立服务,那么它必然有自己的配置文件。php-fpm的配置文件为/usr/local/php-fpm/etc/php-fpm.conf,它同样也支持include语句,类似于nginx.conf里面的include.

1. php-fpm 的pool

Nginx可以配置多个虚拟主机,php-fpm配置文件php-fpm.conf可以设置多个pool,每个pool可以监听一个端口,在其中一个pool资源耗尽,会导致其他站点无法访问资源,报502错误。有必要把站点进行分离,分别使用单独的pool。

1.打开php-fpm.conf

[[email protected] ~]# vim /usr/local/php/etc/php-fpm.conf
[[email protected] ~]# 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]
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

php-fpm.conf部分内容注解


#[www]
#listen = /tmp/php-fcgi.sock
#监听地址,也可以写:listen = 127.0.0.1:9000,本地监听,也可以监听其他IP:port
#此处格式会影响配置Nginx和PHP结合时Nginx寻址PHP的路径
#listen.mode = 666
#当监听的为socket文件时该部分才生效,用于指定.sock文件的权限
#user = php-fpm
#group = php-fpm
#定义php-fpm服务的用户

在原来基础上在复制修改一个

[xavi.com]
listen = /tmp/xavier.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-fpm服务

[19-Mar-2018 23:02:28] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[[email protected] etc]# /etc/init.d/php-fpm reload
Reload service php-fpm  done

查看下php-fpm服务

[[email protected] etc]# ps aux |grep php-fpm
root       4684  0.0  0.2 227268  4984 ?        Ss   23:03   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    4685  0.0  0.2 229292  5080 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4686  0.0  0.2 229292  5080 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4687  0.0  0.2 229292  5080 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4688  0.0  0.2 229292  5084 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4689  0.0  0.2 229292  5088 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4690  0.0  0.2 229292  5088 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4691  0.0  0.2 229292  5088 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4692  0.0  0.2 229292  5088 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4693  0.0  0.2 229292  5088 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4694  0.0  0.2 229292  5088 ?        S    23:03   0:00 php-fpm: pool www
php-fpm    4705  0.0  0.2 229292  5088 ?     
php-fpm    4809  0.0  0.2 229292  5084 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4810  0.0  0.2 229292  5084 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4811  0.0  0.2 229292  5084 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4812  0.0  0.2 229292  5084 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4813  0.0  0.2 229292  5088 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4814  0.0  0.2 229292  5088 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4815  0.0  0.2 229292  5088 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4816  0.0  0.2 229292  5088 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4817  0.0  0.2 229292  5088 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4818  0.0  0.2 229292  5088 ?        S    23:08   0:00 php-fpm: pool xavi.com
php-fpm    4819  0.0  0.2 229292  5088 ?        S    23:08   0:00 php-fpm: pool xavi.com

2.已经看到www和xavi.com两个pool了,下一步如何应用

[[email protected] etc]# cd /usr/local/nginx/conf/vhost/
[[email protected] vhost]# ls
bcd.com.conf  load.conf  proxy.conf  ssl.conf  test.com.conf
[[email protected] vhost]# vim test.com.conf
[[email protected] vhost]# vim bcd.com.conf

server
    {

   listen 80 default_server;
   server_name abcd.com;
   index index.html index.htm index.php;
   root /data/nginx/default;
location ~ \.php$
      {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/xavier.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/nginx/default$fastcgi_script_name;
      }

另一种创建多个pool的方法

在/usr/local/nginx/conf/vhost/下有多个conf文件,这样我们可以配置多个虚拟机,那么php-fpm是否也支持多个conf文件呢,是的

技术分享图片

技术分享图片

技术分享图片

[[email protected] php-fpm.d]# vi 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

[[email protected] php-fpm.d]# vi xavi.conf
[xavi.com]
listen = /tmp/xavier.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-fpm.conf就只有三行内容了

[[email protected] php-fpm.d]# 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
include = etc/php-fpm.d/*.conf

检查语法并重启

[[email protected] php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[20-Mar-2018 20:39:01] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

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

ps aux |grep php-fpm

技术分享图片

二、php-fpm慢执行日志

php网站,建议使用lnmp架构的一个好处就是查看“慢日志”,为什么这么说呢?当我们的客户或者manager发现站点访问速度点慢,如果全部的负载均不是很高,一切正常还是慢,怎么办?那唯一的办法就是检查“慢日志”,慢日志会向我们详细的展示某个页面的多少行代码在执行过程中超过了我们设定的阀值!

2.1 配置www.conf吧

在原有的基础上面,增加两行:

request_slowlog_timeout = 2
slowlog = /usr/local/php-fpm/var/log/www_slow.log

命令的意思是:只要执行某条语句超过2秒就会记录到咱们规定的日志文件中去。

开启慢执行日志

[[email protected] php-fpm.d]# vim /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 = 2
slowlog = /usr/local/php-fpm/var/log/www_slow.log
[[email protected] php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[20-Mar-2018 21:50:45] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

2.查看慢执行日志

[[email protected] php-fpm.d]# ls /usr/local/php-fpm/var/log/
php-fpm.log  www-slow.log
[[email protected] php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log
  • 我们发现已经有配置文件,但是还没有记录任何的错误;

cat /usr/local/nginx/conf/vhost/test.com.conf

技术分享图片

php-fcgi.sock被test.com.conf占用着,在/data/nginx/test.com/下创建sleep.php文件

[[email protected] nginx]# vim /data/nginx/test.com/sleep.

<?php 
echo "slowlog test!"; 
 sleep(2);
echo "done";
?>

技术分享图片

错误报告:

[[email protected] nginx]# vim /data/nginx/test.com/sleep.php
[[email protected] nginx]# curl -x127.0.0.1:80 test.com/sleep.php
curl: (7) Failed connect to 127.0.0.1:80; 拒绝连接

技术分享图片

技术分享图片

技术分享图片

设置了重复多个default 80端口,以及忘了}符号,以后测试文件及时删除

测试结果

[[email protected] vhost]# curl -x127.0.0.1:80 test.com/sleep.php 
slowlog test!done[[email protected] vhost]# 
[[email protected] vhost]#  cat /usr/local/php-fpm/var/log/www-slow.log

[20-Mar-2018 22:58:57]  [pool www] pid 4662
script_filename = /data/nginx/test.com/sleep.php
[0x00007f5860f422e8] sleep() /data/nginx/test.com/sleep.php:1

[20-Mar-2018 22:59:49]  [pool www] pid 4664
script_filename = /data/nginx/test.com/sleep.php
[0x00007f5860f422e8] sleep() /data/nginx/test.com/sleep.php:1 //第一行
[[email protected] vhost]# cat /data/nginx/test.com/sleep.php
<?php echo "slowlog test!"; sleep(2);echo "done"; ?>

错误分析2
技术分享图片

最简单的方法是打开配置文件,查看错误日志:vim /usr/local/php-fpm/etc/php.ini

技术分享图片

识别出来是语法错误
技术分享图片

三、Open_basedir

如果一个server有很多个web服务,那么不建议直接把open_basedir直接配置在php.ini中了。Appache可以针对每个虚拟主机设置一个Open_basedir,php-fpm同样也可以针对不同的pool设置不同的Open_basedir.

[[email protected] vhost]#  vim /usr/local/php-fpm/etc/php-fpm.d/www.conf

技术分享图片

技术分享图片

目的:这样就完全限制了在xavi.com目录下不可以访问其它的站点资源,进行物理隔离多个站点

上述排错是打开php.ini中display_error在生产情况下是不安全的。

建立日志查错体系

vim /usr/local/php-fpm/etc/php.ini

增加如下配置:搜索/error_log 
errot_log = /usr/local/php-fpm/var/log/php_error.log

技术分享图片

技术分享图片

[[email protected] vhost]# 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_error.log
; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=

为保证日志正常输出,把该路径下的日志文件建立起来,并更改其属性

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

测试错误日志:

技术分享图片

修改问conf文件一定要把php-fpm重新启动才能生效

[[email protected] vhost]# !curl
curl -x127.0.0.1:80 test.com/sleep.php 
slowlog test!done[[email protected] vhost]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[[email protected] vhost]# curl -x127.0.0.1:80 test.com/sleep.php 
No input file specified.
[[email protected] vhost]# !cat
cat /usr/local/php-fpm/var/log/php_error.log
[20-Mar-2018 15:38:25 UTC] PHP Deprecated:  Comments starting with ‘#‘ are deprecated in Unknown on line 1 in Unknown on line 0
[20-Mar-2018 15:38:25 UTC] PHP Deprecated:  Comments starting with ‘#‘ are deprecated in Unknown on line 1 in Unknown on line 0
[20-Mar-2018 15:38:31 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/nginx/test.com/sleep.php) is not within the allowed path(s): (/data/nginx/wwtest.com:/tmp/) in Unknown on line 0
[20-Mar-2018 15:38:31 UTC] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0

技术分享图片

四、php-fpm进程管理

  • [ ] pm = dynamic ;
    动态进程管理,也可以是static,只有此处设置为dynamic,下面的配置才生效

  • [ ] pm.max_children = 50 ;
    最大子进程数,ps aux可以查看

  • [ ] pm.start_serve = 20;启动服务时会启动的进程数

  • [ ] pm.min_spare_servers = 5;
    定义空闲时段,子进程数的最小数量,如果达到这个数值时,php-fpm
    ;服务会自动派生新的子进程。

  • [ ] pm.max_spare_servers = 35 ;
    定义在空闲时段,子进程的最大值,如果高于这个数值就开始清理空闲的子进程。

  • [ ] pm.max_requests = 500 ;定义一个子进程最多处理的请求数,也就是说在一个php-fpm的子进程最多可以处理这么请求,当达到这个数值时,他会自动退出。

以上是关于16. php-fpm配置相关的主要内容,如果未能解决你的问题,请参考以下文章

PHP 相关配置

Linux学习总结(四十四)lnmp之php-fpm相关配置

PHP配置优化:php-fpm配置解读

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段