CentOS 上 PHP 运行慢,通常不是“CentOS 本身慢”,而是环境配置、资源、或代码层面的问题。下面按最常见原因 → 排查方法 → 解决思路给你梳理。
很多人用的是:
mod_php(Apache 内嵌)✅ 推荐:PHP-FPM + Nginx / Apache ❌ 慢:CGI、mod_php 高并发直接崩
检查:
ps aux | grep php-fpm
如果没有 php-fpm,基本就慢。
默认配置:
pm = dynamic
pm.max_children = 5
并发一高就排队。
查看是否排队:
tail -f /var/log/php-fpm/error.log
常见报错:
server reached pm.max_children
✅ 解决:
pm.max_children = 50~200(看内存)
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
CentOS 默认 OPcache 是关的。
查看:
php -m | grep opcache
✅ 开启:
opcache.enable=1
opcache.memory_consumption=128
opcache.revalidate_freq=60
效果:PHP 速度直接翻倍
PHP 里:
gethostbyname()
mail()
curl()
如果 DNS 慢,PHP 直接卡死。
✅ 检查:
nslookup example.com
✅ 解决:
/etc/resolv.confmail()尤其是:
file_put_contents✅ 检查:
iostat -x 1
✅ 优化:
SELinux 开启时,PHP 访问文件会非常慢。
检查:
getenforce
如果是 Enforcing:
setenforce 0
(生产建议配置策略,而不是直接关)
特别是:
PHP 本身快,但:
SELECT * FROM user WHERE name LIKE '%xx%'
✅ 方法:
slow query log
php xhprof / tideways
Laravel / Symfony:
php -v
php -m | grep opcache
ps aux | grep php-fpm
free -m
top
iostat -x 1
你可以直接贴:
我可以给你一套精准优化配置。