在本页
- 先决条件
- 安装和配置 MariaDB 数据库
- 安装 Nginx、PHP 及所需依赖
- 安装和配置 LibreNMS
- 为 LibreNMS 配置 Nginx
- 访问 LibreNMS Web 安装向导
- 结论
LibreNMS 是一款用 PHP 编写的免费、开源、基于 Web 且具有自动发现功能的网络监控工具。它使用 MySQL 或 MariaDB 作为数据库后端,并使用 SNMP 来发现远程客户端。它支持多种网络设备,包括 Linux、Cisco、Juniper、FreeBSD、HP、Windows 等。它支持多种身份验证方法,包括 Radius、Active Directory、LDAP、MySQL 等。它简单、易用,任何人都容易理解和使用。
在本教程中,我将解释如何在 Debian 11 上使用 Nginx 安装 LibreNMS。
先决条件
- 一台运行 Debian 11 的服务器。
- 一个指向您服务器 IP 的有效域名。
- 您的服务器上配置了 root 密码。
安装和配置 MariaDB 数据库
首先,您需要在服务器上安装 MariaDB 数据库服务器。您可以使用以下命令进行安装:
apt-get install mariadb-server -y
安装完成后,运行以下脚本以保护 MariaDB 安装并设置 root 密码:
mysql_secure_installation
按下图所示回答所有问题:
Enter current password for root (enter for none): Switch to unix_socket authentication [Y/n] n Change the root password? [Y/n] Y New password: Re-enter new password: Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Y/n] Reload privilege tables now? [Y/n] Y
MariaDB 安全加固完成后,编辑 MariaDB 配置文件并调整一些设置:
nano /etc/mysql/mariadb.conf.d/50-server.cnf
在 [mysqld] 部分内添加以下行:
innodb_file_per_table=1 lower_case_table_names=0
保存并关闭文件,然后重启 MariaDB 以应用更改:
systemctl restart mariadb
接下来,使用以下命令登录 MariaDB:
mysql -u root -p
登录后,为 LibreNMS 创建数据库和用户:
MariaDB [(none)]> CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
MariaDB [(none)]> CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'securepassword';
接下来,使用下面的命令授予 LibreNMS 数据库所有权限:
MariaDB [(none)]> GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';
最后,使用以下命令刷新权限并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
完成后,您可以继续下一步。
安装 Nginx、PHP 及所需依赖
接下来,您需要安装 Nginx Web 服务器、PHP 以及 LibreNMS 所需的其他依赖项。您可以通过运行以下命令来安装所有这些:
apt-get install nginx-full nmap php-cli php-curl php-fpm php-gd php-json php-mbstring php-mysql php-snmp php-xml php-zip python3-dotenv python3-pip python3-pymysql python3-redis python3-setuptools python3-systemd rrdtool snmp snmpd whois acl curl composer fping git graphviz imagemagick mtr-tiny
所有软件包安装完成后,编辑 php.ini 文件并设置您的时区:
nano /etc/php/7.4/fpm/php.ini
更改以下行:
date.timezone = UTC
完成后保存并关闭文件。
安装和配置 LibreNMS
首先,使用以下命令为 LibreNMS 创建一个专用用户:
useradd librenms -d /opt/librenms -M -r -s /bin/bash
接下来,使用以下命令将 LibreNMS 用户添加到 www-data 组:
usermod -a -G librenms www-data
接下来,从 GitHub 仓库下载最新版本的 LibreNMS 到 /opt 目录:
git clone https://github.com/librenms/librenms.git /opt/librenms
接下来,设置 LibreNMS 目录的所有权和权限:
chown -R librenms:librenms /opt/librenms
chmod 775 /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/
接下来,使用以下命令切换用户为 LibreNMS:
su - librenms
接下来,使用以下命令安装 PHP Composer:
cd /opt/librenms
./scripts/composer_wrapper.php install --no-dev
Composer 和其他 PHP 依赖项安装完成后,您应该看到以下输出:
> @php artisan optimize Configuration cache cleared! Configuration cached successfully! Route cache cleared! Routes cached successfully! Files cached successfully! > @php artisan config:clear Configuration cache cleared! > scripts/dynamic_check_requirements.py || pip3 install --user -r requirements.txt || : Requirement already satisfied: PyMySQL!=1.0.0 in /usr/lib/python3/dist-packages (from -r requirements.txt (line 1)) (0.9.3) Requirement already satisfied: python-dotenv in /usr/lib/python3/dist-packages (from -r requirements.txt (line 2)) (0.15.0) Requirement already satisfied: redis>=3.0 in /usr/lib/python3/dist-packages (from -r requirements.txt (line 3)) (3.5.3) Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from -r requirements.txt (line 4)) (52.0.0) Collecting psutil>=5.6.0 Downloading psutil-5.8.0-cp39-cp39-manylinux2010_x86_64.whl (293 kB) Collecting command_runner>=1.3.0 Downloading command_runner-1.3.0-py3-none-any.whl (17 kB) Installing collected packages: psutil, command-runner Successfully installed command-runner-1.3.0 psutil-5.8.0
接下来,使用下面的命令退出 LibreNMS 用户:
exit
接下来,您需要为 PHP-FPM 创建一个单独的配置文件。您可以使用以下命令创建它:
cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/librenms.conf
接下来,使用您喜欢的编辑器编辑 librenms.conf 配置文件:
nano /etc/php/7.4/fpm/pool.d/librenms.conf
将 [www-data] 更改为 [librenms],并更新监听套接字:
user = librenms group = librenms listen = /run/php-fpm-librenms.sock
保存并关闭文件,然后重启 PHP-FPM 服务以应用配置更改:
systemctl restart php7.4-fpm
完成后,您可以继续下一步。
为 LibreNMS 配置 Nginx
接下来,您需要为 LibreNMS 创建一个 Nginx 虚拟主机配置文件。您可以使用以下命令创建它:
nano /etc/nginx/conf.d/librenms.conf
添加以下行:
server {
listen 80;
server_name libre.yourdomain.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-fpm-librenms.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi.conf;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
保存并关闭文件,然后使用以下命令检查 Nginx 是否存在语法错误:
nginx -t
如果一切正常,您将看到以下输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
最后,重启 Nginx 以应用更改。
systemctl restart nginx php7.4-fpm
您还可以使用以下命令检查 Nginx 状态:
systemctl status nginx
您应该看到以下输出:
? nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2021-12-03 14:39:18 UTC; 18s ago
Docs: man:nginx(8)
Process: 39888 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 39892 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 39893 (nginx)
Tasks: 2 (limit: 2341)
Memory: 2.7M
CPU: 42ms
CGroup: /system.slice/nginx.service
??39893 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
??39894 nginx: worker process
Dec 03 14:39:18 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 03 14:39:18 debian11 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Dec 03 14:39:18 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.
接下来,复制 cron 作业配置文件,以便对新添加的设备启用自动发现和轮询。
cp /opt/librenms/librenms.nonroot.cron /etc/cron.d/librenms
接下来,复制 logrotate 配置文件以轮转旧日志。
cp /opt/librenms/misc/librenms.logrotate /etc/logrotate.d/librenms
完成后,您可以继续下一步。
访问 LibreNMS Web 安装向导
此时,LibreNMS 已与 Nginx 一起安装并配置完成。您现在可以使用 URL http://libre.yourdomain.com 访问 LibreNMS 的 Web 安装程序。您应该看到安装前检查页面:

确保所有检查均成功,然后单击数据库图标。您应该看到数据库配置页面:

提供您的数据库信息,然后单击 Check Credentials。您应该看到以下页面:

现在,单击钥匙图标以设置管理员用户:

提供您的管理员用户名和密码,然后单击 Add User 按钮。用户创建完成后,您应该看到以下页面:

单击 Yes 图标按钮。您应该看到以下页面:

现在,在 Web 浏览器中打开另一个标签页,输入 URL https://libre.yourdomain.com/validate 以验证安装。您应该看到以下页面:

提供您的管理员用户名和密码,然后单击 Login 按钮。您应该看到以下页面:

结论
恭喜!您已在 Debian 11 上使用 Nginx 成功安装 LibreNMS。您现在可以开始向 LibreNMS 添加远程设备,并从中央位置开始监控它们。如有任何问题,欢迎随时向我提问。