调优Linux PHP-FPM参数可以显著提高PHP应用程序的性能。以下是一些常见的PHP-FPM参数及其调优建议:
pm 参数pm 参数决定了PHP-FPM如何管理进程池。常见的选项有 dynamic、static 和 ondemand。
dynamic: 根据负载动态调整进程数。
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
static: 固定数量的子进程。
pm = static
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
ondemand: 按需启动子进程。
pm = ondemand
pm.process_idle_timeout = 10s
request_terminate_timeout 参数设置请求的最大执行时间,防止长时间运行的脚本占用资源。
request_terminate_timeout = 30s
memory_limit 参数设置每个PHP进程的内存限制。
memory_limit = 128M
upload_max_filesize 和 post_max_size 参数设置上传文件的最大大小。
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time 参数设置脚本的最大执行时间。
max_execution_time = 30
opcache 参数启用并配置OPcache以提高PHP性能。
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
slowlog 参数启用慢查询日志,帮助识别性能瓶颈。
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 10s
log_level 参数设置日志级别,以便更好地调试和监控。
log_level = notice
catch_workers_output 参数捕获工作进程的输出,便于调试。
catch_workers_output = yes
clear_env 参数清除环境变量,提高安全性。
clear_env = no
top、htop、vmstat 等监控系统资源使用情况。通过以上步骤和参数调整,可以显著提升Linux环境下PHP-FPM的性能。