CentOS 上搭建的 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 环境出现卡顿,通常不是单一原因,而是资源、配置、代码、IO等多方面叠加导致的。下面按常见原因逐一说明,并给出排查与优化建议。
top
htop
ps aux | sort -k3 -rn | head
free -h
vmstat 1
Swap 使用很高,说明内存不够✅ 解决:
prefork 模式 + 进程太多CentOS 默认 Apache 多用 prefork:
httpd -V | grep MPM
问题:
✅ 优化(/etc/httpd/conf/httpd.conf 或 conf.modules.d):
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
或改用 event / worker(需 PHP 使用 FPM)
SHOW PROCESSLIST;
或开启慢查询日志:
slow_query_log = 1
slow_query_log_file = /var/log/mariadb/slow.log
long_query_time = 1
✅ 优化:
SELECT *查看:
SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
✅ 一般设为物理内存的 50%~70%
排查:
iostat -x 1
✅ 优化:
CentOS 默认可能未开启:
php -m | grep opcache
✅ 开启:
yum install php-opcache
opcache.enable=1
opcache.memory_consumption=128
排查:
iftop
netstat -an | grep :80 | wc -l
# 看整体
top
# 看谁最忙
ps aux --sort=-%cpu | head
ps aux --sort=-%mem | head
# 看网络
netstat -ant | grep :80 | wc -l
# 看 MySQL
mysqladmin processlist
CentOS LAMP 卡顿,90% 是:Apache 进程太多 + 内存不足 + MySQL 慢查询 + 没开 OPcache
如果你愿意,可以告诉我:
我可以直接帮你针对性的调优配置。