优化CentOS的PHP执行速度可从以下方面入手:
安装并配置OPCache
sudo yum install php-opcache
/etc/php.ini
,启用OPCache并设置内存大小、缓存文件数等参数,如:opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
sudo systemctl restart php-fpm
。优化PHP-FPM配置
/etc/php-fpm.d/www.conf
参数,如pm.max_children
(根据服务器资源设置)、pm.start_servers
等,匹配负载情况。pm=dynamic
。优化php.ini设置
memory_limit=256M
(根据需求设置)。disable_functions
中添加如exec, shell_exec
等(需根据实际需求)。display_errors=Off
,生产环境建议记录日志而非直接输出。使用缓存系统
php-pecl-memcached
。代码级优化
Web服务器优化
系统内核优化
/etc/sysctl.conf
参数,如vm.swappiness=10
减少内存交换,net.ipv4.tcp_tw_reuse=1
优化TCP连接。升级PHP版本
注意:修改配置前需备份文件,建议在测试环境验证后再应用到生产环境。