LibreNMS 网络监控系统搭建教程

LibreNMS 是一款基于 PHP 的开源网络监控系统,从 Observium 社区版分支而来,提供自动发现、SNMP 监控、流量图表、告警通知、API 接口等丰富功能。它支持监控路由器、交换机、服务器、防火墙等各种网络设备,是中小型网络环境首选的免费监控方案。本文将介绍如何在搬瓦工 VPS 上搭建 LibreNMS。

一、系统要求

  • 操作系统:Ubuntu 22.04/24.04 或 Debian 12(推荐)
  • 内存:至少 2GB,监控 100 台以上设备建议 4GB
  • PHP:8.1 或更高版本
  • 数据库:MariaDB 10.5+ 或 MySQL 8.0+
  • Web 服务器:Nginx 或 Apache

二、安装依赖

apt update
apt install software-properties-common
apt install acl curl fping git graphviz imagemagick mariadb-client mariadb-server mtr-tiny nginx-full nmap php-cli php-curl php-fpm php-gd php-gmp php-json php-mbstring php-mysql php-snmp php-xml php-zip rrdtool snmp snmpd unzip python3-pymysql python3-dotenv python3-redis python3-setuptools python3-psutil python3-command-runner whois -y

三、创建 LibreNMS 用户

useradd librenms -d /opt/librenms -M -r -s "$(which bash)"

四、下载 LibreNMS

cd /opt
git clone https://github.com/librenms/librenms.git
chown -R librenms:librenms /opt/librenms
chmod 771 /opt/librenms
setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/
setfacl -R -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/

五、安装 PHP 依赖

su - librenms
./scripts/composer_wrapper.php install --no-dev
exit

六、配置数据库

# 配置 MariaDB
nano /etc/mysql/mariadb.conf.d/50-server.cnf

[mysqld] 段中添加:

innodb_file_per_table=1
lower_case_table_names=0
systemctl restart mariadb

# 创建数据库和用户
mysql -u root -e "CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -u root -e "CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'StrongPassword123';"
mysql -u root -e "GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';"
mysql -u root -e "FLUSH PRIVILEGES;"

七、配置 Nginx

cat > /etc/nginx/sites-available/librenms <<'EOF'
server {
    listen 80;
    server_name librenms.example.com;
    root /opt/librenms/html;
    index index.php;

    charset utf-8;
    gzip on;
    gzip_types text/css application/javascript text/javascript application/x-javascript image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi.conf;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
EOF

ln -s /etc/nginx/sites-available/librenms /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx

八、配置 PHP-FPM

cp /etc/php/8.1/fpm/pool.d/www.conf /etc/php/8.1/fpm/pool.d/librenms.conf
nano /etc/php/8.1/fpm/pool.d/librenms.conf

修改以下配置项:

[librenms]
user = librenms
group = librenms
listen = /run/php-fpm-librenms.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 16
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
systemctl restart php8.1-fpm

九、配置 SNMP

cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf
nano /etc/snmp/snmpd.conf

修改 community string:

# 将 RANDOMSTRINGGOESHERE 替换为你的 community 字符串
com2sec readonly  default         your_community_string
# 下载发行版检测脚本
curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
chmod +x /usr/bin/distro

systemctl enable snmpd
systemctl restart snmpd

十、配置定时任务和日志轮转

# 复制 cron 配置
cp /opt/librenms/dist/librenms.cron /etc/cron.d/librenms

# 配置日志轮转
cp /opt/librenms/misc/librenms.logrotate /etc/logrotate.d/librenms

十一、完成 Web 安装

打开浏览器访问 http://你的服务器IP,按照安装向导完成配置。安装向导会检查环境依赖、数据库连接,并创建管理员账户。

十二、使用 Docker 部署

mkdir -p /opt/librenms-docker && cd /opt/librenms-docker

cat > docker-compose.yml <<'EOF'
version: "3.8"
services:
  db:
    image: mariadb:10.11
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: librenms
      MYSQL_USER: librenms
      MYSQL_PASSWORD: librenmspass
    volumes:
      - db_data:/var/lib/mysql

  librenms:
    image: librenms/librenms:latest
    restart: always
    depends_on:
      - db
    ports:
      - "8000:8000"
    environment:
      DB_HOST: db
      DB_NAME: librenms
      DB_USER: librenms
      DB_PASSWORD: librenmspass
      DB_TIMEOUT: 60
    volumes:
      - librenms_data:/data

volumes:
  db_data:
  librenms_data:
EOF

docker compose up -d

十三、常见问题

13.1 权限错误

# 修复文件权限
chown -R librenms:librenms /opt/librenms
setfacl -R -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/

13.2 运行验证检查

su - librenms
./validate.php

总结

LibreNMS 是功能全面的开源网络监控平台,特别适合需要管理多台网络设备的运维场景。配合 SNMP 监控配置教程 可以实现完整的网络监控体系。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的优惠,通过 bwh81.net 进入官网购买。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。