监控 Linux + Nginx + MySQL + PHP(LNMP) 可以从 系统层、服务层、应用层 三个维度来做。下面是一套比较实用、由浅入深的方案。
目的:看服务器是否活着、资源是否够用
top / htop # CPU、内存
df -h # 磁盘
free -m # 内存
uptime # 负载
iostat -x 1 # 磁盘IO
sar -n DEV 1 # 网络
目的:看访问量、错误率、连接数
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
查看:
curl http://127.0.0.1/nginx_status
关键指标:
tail -f /var/log/nginx/access.log
关注:
可用:
目的:看慢查询、连接、锁
SHOW PROCESSLIST;
SHOW STATUS LIKE 'Threads%';
SHOW GLOBAL STATUS LIKE 'Slow_queries';
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
目的:看 PHP 是否卡死、内存泄漏、慢脚本
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;
}
关注:
request_slowlog_timeout = 2s
slowlog = /var/log/php-fpm/slow.log
目的:服务挂了能马上知道
ps aux | grep nginx
ps aux | grep php-fpm
systemctl status mysql
如果你愿意,我可以: