一、系统自带工具监控
使用systemctl(Ubuntu 16.04及以上版本)或service(旧版本)命令查看LNMP各组件的运行状态:
sudo systemctl status nginx(systemctl)或sudo service nginx status(service),输出中的“active (running)”表示服务正常;sudo systemctl status mysql(或mariadb);sudo systemctl status php7.x-fpm(7.x替换为实际PHP版本,如php8.2-fpm)。sudo systemctl start <服务名>启动。top命令实时显示系统中各进程的CPU、内存占用率(按M键按内存排序,P键按CPU排序);htop是其增强版(需安装:sudo apt install htop),提供更友好的界面和颜色标识,便于快速识别高资源消耗进程。ps aux | grep <进程名>过滤特定进程,如ps aux | grep nginx查看Nginx进程,ps aux | grep php-fpm查看PHP-FPM进程,确认进程是否存在。使用netstat(传统工具)或ss(更现代的工具)检查服务是否监听预期端口:
sudo netstat -tuln | grep ':80\|:443' 或 sudo ss -tuln | grep ':80\|:443';sudo netstat -tuln | grep ':3306';sudo netstat -tuln | grep ':9000'。查看各组件的错误日志定位问题:
/var/log/nginx/error.log(记录HTTP请求错误、配置解析错误等);/var/log/mysql/error.log(记录数据库启动错误、查询错误等);/var/log/php7.x-fpm.log(7.x替换为实际版本,记录PHP脚本错误、FPM进程异常等)。tail -f /var/log/<服务名>/error.log实时查看最新日志。二、第三方监控工具
企业级开源监控解决方案,支持监控服务器性能、网络状况、LNMP服务状态,可实现报警(邮件、短信)和可视化 dashboard。
sudo apt install zabbix-server-mysql zabbix-frontend-php;zabbix数据库并导入初始数据);sudo systemctl start zabbix-server;http://<服务器IP>/zabbix完成web配置(添加主机、配置监控项)。开源监控组合,Prometheus负责收集时间序列数据(如CPU、内存、服务状态),Grafana负责数据可视化(如 dashboard 展示)。
prometheus.yml(添加node_exporter、nginx_exporter、mysqld_exporter作为监控目标);sudo apt install grafana,启动后访问http://<服务器IP>:3000(默认账号admin/admin);实时系统监控工具,自动检测系统资源(CPU、内存、磁盘、网络)和服务状态,提供直观的 web 图表,无需复杂配置。
bash <(curl -Ss https://my-netdata.io/kickstart.sh),安装完成后访问http://<服务器IP>:19999查看监控页面。老牌开源监控系统,支持监控服务器、网络设备、应用程序,可通过插件扩展功能(如检查Nginx响应时间、MySQL查询性能)。
sudo apt install nagios3;/usr/local/nagios/etc/objects/hosts.cfg添加服务器,services.cfg添加监控项);三、LNMP组件专用监控
启用Nginx的stub_status模块查看基本状态(如活跃连接数、请求数):
/etc/nginx/sites-available/default),添加:location /nginx_status {
stub_status on;
allow 127.0.0.1; # 仅允许本地访问
deny all;
}
sudo systemctl restart nginx;http://<服务器IP>/nginx_status查看状态(需替换为实际IP)。使用mysqladmin命令查看MySQL服务器状态:
mysql -u root -p;SHOW STATUS;查看所有状态变量(如Threads_connected表示当前连接数,Uptime表示运行时间);mysqladmin -u root -p status(输出包括连接数、查询次数、Uptime)。通过php-fpm的status页面查看进程池状态:
/etc/php/7.x/fpm/pool.d/www.conf,7.x替换为实际版本),开启pm.status_path:pm.status_path = /status
/etc/nginx/sites-available/default):location /php_status {
fastcgi_pass 127.0.0.1:9000; # PHP-FPM监听端口
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
allow 127.0.0.1;
deny all;
}
sudo systemctl restart php7.x-fpm && sudo systemctl restart nginx;http://<服务器IP>/php_status查看状态(需替换为实际IP)。四、自动化监控与报警 编写Shell脚本定期检查LNMP服务状态,若异常则重启服务并发送报警(如邮件、钉钉消息):
#!/bin/bash
# 获取当前时间
timestamp=$(date +'%Y-%m-%d %H:%M:%S')
# 检查并重启php-fpm
if ! systemctl is-active --quiet php7.x-fpm; then
systemctl restart php7.x-fpm
echo "$timestamp php-fpm服务异常,已重启" >> /var/log/lnmp_monitor.log
# 发送报警(示例:邮件)
echo "php-fpm服务异常,已重启" | mail -s "LNMP监控报警" your_email@example.com
fi
# 检查并重启mysql
if ! systemctl is-active --quiet mysql; then
systemctl restart mysql
echo "$timestamp mysql服务异常,已重启" >> /var/log/lnmp_monitor.log
echo "mysql服务异常,已重启" | mail -s "LNMP监控报警" your_email@example.com
fi
# 检查并重启nginx
if ! systemctl is-active --quiet nginx; then
systemctl restart nginx
echo "$timestamp nginx服务异常,已重启" >> /var/log/lnmp_monitor.log
echo "nginx服务异常,已重启" | mail -s "LNMP监控报警" your_email@example.com
fi
/usr/local/bin/lnmp_monitor.sh,赋予执行权限:chmod +x /usr/local/bin/lnmp_monitor.sh;crontab -e,添加*/5 * * * * /usr/local/bin/lnmp_monitor.sh。