以下是优化Ubuntu中PHP脚本执行速度的方法:
启用OPcache:安装扩展并配置php.ini
,缓存编译后的字节码以减少重复解析。
sudo apt install php-opcache
在php.ini
中添加:
zend_extension=opcache.so opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=4000
使用PHP-FPM:替代传统的mod_php
,提升并发处理能力。
sudo apt install php-fpm
调整/etc/php/{version}/fpm/pool.d/www.conf
中的参数,如pm.max_children
、pm.start_servers
等。
优化PHP配置:
memory_limit
、max_execution_time
等参数以适配服务器资源。realpath_cache
减少文件路径解析开销。代码优化:
系统级优化:
vm.nr_hugepages
、net.core.somaxconn
)提升I/O和内存性能。监控与分析:
slowlog
)找出高耗时脚本。其他建议:
操作前建议备份配置文件,并在测试环境验证效果后再应用到生产环境。