优化CentOS上PHP-FPM性能可从配置、扩展、代码及硬件等方面入手,具体如下:
php-fpm.conf或www.conf:
dynamic,根据服务器资源设置pm.max_children(建议为CPU核心数×2)、pm.start_servers(通常为max_children的10%-20%)、pm.min/max_spare_servers(建议为CPU核心数×1-2)。pm.max_requests(如500-1000),避免进程长期运行导致内存泄漏。request_terminate_timeout(如30-60秒)和request_slowlog_timeout(如10秒),记录慢请求便于优化。php.ini中启用OPcache,设置opcache.memory_consumption(如64-128M)、opcache.max_accelerated_files(根据项目文件数调整),并开启opcache.enable=1。fastcgi_cache缓存PHP响应,减少PHP-FPM负载。top、htop等工具分析瓶颈。exec、shell_exec),降低安全风险。参考来源: