以下是CentOS LAMP(Linux+Apache+MySQL+PHP)性能调优的核心秘籍,涵盖硬件、系统、服务及监控层面:
/etc/sysctl.conf
,设置vm.swappiness=10
减少交换,net.ipv4.tcp_tw_reuse=1
优化TCP连接复用。/etc/security/limits.conf
,增大nofile
值以支持高并发。worker
/event
模式,静态内容选prefork
,调整MaxRequestWorkers
(建议设置为内存的10%-15%)。mod_cache
+mod_disk_cache
缓存静态内容,mod_deflate
启用Gzip压缩减少传输体积。KeepAlive
并设置MaxKeepAliveRequests=100
、KeepAliveTimeout=5
,平衡连接复用与资源占用。innodb_buffer_pool_size
设为服务器内存的50%-80%,innodb_log_file_size
设为256M-512M。query_cache_type=1
,query_cache_size=64M
(注意:MySQL 8.0+已移除查询缓存,需用应用层缓存替代)。EXPLAIN
分析慢查询。max_connections
匹配服务器负载,关闭不必要的二进制日志(如非主从复制场景)。php-opcache
,在php.ini
中设置opcache.enable=1
,opcache.memory_consumption=128
(根据内存调整)。SELECT *
,只查询必要字段。php.ini
中memory_limit
(如256M)、max_execution_time
,关闭display_errors
生产环境。mod_expires
设置静态文件缓存时间(如CSS/JS缓存1年),降低服务器负载。top
/htop
监控CPU/内存,vmstat
/iostat
分析系统负载,mysqltuner
优化数据库参数。/var/log/httpd/error_log
)和MySQL慢查询日志,定位性能瓶颈。参考来源:[1,2,3,4,6,7,8,9,10,11,12,13,14]