在 CentOS 上如果 PHP 运行/响应慢,可以从 PHP 本身、Web 服务器、系统、代码 四个层面来优化。下面按「最常见 → 高级」给你一套实用提速方案。
top
htop
php-fpm status
重点看:
php-fpm 进程阻塞CentOS + Nginx/Apache 推荐:
yum install php-fpm
yum install php-opcache
php.ini 中:
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
✅ 一般能提速 30%~200%
路径示例:
/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
查看内存后调整:
ps -ylC php-fpm --sort:rss
fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=phpcache:100m inactive=60m;
fastcgi_cache phpcache;
fastcgi_cache_valid 200 60m;
Apache:
event MPMmod_cacheSHOW PROCESSLIST;
或开启:
slow_query_log=1
yum install redis
PHP:
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
systemctl disable firewalld # 如不用
systemctl disable postfix
ulimit -n 65535
/etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
file_get_contentscomposer autoload如果你只想“先快一点”:
php-opcachephp-fpm pm如果你愿意,可以告诉我:
我可以直接给你一份针对性配置文件。