在CentOS环境下对PHP进行调优,可以从多个方面入手,包括配置优化、代码优化、性能监控等。以下是一些具体的步骤和建议:
启用OPcache:
安装OPcache:yum install opcache
配置OPcache(编辑/etc/php.ini
):
[opcache]
zend_extension = /usr/lib64/php/modules/opcache.so
opcache.enable=1
opcache.memory_consumption=64 ;调整内存消耗(MB)
opcache.max_accelerated_files=4000 ;调整最大缓存文件数
opcache.revalidate_freq=2 ;调整重新验证频率(秒)
opcache.fast_shutdown=1 ;启用快速关闭
重启PHP-FPM或Apache:
service php-fpm restart
# 或
service httpd restart
调整PHP-FPM配置:
编辑/etc/php-fpm.d/www.conf
文件,调整以下参数:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.process_idle_timeout = 10
request_terminate_timeout = 120
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
php_value[opcache.save_comments] = 0
php_value[opcache.load_comments] = 0
重启PHP-FPM服务以应用更改:
sudo systemctl restart php-fpm
调整PHP配置文件(php.ini):
memory_limit = 128M
upload_max_filesize
和 post_max_size
safe_mode = Off
disable_functions
在进行任何配置更改前,请务必备份配置文件,并进行充分的性能测试,确保优化措施不会影响系统稳定性。