在 CentOS 上 PHP 运行慢,通常可以从 系统、PHP 配置、代码、数据库、缓存、资源 几个方向排查和优化。下面按最常见原因 → 解决办法给你一套实用思路。
先别急着改配置,先定位:
top
htop
90% 的 PHP 慢,不开 OPcache 是主因
yum install php-opcache -y
编辑 php.ini(路径一般是 /etc/php.ini 或 /etc/php.d/opcache.ini):
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
重启 PHP:
systemctl restart php-fpm
确认你用的是 php-fpm 而不是 mod_php
ps aux | grep php-fpm
/etc/php-fpm.d/www.conf 关键参数:
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
根据内存调整,别开太多
SHOW VARIABLES LIKE 'slow_query_log';
开启:
slow_query_log = 1
long_query_time = 1
SELECT *EXPLAIN 分析 SQLfile_get_contents() 外部 URLvar_dump / 日志yum install redis -y
systemctl start redis
systemctl list-unit-files | grep enabled
ulimit -n 65535
/etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=phpcache:10m;
event MPMmod_deflateCentOS 默认的 PHP 往往很老(5.4 / 7.2)
建议升级:
yum install epel-release
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php81
yum update php
如果你愿意,可以告诉我:
我可以直接给你针对性优化方案。