Apache PHP运行环境优化实操指南
一 架构与模块选择
apache2ctl -V | grep -i mpma2dismod mpm_prefork && a2enmod mpm_event && systemctl restart apache2二 Apache 关键调优
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 10000
</IfModule>
三 PHP 与 OPcache 配置
[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0 ; 生产建议关闭文件时间戳校验
opcache.revalidate_freq=60
opcache.fast_shutdown=1
memory_limit=128M~256M(按应用调优)max_execution_time=30(长任务考虑异步/队列)upload_max_filesize=20M、post_max_size=32M(按业务需求)display_errors=Off、log_errors=On、error_log=/var/log/php_errors.logphp-mysqlnd、php-curl、php-gd、php-mbstring、php-xml、php-zip、php-opcache。四 PHP-FPM 进程与通信
SetHandler "proxy:fcgi://127.0.0.1:9000"SetHandler "proxy:unix:/run/php/phpX.Y-fpm.sock|fcgi://localhost"[www]
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
listen = 127.0.0.1:9000
; 或使用 Unix Socket:
; listen = /run/php/phpX.Y-fpm.sock
pm.max_children 依据“可用内存 / 单进程内存”估算,并预留系统与其他服务内存。五 缓存、数据库与监控落地
htop、top、apachetop