LibreNMS安装

系统:ubuntu 24.04

一、为什么 Ubuntu 24.04 最适合 LibreNMS

✔ PHP 8.3 原生支持(无需 PPA)
✔ MariaDB 10.11+
✔ Python 3.12
✔ SNMP / RRDtool 版本新
✔ 官方 LibreNMS 已完整支持

👉 结论:24.04 是目前最推荐系统

🧱 二、基础系统准备

apt update && apt upgrade -y
apt install -y software-properties-common curl git acl unzip wget lsb-release

设置时区(建议统一):

timedatectl set-timezone Asia/Shanghai

📦 三、安装 LibreNMS 依赖(关键步骤)
✅ 1️⃣ 安装核心组件

apt install -y \
nginx-full mariadb-server mariadb-client \
fping snmp snmpd rrdtool whois mtr-tiny nmap \
python3 python3-pip python3-redis python3-mysqldb \
graphviz imagemagick

✅ 2️⃣ 安装 PHP 8.3(24.04 默认自带)

apt install -y \
php-cli php-fpm php-mysql php-curl php-gd \
php-mbstring php-snmp php-xml php-zip php-gmp \
php-bcmath php-json

👉 注意:24.04 不需要 PPA!

👤 四、创建 LibreNMS 用户

useradd librenms -d /opt/librenms -M -r -s /bin/bash

📥 五、下载 LibreNMS 源码

cd /opt
git clone https://github.com/librenms/librenms.git
chown -R librenms:librenms /opt/librenms
chmod 771 /opt/librenms

如果因为网络问题超时无法下载:

方案1:强制 Git 用 HTTP/1.1(最有效)
git config --global http.version HTTP/1.1

然后重新 clone:
git clone https://github.com/librenms/librenms.git

🔐 六、目录权限(非常关键)

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

📦 七、安装 Composer 依赖

切换用户:

su - librenms
cd /opt/librenms

执行:

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

🗄️ 八、配置 MariaDB

mysql -u root

执行:

CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

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

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

FLUSH PRIVILEGES;

EXIT;

⚙️ 九、配置 PHP-FPM

创建 pool:

cp /etc/php/8.3/fpm/pool.d/www.conf /etc/php/8.3/fpm/pool.d/librenms.conf

nano /etc/php/8.3/fpm/pool.d/librenms.conf
修改:
[librenms]
user = librenms
group = librenms
listen = /run/php/php8.3-fpm.sock

重启:
systemctl restart php8.3-fpm

🌐 十、配置 Nginx

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

写入:

server {
    listen 80;
    server_name your-ip-or-domain;

    root /opt/librenms/html;
    index index.php;

    charset utf-8;

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

    location ~ \.php {
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

重启:

rm -f /etc/nginx/sites-enabled/default
systemctl restart nginx

🧪 十一、SNMP 配置

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

修改 community(例如 public):

public

启动:

systemctl enable snmpd
systemctl restart snmpd

⏱️ 十二、Cron + Scheduler(核心)

cp /opt/librenms/dist/librenms.cron /etc/cron.d/librenms

启用 scheduler:

cp /opt/librenms/dist/librenms-scheduler.service /etc/systemd/system/
cp /opt/librenms/dist/librenms-scheduler.timer /etc/systemd/system/

systemctl enable librenms-scheduler.timer
systemctl start librenms-scheduler.timer

🌍 十三、Web 初始化

切换到 librenms 用户
su - librenms

然后进入目录:
cd /opt/librenms

你必须配置 .env 文件
编辑环境配置

cd /opt/librenms
nano .env

数据库部分
DB_HOST=localhost
DB_DATABASE=librenms
DB_USERNAME=librenms
DB_PASSWORD=StrongPassword123
👉 密码必须和你 MySQL 创建时一致

APP_URL=http://192.168.1.10

重新执行初始化(关键)

php artisan key:generate

执行数据库迁移

php artisan migrate --force

修复权限问题

修复整个 LibreNMS 目录权限(关键)
chown -R librenms:www-data /opt/librenms

重点修复 logs 权限
chmod -R 775 /opt/librenms/logs
chmod -R 775 /opt/librenms/storage
chmod -R 775 /opt/librenms/bootstrap/cache

确保 log 文件存在
touch /opt/librenms/logs/librenms.log
chown librenms:www-data /opt/librenms/logs/librenms.log
chmod 664 /opt/librenms/logs/librenms.log

修复文件权限(关键)
chown librenms:www-data /opt/librenms/.env
chmod 664 /opt/librenms/.env

确保 Web 进程有写权限
chmod -R 775 /opt/librenms/storage
chmod -R 775 /opt/librenms/bootstrap/cache

重启服务
systemctl restart php8.3-fpm
systemctl restart nginx

浏览器访问:

http://服务器IP
或:
http://服务器IP/install

image

其他问题处理

通知告警
Error: Python requirements not met Python 3 dependencies are missing. You need to install them via pip3 install -r requirements.txt or system packages to continue to receive updates. If you do not install Python 3 and required packages, LibreNMS will continue to function but stop receiving bug fixes and updates.

先确认缺少哪些依赖

切换到 librenms 用户:
sudo -u librenms -s
cd /opt/librenms

执行:
./validate.php
或者:
./lnms validate

会显示具体缺少哪些 Python 模块。
  1. 时区不一致(FAIL)

当前:

系统时区:CST
PHP时区:UTC

查看系统时区:
timedatectl

如果你在国内,建议:
sudo timedatectl set-timezone Asia/Shanghai

然后修改 PHP:回到root账户

编辑 CLI:
sudo vim /etc/php/8.3/cli/php.ini

找到:(/timezone回车)

;date.timezone =

改成:(按i进行编辑)
date.timezone = Asia/Shanghai

再修改 FPM:
sudo vim /etc/php/8.3/fpm/php.ini

同样修改:
date.timezone = Asia/Shanghai

重启:
sudo systemctl restart php8.3-fpm

验证:
php -i | grep timezone

应该看到:
Default timezone => Asia/Shanghai

有其他问题继续检测执行:
./validate.php后针对修复

安装python依赖python venv 组件(对应你当前 Python 3.12)

先修系统依赖(必须 root)

请直接执行:
apt update
apt install -y python3.12-venv

安装后检查:
python3 -m ensurepip --version
如果不报错就 OK

重新创建 venv(关键步骤)
⚠️ 在 LibreNMS 目录执行:

cd /opt/librenms
python3 -m venv .venv

检测执行:
ls -l /opt/librenms/.venv/bin/python

如果看到类似:
/opt/librenms/.venv/bin/python
说明 ✔ venv 已成功创建

正确进入 venv(关键)

你现在必须这样做:
cd /opt/librenms
source .venv/bin/activate

再检查 python(这一步很关键)
which python

应该输出:
/opt/librenms/.venv/bin/python

继续检测

切换到 librenms 用户:
sudo -u librenms -s
cd /opt/librenms

执行:
./validate.php
仍然存在 3 个问题
❌ 1. Python 依赖没装进 venv(核心问题)
command_runner>=1.3.0 missing

👉 说明:你没有在 venv 里执行 pip install

❌ 2. cron wrapper 没装
Python wrapper cron entry is not present
❌ 3. 文件权限混乱(严重但可修)
owned by different user than librenms

✅ 一、先修 Python(最重要)

⚠️ 必须用 librenms 用户 + venv

1️⃣ 切换用户
sudo -u librenms -s

2️⃣ 进入目录
cd /opt/librenms

3️⃣ 激活 venv(关键)
source .venv/bin/activate

确认:
which python

必须是:
/opt/librenms/.venv/bin/python

4️⃣ 安装依赖(关键修复点)
pip install --upgrade pip setuptools wheel
pip install -r requirements.txt

安装日志可能会有报警,可以做如下处理

用 sudo 强制清理旧残留
回到root账户
sudo rm -rf /opt/librenms/.venv

切回 librenms:
sudo -iu librenms

创建:
python3 -m venv /opt/librenms/.venv

激活
source /opt/librenms/.venv/bin/activate

升级 pip
pip install --upgrade pip setuptools wheel

安装依赖
pip install -r requirements.txt

先退出 venv
deactivate

回到root
修正权限(最后一步)
sudo chown -R librenms:librenms /opt/librenms/.venv

修正整个目录权限(关键)
sudo chown -R librenms:librenms /opt/librenms
✔ 2. 给 Web 用户补写权限(关键)

Ubuntu 默认 web 用户是:

www-data

执行:

sudo usermod -aG librenms www-data
✔ 3. 强制修 logs 权限
sudo chmod -R 775 /opt/librenms/logs
sudo chmod -R 775 /opt/librenms/rrd
sudo chmod -R 775 /opt/librenms/storage
✔ 4. 确保目录存在
sudo mkdir -p /opt/librenms/logs
sudo touch /opt/librenms/logs/librenms.log
sudo chown librenms:www-data /opt/librenms/logs/librenms.log
✔ 5. 重启 Web 服务
sudo systemctl restart nginx php8.3-fpm

如果你想“一步到位最干净方案”(推荐)

直接执行这个(生产常用):

sudo chown -R librenms:www-data /opt/librenms
sudo chmod -R 775 /opt/librenms
sudo usermod -aG librenms www-data


配置grafana连接librenms

第一部分:安装 InfluxDB 1.8
bash

1. 下载 deb 包(绕过 apt 源签名问题)

cd /opt
wget https://dl.influxdata.com/influxdb/releases/influxdb_1.8.10_amd64.deb

2. 安装

sudo dpkg -i influxdb_1.8.10_amd64.deb

3. 修复依赖(如有)

sudo apt-get install -f -y

4. 启动并设置开机自启

sudo systemctl daemon-reload
sudo systemctl start influxdb
sudo systemctl enable influxdb

5. 检查状态

sudo systemctl status influxdb --no-pager
第二部分:创建数据库和用户
bash

1. 进入 InfluxDB 命令行

influx

2. 创建数据库

CREATE DATABASE librenms;

3. 创建管理员用户(用于 LibreNMS 写入)

CREATE USER "librenms" WITH PASSWORD 'LibreNMS_Pass_2024' WITH ALL PRIVILEGES;

4. 创建只读用户(用于 Grafana 查询)

CREATE USER "grafana" WITH PASSWORD 'grafanA';

5. 授予只读用户权限

GRANT READ ON "librenms" TO "grafana";

6. 验证

SHOW DATABASES;
SHOW USERS;
SHOW GRANTS FOR "grafana";

7. 退出

exit
第三部分:开启 InfluxDB 认证
bash

1. 编辑配置文件

sudo vim /etc/influxdb/influxdb.conf

找到 [http] 部分,修改为:

auth-enabled = true

或者用 sed 命令直接修改:

bash

2. 用 sed 开启认证

sudo sed -i 's/# auth-enabled = false/auth-enabled = true/' /etc/influxdb/influxdb.conf

3. 重启 InfluxDB

sudo systemctl restart influxdb

4. 测试认证是否生效

influx -username librenms -password 'LibreNMS_Pass_2024' -execute "SHOW DATABASES"
第四部分:配置 LibreNMS 连接 InfluxDB
bash

切换到 LibreNMS 目录

cd /opt/librenms

启用 InfluxDB

sudo -u librenms lnms config:set influxdb.enable true

配置连接参数

sudo -u librenms lnms config:set influxdb.transport http
sudo -u librenms lnms config:set influxdb.host '127.0.0.1' # 如果 InfluxDB 在本机
sudo -u librenms lnms config:set influxdb.port 8086
sudo -u librenms lnms config:set influxdb.db 'librenms'
sudo -u librenms lnms config:set influxdb.username 'librenms'
sudo -u librenms lnms config:set influxdb.password 'LibreNMS_Pass_2024'

重启轮询器

sudo systemctl restart librenms.service

手动触发一次轮询(可选)

sudo -u librenms lnms device:poll all
第五部分:配置 InfluxDB 自动清理(保留策略)
bash

1. 用管理员用户登录

influx -username librenms -password 'LibreNMS_Pass_2024'

2. 切换到 librenms 数据库

USE librenms;

3. 查看当前策略

SHOW RETENTION POLICIES;

4. 创建新的保留策略(90天,设为默认)

CREATE RETENTION POLICY "90d" ON "librenms" DURATION 90d REPLICATION 1 DEFAULT;

5. 验证新策略

SHOW RETENTION POLICIES;

6. 确认新策略的 default 列为 true

7. (可选)删除旧的 autogen 策略(会永久删除历史数据!)

DROP RETENTION POLICY "autogen" ON "librenms";

8. 退出

exit
第六部分:验证数据是否写入
bash

查看所有 measurement

influx -username grafana -password grafanA -database librenms -execute "SHOW MEASUREMENTS"

查看最新数据

influx -username grafana -password grafanA -database librenms -execute "SELECT * FROM processors ORDER BY time DESC LIMIT 5"

查看数据保留策略

influx -username librenms -password 'LibreNMS_Pass_2024' -database librenms -execute "SHOW RETENTION POLICIES"

第七部分:防火墙配置(允许 Grafana 访问)
bash

在 InfluxDB 服务器上执行

如果使用 ufw

sudo ufw allow from <Grafana_主机_IP> to any port 8086 proto tcp

查看防火墙状态

sudo ufw status

如果需要测试网络连通性(在 Grafana 主机上执行)

telnet <InfluxDB_IP> 8086
curl -u grafana:grafanA 'http://<InfluxDB_IP>:8086/query?db=librenms&q=SHOW+MEASUREMENTS'
第八部分:Grafana 数据源配置
在 Grafana Web 界面中:

配置项 填写值
URL http://<InfluxDB_IP>:8086
Database librenms
User grafana
Password grafanA
HTTP Method GET

配置完成后,可以在grafana中配置仪表盘
也可以通过AI桌面助手(百度domate等)连接influxdb自动生成网络报告

posted @ 2026-06-05 11:47  红妹妹  阅读(46)  评论(0)    收藏  举报