Ubuntu LAMP服务器监控方案
系统级监控是LAMP服务器监控的基础,用于掌握服务器整体资源使用情况,避免因资源耗尽导致服务中断。
top/htop:实时显示系统中各个进程的CPU、内存占用情况,htop比top界面更友好(需安装:sudo apt install htop);vmstat:报告虚拟内存、磁盘、CPU等的统计信息(如vmstat 1每秒刷新一次);iostat:监控磁盘I/O操作情况(需安装sysstat包:sudo apt install sysstat);free:查看系统内存使用情况(包括物理内存、交换分区);df:检查文件系统磁盘空间占用(如df -h以人类可读格式显示)。针对LAMP(Linux、Apache、MySQL、PHP)堆栈的各个组件进行深度监控,及时发现组件性能瓶颈或故障。
/etc/apache2/mods-enabled/status.conf,开启ExtendedStatus On并将Allow from all改为允许监控服务器IP,重启Apache后访问http://server-ip/server-status查看;top的工具,实时监控Apache请求处理情况(需安装:sudo apt install apachetop);rewrite、ssl)是否启用。mysqladmin status显示连接数、查询缓存命中率)、刷新权限(mysqladmin flush-privileges);SHOW STATUS;查看全局状态变量(如Threads_connected当前连接数)、SHOW PROCESSLIST;查看当前执行的线程(找出慢查询或卡住的进程);php.ini中开启xdebug.mode=profile);通过可视化工具将监控数据转化为直观的图表,便于实时掌握服务器状态及历史趋势。
sudo apt update && sudo apt install prometheus,编辑/etc/prometheus/prometheus.yml配置监控目标(如本地服务器);wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -,编辑/etc/apt/sources.list.d/grafana.list添加deb https://packages.grafana.com/oss/ubuntu $(lsb_release -cs) main,安装Grafana:sudo apt install grafana;sudo systemctl start prometheus && sudo systemctl start grafana-server,访问http://server-ip:9090(Prometheus)和http://server-ip:3000(Grafana,默认账号admin/admin)配置仪表板。及时获取服务器异常信息,快速响应问题,避免影响业务。
./configure --with-httpd-conf=/etc/apache2/sites-enabled && make all && make install;/etc/nagios/nagios.cfg(开启监控功能),添加服务检查(如commands.cfg中定义check_http命令,services.cfg中添加Apache监控);sudo systemctl restart apache2 && sudo systemctl restart nagios3,访问http://server-ip:8080查看监控状态。/var/log/auth.log),自动禁止恶意IP(如多次SSH登录失败),减少暴力破解风险(需安装:sudo apt install fail2ban,配置/etc/fail2ban/jail.local开启SSH防护);sudo apt install logwatch,配置/usr/share/logwatch/default.conf/services/http.conf过滤Apache错误日志)。日志记录了服务器运行事件,通过分析日志可快速定位故障原因(如Apache错误、MySQL慢查询)。
tail -f /var/log/apache2/access.log:实时查看Apache访问日志(记录请求IP、URL、状态码);tail -f /var/log/apache2/error.log:实时查看Apache错误日志(如404、500错误);journalctl -f:查看systemd日志(涵盖系统启动、服务状态变化);sudo journalctl -f -p err:过滤错误级别日志,快速定位严重问题。/var/log目录下的日志,通过Kibana创建仪表板查看日志趋势。