温馨提示×

如何监控linux lnmp性能

小樊
39
2026-09-09 17:05:58
栏目: 智能运维

监控 Linux + Nginx + MySQL + PHP(LNMP) 的性能,一般分为 系统层、Web层、数据库层、PHP层 四个维度。下面给你一套实用、可落地的监控方案


一、系统层监控(CPU / 内存 / 磁盘 / 网络)

1. 基础命令

top / htop
free -h
df -h
iostat -x 1
vmstat 1
sar -u 1  # 需要 sysstat

2. 长期监控工具

  • node_exporter + Prometheus + Grafana(推荐)
  • Zabbix
  • Netdata(开箱即用,轻量)

示例(Netdata)

bash <(curl -Ss https://my-netdata.io/kickstart.sh)

访问:http://IP:19999


二、Nginx 性能监控

1. 开启 Nginx Status

server {
    location /nginx_status {
        stub_status on;
        allow 127.0.0.1;
        deny all;
    }
}

查看:

curl http://127.0.0.1/nginx_status

指标说明:

  • Active connections
  • Requests per second
  • Reading / Writing / Waiting

2. 日志分析

awk '{print $7}' access.log | sort | uniq -c | sort -nr | head

3. 可视化

  • Nginx Exporter + Prometheus
  • ELK / Grafana Loki

三、MySQL 性能监控

1. 自带命令

SHOW PROCESSLIST;
SHOW STATUS LIKE 'Threads%';
SHOW GLOBAL STATUS;

2. 慢查询

slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1

分析:

mysqldumpslow -s t /var/log/mysql/slow.log

3. 专业工具

  • Prometheus + mysqld_exporter
  • Percona Monitoring and Management (PMM)(强烈推荐)
  • pt-query-digest

四、PHP 性能监控(PHP-FPM)

1. 开启 PHP-FPM Status

pm.status_path = /status

Nginx 配置:

location /php_status {
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

查看:

curl http://127.0.0.1/php_status

关注:

  • active processes
  • idle processes
  • requests

2. PHP 慢日志

slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 2s

3. 高级分析

  • XHProf / Tideways
  • Blackfire
  • opcache 状态页

五、一体化监控方案(推荐)

✅ 中小团队

Netdata / Zabbix

✅ 中大型 / 生产环境

Prometheus + Node Exporter
             + Nginx Exporter
             + MySQL Exporter
             + PHP-FPM Exporter
             + Grafana

✅ 数据库重点

PMM (Percona)

六、常见性能瓶颈排查顺序

  1. CPU 是否跑满(top
  2. 内存是否不足(free
  3. 磁盘 IO 是否瓶颈(iostat
  4. Nginx 连接数是否过多
  5. PHP-FPM 进程是否不够
  6. MySQL 慢查询 / 锁等待

如果你愿意,我可以:

  • ✅ 给你一套 Prometheus + Grafana 的 LNMP 监控配置
  • ✅ 帮你分析 具体性能问题(贴 top / slow log)
  • ✅ 给你一个 生产环境 LNMP 调优清单

你现在是学习 / 线上排查 / 搭建监控平台

0