1. 系统自带命令行工具
top/htop:实时查看系统进程的资源占用(CPU、内存、进程数)。htop需安装(sudo apt install htop),界面更友好,支持排序和过滤。systemctl:检查LNMP组件(Nginx、MySQL、PHP-FPM)的服务状态,确保其正常运行:sudo systemctl status nginx # 查看Nginx状态
sudo systemctl status mysql # 查看MySQL状态
sudo systemctl status php-fpm # 查看PHP-FPM状态
sudo tail -f /var/log/nginx/error.log # Nginx错误日志
sudo tail -f /var/log/mysql/error.log # MySQL错误日志
sudo tail -f /var/log/php-fpm.log # PHP-FPM错误日志(路径取决于配置)
2. 第三方命令行工具
nmon:综合监控CPU、内存、磁盘I/O、网络等指标,适合深度分析性能瓶颈。安装后运行nmon即可进入交互界面,按c(CPU)、m(内存)、d(磁盘)切换模块。tiptop:文本界面的实时系统监控工具,支持定制监控指标(如tiptop -c cpu,mem仅看CPU和内存),并可设置告警阈值。1. Prometheus + Grafana(指标监控+可视化)
prometheus.yml,添加LNMP监控目标(如Nginx Exporter、MySQL Exporter)。2. Zabbix(分布式监控)
1. Nginx监控
nginx -t检查配置文件语法,使用stub_status模块(需在配置中开启)获取活跃连接数、请求数等指标:location /nginx_status {
stub_status on;
allow 127.0.0.1; # 仅允许本地访问
deny all;
}
访问http://localhost/nginx_status查看结果。2. MySQL监控
mysqladmin查看服务器状态(如mysqladmin -u root -p status),通过SHOW GLOBAL STATUS查询关键指标(如QPS、TPS):SHOW GLOBAL STATUS LIKE 'Queries'; -- 总查询数
SHOW GLOBAL STATUS LIKE 'Threads_running'; -- 当前运行线程数
slow_query_log),使用EXPLAIN优化SQL语句。3. PHP监控
php-fpm.conf中开启pm.status_path(如pm.status_path = /status),通过http://localhost/status查看进程数、请求处理时间等指标。ELK Stack(Elasticsearch + Logstash + Kibana)
input(读取日志文件)、filter(解析字段)、output(发送到Elasticsearch)。