# LibreNMS架設 - Debian 12

#### **運行環境**

- PVE
- LXC
- Debian 12

#### **安裝nginx mariadb php**

```
apt update
```

```
apt install php-mbstring php-cli php-common php-curl php-fpm php-gd php-mbstring php-mysql php-opcache php-readline php-snmp php-xml php-zip acl curl fping git graphviz imagemagick mariadb-client mariadb-server mtr-tiny nginx-full nmap rrdtool snmp snmpd whois unzip python3-pymysql python3-dotenv python3-redis python3-setuptools python3-systemd python3-pip -y
```

#### **安裝 Composer**

```
cd /usr/local/bin

curl -sS https://getcomposer.org/installer | php 

mv composer.phar composer 
```

#### **新增使用者**  


```
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/
```

#### **安裝 LibreNMS和套件**

```
su librenms
cd /opt/librenms/
./scripts/composer_wrapper.php install --no-dev

exit
pip3 install --user --break-system-packages -r /opt/librenms/requirements.txt
```

#### **修改PHP時區**

```
sed -i "s|^;date.timezone =.*$|date.timezone = Asia/Taipei|" /etc/php/8.2/fpm/php.ini
sed -i "s|^;date.timezone =.*$|date.timezone = Asia/Taipei|" /etc/php/8.2/cli/php.ini

系統設定時區
timedatectl set-timezone Asia/Taipei
```

#### **設定 PHP-FPM**

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

```
Change [www] to [librenms]:
[librenms]
Change user and group to “librenms”:
user = librenms
group = librenms
Change listen to a unique name:
listen = /run/php-fpm-librenms.sock
```

```
systemctl enable php8.2-fpm
systemctl restart php8.2-fpm
```


#### **設定MariaDB**

**編輯`/etc/mysql/mariadb.conf.d/50-server.cnf`**

```
[mysqld]底下新增

innodb_file_per_table=1
lower_case_table_names=0
```

```
systemctl enable mariadb
systemctl restart mariadb
```


#### **MariaDB新增使用者**

```
mysql -u root

CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'librenmsuser'@'localhost' IDENTIFIED BY 'librenmspswd';
GRANT ALL PRIVILEGES ON librenms.* TO 'librenmsuser'@'localhost';
FLUSH PRIVILEGES;
exit
```


#### **設定Nginx**

**建立`/etc/nginx/conf.d/librenms.conf`**

```
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_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/run/php-fpm-librenms.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
    }
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

```
rm /etc/nginx/sites-enabled/default
rm /etc/nginx/sites-available/default

systemctl enable nginx
systemctl restart nginx
```

#### **Enable lnms command completion**

```
ln -s /opt/librenms/lnms /usr/bin/lnms
cp /opt/librenms/misc/lnms-completion.bash /etc/bash_completion.d/
```

#### **Configure snmpd**

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

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
```

#### **設定排成**  


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

#### **啟用調度程式**

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

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

#### **Copy logrotate config**

```
cp /opt/librenms/misc/librenms.logrotate /etc/logrotate.d/librenms
```

瀏覽器`<strong>http://ip</strong>`，依序安裝後進行組態驗證

```
cd /opt/librenms

su librenms

./validate.php
```