在Ubuntu上调优PHP-FPM(FastCGI Process Manager)可以显著提高PHP应用程序的性能。以下是一些常见的调优步骤和建议:
首先,确保你已经安装了PHP-FPM。如果没有安装,可以使用以下命令进行安装:
sudo apt update
sudo apt install php-fpm
PHP-FPM的配置文件通常位于/etc/php/{version}/fpm/pool.d/www.conf
,其中{version}
是你的PHP版本号(例如7.4、8.0等)。
编辑www.conf
文件,调整以下参数:
pm
:选择进程管理模式,常用的有dynamic
、ondemand
和static
。
dynamic
:根据负载动态调整进程数。ondemand
:按需启动进程。static
:固定进程数。pm.max_children
:最大子进程数。pm.start_servers
:启动时的服务器进程数。pm.min_spare_servers
:最小空闲服务器进程数。pm.max_spare_servers
:最大空闲服务器进程数。例如,对于dynamic
模式:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
request_terminate_timeout
:请求超时时间,单位为秒。slowlog_timeout
:慢请求日志超时时间,单位为秒。例如:
request_terminate_timeout = 30s
slowlog_timeout = 30s
编辑/etc/php/{version}/fpm/php.ini
文件,调整以下参数:
memory_limit
:每个PHP进程的内存限制。max_execution_time
:脚本最大执行时间,单位为秒。post_max_size
:POST请求的最大大小。upload_max_filesize
:上传文件的最大大小。例如:
memory_limit = 256M
max_execution_time = 300
post_max_size = 50M
upload_max_filesize = 50M
确保PHP-FPM的日志文件路径正确,并定期检查日志文件以监控性能和错误。
access.log
:访问日志。error.log
:错误日志。在调整配置后,重启PHP-FPM服务以应用更改:
sudo systemctl restart php{version}-fpm
使用工具如htop
、top
、php-fpm-status
等来监控PHP-FPM的性能和资源使用情况。
通过以上步骤,你可以有效地调优PHP-FPM,提高Ubuntu上PHP应用程序的性能。