Ubuntu LAMP监控实现方法
Ubuntu LAMP(Linux+Apache+MySQL+PHP)环境的监控需覆盖系统资源、Web服务、数据库、应用性能等多个维度,以下是具体的监控方案及实现步骤:
通过Ubuntu自带的图形化工具快速查看系统资源使用情况。操作路径:点击左上角“活动”→ 搜索“系统监控”→ 打开即可查看CPU、内存、磁盘、网络的实时利用率及进程列表。
top命令实时显示进程的CPU、内存占用(按M按内存排序、P按CPU排序);htop是增强版(需安装:sudo apt install htop),支持鼠标操作、颜色高亮,更直观。vmstat(系统活动报告)监控虚拟内存、磁盘IO、CPU负载(vmstat 1每秒刷新);iostat(磁盘IO统计)需安装sysstat包(sudo apt install sysstat),用于分析磁盘读写性能;sar(系统活动记录器)可收集历史数据(sar -u 1 3查看CPU过去3次采样)。sudo nano /etc/apache2/mods-enabled/status.conf),取消ExtendedStatus On和<Location /server-status>段的注释,重启Apache(sudo systemctl restart apache2);通过浏览器访问http://服务器IP/server-status?auto查看实时状态。sudo apt update && sudo apt install prometheus;配置:编辑/etc/prometheus/prometheus.yml,添加监控目标(如本地服务器- targets: ['localhost:9090']);启动:sudo systemctl start prometheus,访问http://服务器IP:9090查看指标。wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -;echo "deb https://packages.grafana.com/oss/ubuntu $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/grafana.list),然后sudo apt install grafana;启动:sudo systemctl start grafana-server,访问http://服务器IP:3000(默认账号admin/admin),导入Prometheus数据源并选择LAMP监控模板(如“LAMP Server Monitoring”)。Nagios用于监控主机、服务状态,支持报警(邮件、短信)。安装:下载Nagios Core(wget http://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.6.tar.gz),解压后编译安装(./configure --with-httpd-conf=/etc/apache2/sites-enabled;make all && make install);配置:编辑/usr/local/nagios/etc/nagios.cfg开启监控,添加LAMP服务检查(如Apache存活、MySQL响应时间);启动:sudo systemctl start nagios3,访问http://服务器IP:8080查看状态。
Zabbix支持分布式监控、自动发现,适合大规模环境。安装:添加Zabbix仓库(wget https://repo.zabbix.com/zabbix/5.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.4-1ubuntu20.04_all.deb;dpkg -i zabbix-release_*.deb;apt update),安装服务器及前端(sudo apt install zabbix-server-mysql zabbix-frontend-php zabbix-apache-conf zabbix-agent);配置数据库:创建Zabbix数据库(mysql -u root -p;CREATE DATABASE zabbix CHARACTER SET utf8 COLLATE utf8_bin;;授权用户),导入初始数据(zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -u zabbix -p zabbix);修改配置文件(/etc/zabbix/zabbix_server.conf设置数据库连接),启动服务(sudo systemctl start zabbix-server zabbix-agent),访问http://服务器IP:80登录(默认账号admin/zabbix)。
mysqladmin -u root -p status显示连接数、查询数)、刷新权限(mysqladmin flush-privileges)。performance_schema数据库获取详细性能指标(如慢查询、锁等待)。phpinfo.php文件(<?php phpinfo(); ?>),放置在Web根目录(如/var/www/html),访问http://服务器IP/phpinfo.php查看PHP配置(如memory_limit、max_execution_time)、加载的扩展(如mysqli、opcache)。xdebug.ini(sudo apt install php-xdebug)。使用psutil库编写Python脚本,监控系统资源并发送报警。示例代码:
import psutil
import smtplib
from email.mime.text import MIMEText
# 获取系统资源
cpu_usage = psutil.cpu_percent(interval=1)
mem_usage = psutil.virtual_memory().percent
disk_usage = psutil.disk_usage('/').percent
# 报警阈值
thresholds = {'cpu': 80, 'mem': 85, 'disk': 90}
# 发送邮件报警
def send_alert(resource, usage):
msg = MIMEText(f"{resource} usage exceeded threshold: {usage}%")
msg['Subject'] = f"Alert: {resource} High Usage"
msg['From'] = 'your_email@example.com'
msg['To'] = 'admin@example.com'
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login('your_email@example.com', 'password')
server.send_message(msg)
# 检查阈值
for resource, threshold in thresholds.items():
if locals()[f"{resource}_usage"] > threshold:
send_alert(resource, locals()[f"{resource}_usage"])
将脚本添加到cron定时任务(crontab -e添加*/5 * * * * /usr/bin/python3 /path/to/script.py),每5分钟执行一次。
以上方案可根据需求组合使用:基础监控用系统自带工具,可视化用Prometheus+Grafana,报警用Nagios/Zabbix,应用层用MySQL/PHP工具,自定义需求用脚本。