以下是CentOS LNMP资源占用优化要点:
vm.swappiness=10
减少交换分区使用,net.ipv4.tcp_tw_reuse=1
复用TIME_WAIT连接)。worker_processes
设为CPU核心数,worker_connections
根据内存调整(建议1024-4096)。keepalive
减少连接开销,设置keepalive_timeout=30s
。sendfile
和tcp_nopush
,减少内存拷贝。gzip on
)减少传输数据量。innodb_buffer_pool_size
设为物理内存的50%-70%,key_buffer_size
设为256M-512M。max_connections
匹配并发需求,避免过多连接占用内存。OPTIMIZE TABLE
)和索引,避免全表扫描。pm.max_children
按内存计算(建议每个进程占用20-30M,总内存预留50%给系统),pm.start_servers
设为pm.max_children/2
。request_terminate_timeout
限制脚本执行时间,避免长时间占用资源。SELECT *
)。top
、htop
、vmstat
监控系统资源,mysqltuner
分析MySQL配置。参考来源:[1,2,3,4,5,6,7,9,10,11,12]