LAMP (Linux, Apache, MySQL, PHP) 是广泛使用的Web开发环境。以下是针对LAMP堆栈各层的性能优化建议:
内核参数调优
ulimit -n 65536
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.core.somaxconn = 65535
文件系统优化
noatime,nodiratime
挂载选项减少磁盘I/O内存管理
vm.swappiness = 10
MPM模块选择
event
MPM(Apache 2.4+)<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
模块管理
mod_rewrite
, mod_ssl
等按需启用mod_cache
, mod_expires
, mod_deflate
其他配置
KeepAlive On
,但调整超时:KeepAliveTimeout 2
HostnameLookups Off
配置优化
innodb_buffer_pool_size = 4G # 通常设为可用内存的50-70%
innodb_log_file_size = 256M
query_cache_size = 64M
tmp_table_size = 64M
max_connections = 200
索引优化
ANALYZE TABLE
更新统计信息查询优化
OPcache配置
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
PHP-FPM优化
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
代码优化
缓存策略
内容分发
监控与分析
通过综合应用这些优化措施,可以显著提升LAMP环境的性能和响应速度。