首页 > PHP技术 > php高级 > php-fpm优化
2020
04-27

php-fpm优化



1.通常情况我们修改/etc/php.ini文件,仅会修改错误日志与文件上传

#;;;;;;;;;;;;;;;;;
# Error logging ;
#;;;;;;;;;;;;;;;;;
expose_php = Off # 关闭php版本信息
display_error = Off # 屏幕不显示错误日志
error_reporting = E_WARNING & E_ERROR # 记录php错误日志至后台
log_errors = On # 开启日志
error_log = /var/log/php_error.log # 错误日志记录的位置
date.timezone = PRC # 时区调整,默认PRC, 建议调整为Asia/Shanghai

#;;;;;;;;;;;;;;;
# File Uploads ;
#;;;;;;;;;;;;;;;
file_uploads = On # 开启文件上传功能,默认启动
upload_max_filesize = 300M # 允许上传文件的最大大小
post_max_size = 300M # 允许客户端单个POST请求发送的最大数据
max_file_uploads = 20 # 允许同时上传的文件的最大数量
memory_limit = 128M # 每个脚本执行最大内存

#/etc/php.ini优化配置如下
sql.safe_mode = Off
post_max_size = 300M
upload_max_filesize = 300M
max_file_uploads = 20
memory_limit = 128M
date.timezone = Asia/Shanghai

expose_php = Off
display_error = Off
error_reporting = E_WARNING & E_ERROR
log_errors = On
error_log = /var/log/php_error.log

2.php-fpm主配置文件/etc/php-fpm.conf调整

#第一部分,fpm配置
;include=etc/fpm.d/*.conf

#第二部分,全局配置
[global]
;pid = /var/log/php-fpm/php-fpm.pid #pid文件存放的位置
;error_log = /var/log/php-fpm/php-fpm.log #错误日志存放的位置
;log_level = error #日志级别, alert, error, warning, notice, debug
rlimit_files = 65535 #php-fpm进程能打开的文件数
events.mechanism = epoll #使用epoll事件模型处理请求

#第三部分,进程池定义
[www] #池名称
user = www #进程运行的用户
group = www #进程运行的组
;listen = /dev/shm/php-fpm.sock #监听在本地socket文件
listen = 127.0.0.1:9000 #监听在本地tcp的9000端口
;listen.allowed_clients = 127.0.0.1 #允许访问FastCGI进程的IP,any不限制

; Choose how the process manager will control the number of child processes.
; Possible Values:
; static - a fixed number (pm.max_children) of child processes;
; dynamic - the number of child processes are set dynamically based on the
; following directives:
; pm.max_children - the maximum number of children that can
; be alive at the same time.
; pm.start_servers - the number of children created on startup.
; pm.min_spare_servers - the minimum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is less than this
; number then some children will be created.
; pm.max_spare_servers - the maximum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is greater than this
; number then some children will be killed.
; Note: This value is mandatory.

pm = dynamic #
pm.max_children = 512 #最大启动的php-fpm进程数
pm.start_servers = 32 #初始启动的php-fpm进程数
pm.min_spare_servers = 32 #最少的空闲php-fpm进程数
pm.max_spare_servers = 64 #最大的空闲php-fpm进程数
pm.max_requests = 1500 #每一个进程能响应的请求数
pm.process_idle_timeout = 15s;

# 错误日志
php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www_error.log
php_admin_flag[log_errors] = on

# 将查询超过5s的连接记录至慢查询日志中
request_slowlog_timeout = 5s
slowlog = /var/log/php/slow.log

3.php-fpm状态模块,用于监控php-fpm状态使用

[[email protected] ~]# vim /etc/php-fpm.d/www.conf
# 开启php的状态页面
pm.status_path = /phpfpm_status

#
[[email protected] conf.d]# cat /etc/nginx/conf.d/fpm.conf
server {
listen 80;
server_name php.qls.com;
location / {
root /code;
index index.php;
}
location /phpfpm_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

4.访问测试phpfpm_status状态页面

[[email protected] ~]# curl http://127.0.0.1/phpfpm_status
pool: www
process manager: dynamic
start time: 05/Jul/2016:15:30:56 +0800
start since: 409
accepted conn: 22
listen queue: 0
max listen queue: 0
listen queue len: 128
idle processes: 4
active processes: 1
total processes: 5
max active processes: 2
max children reached: 0

#PHP-FPM状态解释:
pool #fpm池名称,大多数为www
process manager #进程管理方式dynamic或者static
start time #启动日志,如果reload了fpm,时间会更新
start since #运行时间
accepted conn #当前池接受的请求数
listen queue #请求等待队列,如果这个值不为0,那么需要增加FPM的进程数量
max listen queue #请求等待队列最高的数量
listen queue len #socket等待队列长度
idle processes #空闲进程数量
active processes #活跃进程数量
total processes #总进程数量
max active processes #最大的活跃进程数量(FPM启动开始计算)
max children reached #程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量过小,可以适当调整。

5.PHP-FPM配置文件

[[email protected] ~]# cat /etc/php-fpm.d/www.conf
[global]
pid = /var/run/php-fpm.pid

error_log = /var/log/php/php-fpm.log
log_level = warning
rlimit_files = 655350
events.mechanism = epoll

[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0660

listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 512
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.process_idle_timeout = 15s;
pm.max_requests = 2048
pm.status_path = /phpfpm_status

#php-www模块错误日志
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php/php-www.log
php_admin_flag[log_errors] = on

#php慢查询日志
request_slowlog_timeout = 5s
slowlog = /var/log/php/php-slow.log

扫码芷若 获取免费视频学习资料

编程学习

查 看2019高级编程视频教程免费获取