94.php-fpm配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了94.php-fpm配置相关的知识,希望对你有一定的参考价值。
php-fpm的pool
php-fpm配置文件php-fpm.conf可以设置多个pool,在其中一个pool资源耗尽,会导致其他站点无法访问资源,报502错误。有必要把站点进行分离,分别使用单独的pool。
1、修改配置文件
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[[email protected] etc]# vim php-fpm.conf
include = etc/php-fpm.d/*.conf
[global]#定义全局参数
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log#添加上面这行,暂时注释掉下面的pool#[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服务的用户#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、创建配置文件目录和子配置文件
[[email protected] etc]# mkdir /usr/local/php-fpm/etc/php-fpm.d
[[email protected] etc]# cd php-fpm.d
[[email protected] php-fpm.d]# ls
[[email protected] php-fpm.d]# vim www.conf //新建配置文件写入以下内容
[www]
listen = /tmp/www.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服务的用户
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]# vim zlinu.conf //在新建另一个配置文件,写入以下内容
[sdwaqw]
listen = /tmp/sdwaqw.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服务的用户
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、验证配置
[[email protected] php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[19-Mar-2018 18:07:47] 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
[[email protected] php-fpm.d]# ls /tmp/ | grep sock
mysql.sock
www.sock
sdwaqw.sock
[[email protected] php-fpm.d]# ps aux |grep php
root 2570 0.0 0.2 227240 4992 ? Ss 18:08 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 2571 0.0 0.2 227180 4720 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2572 0.0 0.2 227180 4720 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2573 0.0 0.2 227180 4720 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2574 0.0 0.2 227180 4720 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2575 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2576 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2577 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2578 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2579 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2580 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2581 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2582 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2583 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2584 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2585 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2586 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2587 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2588 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2589 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2590 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool www
php-fpm 2591 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2592 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2593 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2594 0.0 0.2 227180 4724 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2595 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2596 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2597 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2598 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2599 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2600 0.0 0.2 227180 4728 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2601 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2602 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2603 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2604 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2605 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2606 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2607 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2608 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2609 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
php-fpm 2610 0.0 0.2 227180 4732 ? S 18:08 0:00 php-fpm: pool sdwaqw
4、修改虚拟主机配置文件,为站点配置pool
[[email protected] php-fpm.d]# cd /usr/local/nginx/conf/vhost
[[email protected] vhost]# vim sdwaqw.conf
server
{
listen 80 default_server;# 有default_server标记的就是默认虚拟主机
server_name sdwaqwtest.com;
index index.html index.htm index.php;
root /data/wwwroot/sdwaqw;
location ~ \.php$
{
include fastcgi_params;
# 这里写对应php-fpm.conf内设置的sock
fastcgi_pass unix:/tmp/sdwaqw.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/sdwaqw$fastcgi_script_name;
}
}
[[email protected] vhost]# /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
以上为分别设置pool,单独保存在文件夹。也可以直接在pool-fpm.conf中添加多个pool,站点调用对应的就行。
二、php-fpm的慢执行日志
1、编辑配置文件
[[email protected] vhost]# vim /usr/local/php-fpm/etc/php-fpm.d/zlinu.conf
[sdwaqw]
listen = /tmp/sdwaqw.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服务的用户
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#当请求超过1秒开始记录日志
slowlog = /usr/local/php-fpm/var/log/sdwaqw-slow.log#日志存放地址
[[email protected] vhost]# /usr/local/php-fpm/sbin/php-fpm -t
[19-Mar-2018 18:30:23] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[[email protected] vhost]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm .
done
Starting php-fpm done
2、检验慢日志
[[email protected] vhost]# touch /usr/local/php-fpm/var/log/sdwaqw-slow.log
[[email protected] vhost]# chmod 777 /usr/local/php-fpm/var/log/sdwaqw-slow.log
[[email protected] vhost]# echo ‘<?php echo "slowlog test!"; sleep(2);echo "done"; ?>‘ > /data/wwwroot/sdwaqw/sleep.php
[[email protected] php-fpm.d]# curl -x127.0.0.1:80 sdwaqw.com/sleep.php
slowlog test!done[[email protected] php-fpm.d]#
[[email protected] php-fpm.d]# cat /usr/local/php-fpm/var/log/sdwaqw-slow.log //查看蛮日志
[19-Mar-2018 19:48:08] [pool sdwaqw] pid 2354
script_filename = /data/wwwroot/sdwaqw/sleep.php
[0x00007f8f0c1802e8] sleep() /data/wwwroot/sdwaqw/sleep.php:3
[19-Mar-2018 19:53:29] [pool sdwaqw] pid 2356
script_filename = /data/wwwroot/sdwaqw/sleep.php
[0x00007f8f0c1802e8] sleep() /data/wwwroot/sdwaqw/sleep.php:3
三、php-fphp-fpm定义open_basedir
当一台服务器跑多个站点时,使用openbasedir限定各个站点所能访问的服务器上的目录的范围。在php-fpm服务中,可以针对每个pool设定open basedir。
[[email protected] php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/sdwaqw.conf //加入如下内容
php_admin_value[open_basedir]=/data/wwwroot/sdwaqw:/tmp/
四、php-fpm进程管理
pm = dynamic
;动态进程管理,也可以是static
;只有此处设置为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的子进程最多可以处理这么请求,当达到这个数值时,他会自动退出。
以上是关于94.php-fpm配置的主要内容,如果未能解决你的问题,请参考以下文章