在 CentOS 上 PHP 运行慢,通常不是“CentOS 本身慢”,而是环境配置、资源、代码或扩展的问题。下面按常见原因 → 排查方法 → 解决建议给你系统梳理。
| 模式 | 性能 |
|---|---|
| mod_php | 一般 |
| php-cgi | 慢 |
| php-fpm | ✅ 推荐 |
✅ 检查:
ps aux | grep php
如果是 php-fpm 最好。
默认配置往往只开几个进程。
查看:
cat /etc/php-fpm.d/www.conf
重点参数:
pm = dynamic
pm.max_children = 5 # 太小
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
✅ 建议(4核8G示例):
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
重启:
systemctl restart php-fpm
没开 OPcache,PHP 每次都重新编译。
检查:
php -m | grep opcache
如果没有:
yum install php-opcache
php.ini:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
PHP 中如果有:
mail()gethostbyname()会卡在 DNS。
✅ 解决:
127.0.0.1 而不是 localhost/etc/resolv.conf 去掉无效 DNSSELinux 会导致文件访问慢。
临时测试:
setenforce 0
如果变快了:
getenforce
✅ 正确做法是配置策略,而不是长期关闭。
free -h
如果有 swap 被大量使用:
top
✅ 解决:
iostat -x 1
如果是云服务器(低 IO 盘),会很明显。
✅ 建议:
用:
yum install php-xdebug
或:
microtime(true)
常见慢点:
file_get_contents如:
APP_ENV=productionphp -v
php -m | grep opcache
ps aux | grep php-fpm
free -h
top
你可以直接贴:
php-fpm.conf 或 www.conf我可以直接帮你定位是哪一层慢。