原生态部署librenms

为什么写这个?

1、别的帖子都要钱,我真看不惯。

2、要了钱程序还搭不起来,恶心。

3、法布施是智慧聪明才艺地修因。

正题开始:

一、部署目标

本次 LibreNMS 部署以官方推荐架构为基础,目标是搭建一套结构清晰、运行稳定、便于后续维护的监控系统。

1.1 架构目标

系统运行在 Ubuntu Server 24.04 上,整体采用传统、成熟的部署方式:

Web 服务使用 Nginx;

PHP 通过 PHP-FPM 独立进程池运行,避免与其他站点相互影响;

Database后端数据库使用 MariaDB;

LibreNMS 通过源码方式部署,不使用 Docker 或容器化方案。

所有配置均来源于官方文档推荐做法,不引入额外优化或魔改。

1.2 访问与安全目标

在满足功能需求的同时,部署过程兼顾基本的安全性要求:

Web 访问统一通过 HTTPS,证书使用自签名方式

禁用 Nginx 默认站点,仅暴露 LibreNMS 服务

LibreNMS 程序及相关任务 不以 root 用户运行

1.3 功能目标

部署完成后,LibreNMS 应达到以下运行状态:

Web 界面可正常访问,并顺利完成初始化安装

Scheduler 与 Cron 服务正常运行,定时任务无报错

SNMP 服务可用,能够成功监控本机设备

设备数据采集正常,图表能够持续生成且无异常

1.4 实施目标

我所在单位目前有两栋楼,每栋楼有三个网络,因为安全考虑,全部是物理分离的,我用的是vitualbox,导出导入虚拟机,使得迁移很方便。

二、系统时间与时区配置

确保系统时间、时区和 NTP 同步正常,避免后续数据采集、RRD 绘图出现时间错乱。

bash 复制代码
sudo timedatectl set-timezone Asia/Shanghai
sudo timedatectl set-ntp true
sudo timedatectl status

代码实例:

bash 复制代码
vboxuser@nms:~$ timedatectl set-timezone Asia/Shanghai
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-timezone ====
Authentication is required to set the system timezone.
Authenticating as: vboxuser
Password: 
==== AUTHENTICATION COMPLETE ====

vboxuser@nms:~$ timedatectl set-ntp true
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-ntp ====
Authentication is required to control whether network time synchronization shall be enabled.
Authenticating as: vboxuser
Password: 
==== AUTHENTICATION COMPLETE ====

vboxuser@nms:~$ timedatectl status
               Local time: Wed 2026-05-13 16:22:03 CST
               Universal time: Wed 2026-05-13 08:22:03 UTC
               RTC time: Wed 2026-05-13 08:22:03
               Time zone: Asia/Shanghai (CST, +0800)
           System clock synchronized: yes
               NTP service: active
               RTC in local TZ: no

三、安装基础依赖

安装 LibreNMS 运行所需的所有系统组件,包括 Web、数据库、PHP、SNMP、RRDTool 等。

bash 复制代码
sudo apt install -y 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 \
python3-command-runner python3-dotenv python3-pip \
python3-psutil python3-pymysql python3-redis \
python3-setuptools python3-systemd \
rrdtool snmp snmpd traceroute unzip whois vim

四、创建 librenms 系统用户(***必须***)

bash 复制代码
# 创建librenms用户
sudo useradd librenms -d /opt/librenms -M -r -s "$(which bash)"

# 查看librenms用户是否创建成功
sudo cat /etc/passwd

备注:

LibreNMS 官方要求使用独立用户运行程序,避免使用 root,提升安全性。

五、下载 LibreNMS 程序

bash 复制代码
# 查看当前位置
pwd

# 跳转目录到/opt
cd /opt

# 通过官方 GitHub 仓库获取最新稳定代码。
sudo git clone https://github.com/librenms/librenms.git

# 如果网速较慢,推荐使用它只下载最新的快照,不包含历史版本。
git clone --depth 1 https://github.com/librenms/librenms.git

# 设置目录权限(官方原样)
sudo chown -R librenms:librenms /opt/librenms

sudo chmod 771 /opt/librenms

sudo setfacl -d -m g::rwx \
/opt/librenms/rrd \
/opt/librenms/logs \
/opt/librenms/bootstrap/cache/ \
/opt/librenms/storage/

sudo setfacl -R -m g::rwx \
/opt/librenms/rrd \
/opt/librenms/logs \
/opt/librenms/bootstrap/cache/ \
/opt/librenms/storage/

六、安装 PHP 依赖

bash 复制代码
# 修改librenms用户密码
sudo passwd librenms

# 切换用户
su - librenms

# 查看当前目录【***很关键***】
pwd

# 当前目录为:/opt/librenms

# 安装 LibreNMS 所需的 PHP 库,仅安装生产环境所需组件。
./scripts/composer_wrapper.php install --no-dev

看到如下提示为成功:

bash 复制代码
   INFO  Blade templates cached successfully.  
> @php artisan optimize
   INFO  Caching framework bootstrap, configuration, and metadata.  
  config ....................................................... 562.56ms DONE
  events ............................................................ 1.72ms DONE
  routes ....................................................... 102.78ms DONE
  views ........................................................ .600.07ms DONE
> @php artisan config:clear
   INFO  Configuration cache cleared successfully.

七、配置 PHP

确保 PHP CLI 与 PHP-FPM 使用正确时区,否则会影响图表与数据采集。

编辑以下两个文件,每个都要改:

bash 复制代码
sudo vi /etc/php/8.3/fpm/php.ini
sudo vi /etc/php/8.3/cli/php.ini

设置:

bash 复制代码
date.timezone = Asia/Shanghai

提示:

在命令行模式下使用/date.timezone进行所搜,找到了按回车,光标定位。

八、配置 MariaDB

按照 LibreNMS 官方建议优化 MariaDB 行为,避免大小写及 InnoDB 问题。

编辑配置文件:

bash 复制代码
sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf

在 [mysqld] 下添加:

bash 复制代码
innodb_file_per_table=1
lower_case_table_names=0

初始化数据库

bash 复制代码
sudo systemctl enable mariadb
sudo systemctl restart mariadb

登录数据库

sudo mysql -u root

创建数据库

CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

创建用户

CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'password';

将数据库授权给用户

GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';

强制刷新权限

FLUSH PRIVILEGES;

退出

EXIT;

配置代码复现,如下:

bash 复制代码
vboxuser@nms:~$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
vboxuser@nms:~$ 
vboxuser@nms:~$ 
vboxuser@nms:~$ 
vboxuser@nms:~$ sudo mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.11.14-MariaDB-0ubuntu0.24.04.1 Ubuntu 24.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'password';
Query OK, 1 row affected (0.018 sec)

MariaDB [(none)]> CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.011 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';
Query OK, 0 rows affected (0.004 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> EXIT;
Bye

九、配置 PHP-FPM

bash 复制代码
# 复制配置文件模版
sudo cp /etc/php/8.3/fpm/pool.d/www.conf /etc/php/8.3/fpm/pool.d/librenms.conf

# 编辑配置文件:
sudo vi /etc/php/8.3/fpm/pool.d/librenms.conf

修改为:

bash 复制代码
[librenms]
user = librenms
group = librenms
listen = /run/php-fpm-librenms.sock

重启服务:

bash 复制代码
sudo systemctl restart php8.3-fpm

备注:

本步骤的主要目的是:为 LibreNMS 创建独立的 PHP-FPM 池,避免与其他站点冲突。

十、配置 HTTPS

在内网环境下,使用 自签名证书 实现 HTTPS 访问。

创建文件夹cert

sudo mkdir -p /etc/nginx/certs/

进入文件夹cert

cd /etc/nginx/certs/

一键生成自签名的 SSL 证书和私钥

sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \

-keyout librenms.key \

-out librenms.crt \

-subj "/C=CN/ST=Shanghai/L=Shanghai/O=LibreNMS/OU=IT/CN=librenms.local"

代码复现,如下:

bash 复制代码
vboxuser@nms:/etc/nginx/certs$ sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \
-keyout librenms.key \
-out librenms.crt \
-subj "/C=CN/ST=Shanghai/L=Shanghai/O=LibreNMS/OU=IT/CN=librenms.local"
...+.+........+.......+.........+.........+..+.+.....+...+....+......+..+.........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+.....+.+.....+......+.......+..+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...+.........+.+..+.......+...........+..........+.....+.......+.............................+......+....+...+...+.....+...+.........+..........+.....+.+...............+..+.......+..+.+.........+.....+.+...+....................+.+.....+................+..+.+.....................+...+........+.......+.....+......+......+.+........+....+...............+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
...+....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+.....+.+..+...+...+....+..+...+............+................+.........+...+..+...+...+...+.........+...+..........+..+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+..+..........+...+......+...+..+......+....+......+........+.+......+..+......+.......+........+......+..........+......+...+..+.......+..+.+............+..+...+.+.....+.+...+...+........+......+..................+.......+........+.......+.......................+...+....+.....................+...+..+.......+...+.........+.....+......+.......+..+.+........+.......+...+..+.........+...+............+...+......+.........+...+...+....+..+....+.....+............+...+.......+.....+..........+...+...........+.+...+......+.....+...+...+.............+.....+....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
vboxuser@nms:/etc/nginx/certs$ ll
total 16
drwxr-xr-x 2 root root 4096 May 13 20:10 ./
drwxr-xr-x 9 root root 4096 May 13 19:48 ../
-rw-r--r-- 1 root root 1350 May 13 20:10 librenms.crt
-rw------- 1 root root 1704 May 13 20:10 librenms.key
vboxuser@nms:/etc/nginx/certs$ 

十一、配置 Nginx

通过 Nginx 提供 HTTPS Web 访问,并将 PHP 请求转交给 PHP-FPM。

sudo vi /etc/nginx/conf.d/librenms.conf

推荐完整配置(HTTPS + 1886 端口):

如果粘贴带格式,推荐使用secureCRT + nano

sudo nano /etc/nginx/conf.d/librenms.conf

server {

listen 1886 ssl;

server_name 192.168.1.152;

ssl_certificate /etc/nginx/certs/librenms.crt;

ssl_certificate_key /etc/nginx/certs/librenms.key;

root /opt/librenms/html;

index index.php;

location / {

try_files uri uri/ /index.php?$query_string;

}

location ~ \.php$ {

include fastcgi.conf;

fastcgi_pass unix:/run/php-fpm-librenms.sock;

}

location ~ /\.(?!well-known).* {

deny all;

}

}

禁用默认站点并重启服务:

bash 复制代码
sudo rm /etc/nginx/sites-enabled/default /etc/nginx/sites-available/default

备注:

这里一定是rm命令,而不是mv命令,这里我惯性思维用了mv,报错了。

bash 复制代码
sudo systemctl restart nginx
sudo systemctl restart php8.3-fpm

十二、启用 lnms 命令补全

提升命令行操作体验,支持 Tab 自动补全。

bash 复制代码
sudo ln -s /opt/librenms/lnms /usr/bin/lnms

sudo cp /opt/librenms/misc/lnms-completion.bash /etc/bash_completion.d/

十三、配置 SNMP(监控本机)(可选)

通过 SNMP 将本机作为第一台被监控设备。

bash 复制代码
sudo cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf

sudo vi /etc/snmp/snmpd.conf

将:RANDOMSTRINGGOESHERE 修改为:public123

这是本服务器的snmp密钥,后续在NMS添加的时候添加这个key启动服务:

bash 复制代码
sudo systemctl enable snmpd

systemctl restart snmpd

备注:

这步我没有做,直接跳过了,因为我现场有大量的h3c交换机,直接就管理起来了。

十四、配置 Cron 与 Scheduler

目的:确保设备发现、数据采集、告警等任务按计划自动执行。

bash 复制代码
# 复制cron到librenms目录下
sudo cp /opt/librenms/dist/librenms.cron /etc/cron.d/librenms

# 复制service和timer到system目录下
sudo cp /opt/librenms/dist/librenms-scheduler.service /opt/librenms/dist/librenms-scheduler.timer /etc/systemd/system/

# 激活timer
sudo systemctl enable librenms-scheduler.timer

# 打开timer
sudo systemctl start librenms-scheduler.timer

# 确认timer状态:
sudo systemctl list-timers | grep librenms

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

十五、Web 安装向导

打开浏览器,访问:

https://192.168.1.152:1886/install

configure databse

Host:localhost

Port:3306

Unix-Socket:

User:librenms

Password:password

Database Name:librenma

create admin user

librenms

librenms

按照页面提示完成数据库、账号等配置即可。

十六、解决检查报错

首次登录 LibreNMS ,进入 Web 界面右上角的 配置检查,通常会看到几条系统检查告警。

这些告警大多属于初始化阶段的正常现象。

未添加设备告警:

是没有添加设备的原因,添加设备就好了。

base_url 相关告警

配置路径:Glocal Settings - system - server ,base_url

编辑配置文件.env:

sudo nano /opt/librenms/.env

su - librenms lnms config:cache

备注:

我一般喜欢reboot一下服务器,校验是不是正常。

再检查一下,报错都没了。完成!

检查安装配置是否正确

方法一:前台网页

https://192.168.1.152:1886/validate

方法二:后台代码

cd /opt/librenms./validate.php

代码复现,如下:

bash 复制代码
librenms@nms:~$ ./validate.php
===========================================
Component | Version
--------- | -------
LibreNMS  | 26.4.1 (2026-05-13T10:53:47+08:00)
DB Schema | 2026_04_25_154814_move_operation_mute_to_operations_table (384)
PHP       | 8.3.6
Python    | 3.12.3
Database  | MariaDB 10.11.14-MariaDB-0ubuntu0.24.04.1
RRDTool   | 1.7.2
SNMP      | 5.9.4.pre2
===========================================

[OK]    Composer Version: 2.9.8
[OK]    Dependencies up-to-date.
[WARN]  You have no devices.
        [FIX]: 
        Consider adding a device such as localhost: /addhost
[OK]    Database Connected
[OK]    Database Schema is current
[OK]    SQL Server meets minimum requirements
[OK]    lower_case_table_names is enabled
[OK]    MySQL engine is optimal
[OK]    Database and column collations are correct
[OK]    Database schema correct
[OK]    MySQL and PHP time match
[OK]    Locks are functional
[OK]    Python poller wrapper is polling
[OK]    Redis is unavailable
[OK]    rrd_dir is writable
[OK]    rrdtool version ok

重要提示:***************如果服务器断电或重启,启动Librenms**************

bash 复制代码
# start MariaDB
sudo systemctl enable --now mariadb

sudo systemctl start mariadb

# start Nginx
sudo systemctl enable --now nginx

sudo systemctl start nginx

# start LibreNMS
sudo systemctl enable --now librenms-scheduler.timer

sudo systemctl start librenms-scheduler.timer

# start SNMP
sudo systemctl enable --now snmpd

sudo systemctl start snmpd

备注:

systemctl命令的格式如下:以snmpd为例

sudo systemctl enable snmpd 激活

sudo systemctl start snmpd 启动

sudo systemctl restart snmpd 重启

sudo systemctl status snmpd 查看

***************如果服务器断电或重启,启动Librenms**************

启动完成后,浏览器访问:

https://192.168.1.152:1886

libre

Libre@nms

重要提示:如果管理员账号无法登录,新增管理员账号即可,命令如下:

bash 复制代码
php lnms user:add libre --role=admin --password=Lfac@2206

原来的管理员信息,删不删除,随你,但是新增的时候,不要和原来管理员重名,否则失败。

注意:

1、一定是HTTPS: 安全的超文本传输协议,数据经过加密,通常使用 443 端口。

2、端口不是默认的8000,而是修改后的1886。

尊重:原文地址

https://www.kd010.com/hyzs/2434.html

相关推荐
tryqaaa_9 小时前
学习日志(三)【php语法学习,iscc校赛wp】
android·网络协议·学习·安全·web安全·web
汤愈韬10 小时前
IP安全 SEC VPN_2
网络·网络协议·安全·网络安全·security
Seven9711 小时前
输入网址到网页显示发生了什么
http
晨晖211 小时前
项目上传到gitee的两种方式,ssh和https
https·gitee·ssh
是三旬老汉。11 小时前
从传感器到推理端:VLA 机器人 TCP 通信与 msgpack 序列化深度解析
python·网络协议·tcp/ip·机器人
marsh020612 小时前
55 openclaw协议扩展:支持非HTTP协议的通信方式
网络·网络协议·http·青少年编程
Aaswk12 小时前
网络体系结构 | 物理层:传输介质与编码
网络·网络协议·tcp/ip
守护安静星空12 小时前
websocket
网络·websocket·网络协议
yqcoder12 小时前
Web 世界的基石:深入解析 HTTP/1.1 的六大核心特点
前端·网络协议·http