LNMP是指Linux、Nginx、MySQL和PHP的组合,常用于搭建网站和Web应用。要监控系统状态,可以采用以下几种方法:
可以使用 top
、htop
或 vmstat
等命令来查看CPU、内存、磁盘和网络使用情况。
sudo systemctl status nginx
检查Nginx服务状态。sudo systemctl status mysql
检查MySQL服务状态。sudo systemctl status php-fpm
检查PHP-FPM服务状态。通过查看Nginx、MySQL和PHP的错误日志来分析错误和异常。例如:
sudo tail -n 10 /var/log/nginx/error.log
sudo tail -n 10 /var/log/mysql/error.log
sudo tail -n 10 /var/log/php7.x/error.log
(路径可能因PHP版本不同而异)。可以使用 Zabbix、Prometheus 和 Grafana 等监控工具来收集并展示系统指标。
可以编写脚本定期检查服务状态,并在检测到服务异常时自动重启服务。例如,下面的脚本可以检查Nginx、MySQL和PHP-FPM服务是否运行,并在必要时重启它们:
#!/bin/bash
# 获取当前时间
echo "$(date +'%Y-%m-%d %H:%M:%S')"
# 检查php-fpm服务是否运行
if ! netstat -an | grep php-cgi | wc -l; then
# 重启php-fpm服务
/etc/init.d/php-fpm restart
# 写入日志
echo "$(date) php-fpm service is down... restart..." >> /home/checkfail-lnmp.log
fi
# 检查mysql服务是否运行
if ! netstat -anpt | grep mysqld | awk '{print $4}' | wc -l; then
# 重启mysql服务
/etc/init.d/mysql restart
# 写入日志
echo "$(date) mysqld service is down... restart..." >> /home/checkfail-lnmp.log
fi
# 检查nginx服务是否运行
if ! netstat -anpt | grep nginx | awk '{print $4}' | wc -l; then
# 重启nginx服务
/etc/init.d/nginx restart
# 写入日志
echo "$(date) nginx service is down... restart..." >> /home/checkfail-lnmp.log
fi
为脚本添加执行权限,并使用 crontab
设置每分钟检查一次:。
通过上述方法,可以有效地监控LNMP环境的运行状态,确保服务的稳定性和可靠性。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: linux怎么排查内存占用高的原因