How to Setup LibreNMS Monitoring Tool with Nginx on Ubuntu 16.04 LTS

LibreNMS is an open source monitoring tool based on PHP/MYSQL/SNMP. It's a featured network monitoring system that provides support for wide range of network hardware and operating systems including, FreeBSD, Cisco, Linux, HP etc.

In this tutorial, I will show you how to install and configure the open source monitoring tool 'LibreNMS' using Nginx as the webserver, MariaDB as the database, and Ubuntu 16.04 server as our main server. We will guide you step-by-step on how to install and configure 'LibreNMS' on Ubuntu 16.04 Xenial Xerus server.

What we will do

  1. Install Packages
  2. Install Nginx Webserver
  3. Install and Configure PHP-FPM
  4. Install and Configure MariaDB
  5. Download and Configure LibreNMS
  6. LibreNMS Web Installer
  7. Final configuration

Prerequisites

  • Ubuntu 16.04 Server
  • Root Privileges

Step 1 - Install Packages

The first step we must do for installing LibreNMS Monitoring Tools is to install some packages needed on the server. Connect to your server and update the repository.

ssh [email protected]
sudo apt update

Install all the required packages for LibreNMS from the Ubuntu repository using the following command.

apt install fping imagemagick whois mtr-tiny nmap python-mysqldb snmpd  rrdtool git snmp graphviz

After the installation is complete, goto the next step.

Step 2 - Install Nginx Webserver

In this tutorial, we will be running LibreNMS under the Nginx web server. Nginx is powerful web server that's available in the Ubuntu repositories.

Install nginx using apt command from the repository in the following way.

apt install nginx

When it's done, start the service and enable it to run automatically every time at system boot.

systemctl start nginx
systemctl enable nginx

Nginx web server is running under the default port 80. We can check the port using the netstat command, and check Nginx using curl command to get the HTTP status code, as shown below.

netstat -plntu | grep 80
curl -I localhost

Check ports with netstat

Nginx installation has been completed.

Step 3 - Install and Configure PHP-FPM

LibreNMS is PHP based web application monitoring tool. It offers support for new PHP version 7.0, and we will be using it for this guide.

Install PHP, PHP-FPM, and all extensions/modules needed for LibreNMS installation using the apt command below.

apt install php7.0-cli php7.0-mysql php7.0-gd php7.0-snmp php-pear php7.0-curl php7.0-fpm php7.0-mcrypt php7.0-json php-net-ipv4 php-net-ipv6

Next, we need to add some configuration to php.ini files. We need to define the default timezone in php.ini file and make sure the timezone matches with the current timezone used by the system.

Check the currently timezone used by the system with the following command.

timedatectl

You should get a result similar to the following.

Check timezone settings

You can see the server is using the 'Europe/Paris' timezone.

Now go to the PHP configuration directory and edit php.ini files for the cli and fpm config.

cd /etc/php/7.0/
vim fpm/php.ini
vim cli/php.ini

Uncomment the 'date.time' line and change the value with our system timezone 'Europe/Paris'.

date.time = Europe/Paris

Uncomment cgi configuration below, change the value to 0.

cgi.fix_pathinfo = 0

Save and exit.

All configuration is complete. Now start the service and enable it to launch every time at system boot using the following systemctl commands.

systemctl start php7.0-fpm
systemctl enable php7.0-fpm

PHP-FPM is now running on ubuntu server - it's running under the sock file. Check it with the netstat command.

netstat -pl | grep php

Check PHP-FPM process

Step 4 - Install and Configure MariaDB

In this step, we will install the mariadb-server for the LibreNMS database. We will install, configure and create a new database and a new user for the LibreNMS installation.

Install mariadb-server from ubuntu repository using the apt command below.

apt install mariadb-server mariadb-client mariadb

When it's done, start the service, and enable it to run automatically at system boot, something you can do using the following systemctl commands.

systemctl start mysql
systemctl enable mysql

The database server mariadb is now running. Next we need to configure the root password for mariadb. We can use the 'mysql_secure_installation' command below to configure the root password.

mysql_secure_installation

You will be asked about the new root password - type your password and press 'Enter' to continue.

Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

The root password for mariadb has been configured.

Next, we must create a new database and user for LibreNMS. We will create a new database named 'librenms', a new user named 'librenms' with password 'hakase-labs123'.

Log in to the mariadb shell using the following command.

mysql -u root -p
Type the ROOT Password:

Run mariadb queries below to create a new database and user, and grant all privileges on the database to the new user.

CREATE DATABASE librenms CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'hakase-labs123';
GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';
FLUSH PRIVILEGES;

A new database and user for LibreNMS has been created.

Add a database

For the LibreNMS installation, we need to add some configuration to the configuration file. Go to the '/etc/mysql/' directory and edit the mariadb configuration file.

cd /etc/mysql/
vim mariadb.conf.d/50-server.cnf

Paste configuration below under the '[mysqld]' section.

innodb_file_per_table=1
sql-mode=""
lower_case_table_names=0

Save and exit.

Change MariaDB settings

Now apply the new configuration by restarting the service.

systemctl restart mysql

The mariadb database configuration has been completed.

Step 5 - Download and Configure LibreNMS

In this step, we will configure the system for LibreNMS installation.

- Add New User and Download LibreNMS

Create a new system user named 'librenms', define '/opt/librenms' as the default home directory for the user, and assign the new 'librenms' user to the www-data group.

Run the following command to do it all.

useradd librenms -d /opt/librenms -M -r
usermod -a -G librenms www-data

Now go to the '/opt/' directory and download the LibreNMS source code using the git command.

cd /opt/
git clone https://github.com/librenms/librenms.git librenms

Add librenms user

Next, create a new directory for libreNMS log files and rrd files.

mkdir -p /opt/librenms/{logs,rrd}

Change ownership permissions for the 'rrd' directory to '775', and change the owner of the 'librenms' directory to the 'librenms' user and group.

chmod -R 775 /opt/librenms/rrd/
chown -R librenms:librenms /opt/librenms/

Create a directory

A new 'librenms' has been created, and the LibreNMS source code has been downloaded.

- Configure LibreNMS Virtualhost

Go to the 'nginx' configuration directory and create a new virtual host file 'librenms' with vim

cd /etc/nginx/
vim sites-available/librenms

Paste the following LibreNMS Virtual host configuration there.

server {

    # Add your own domain name
    listen      80;
    server_name librenms.irsyadf.me;

    # LibreNMS Webroot directory
    root        /opt/librenms/html;
    index       index.php;

    # LibreNMS logs
    access_log  /opt/librenms/logs/access_log;
    error_log   /opt/librenms/logs/error_log;
   
    # Enabling Gzip compression on Nginx
    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 /api/v0 {
        try_files $uri $uri/ /api_v0.php?$query_string;
    }

    # PHP-FPM handle all .php files requests
    location ~ \.php {
        include fastcgi.conf;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
 
    location ~ /\.ht {
        deny all;
    }
}

Save and exit. Now, activate the virtualhost.

ln -s /etc/nginx/sites-available/librenms /etc/nginx/sites-enabled/

Test the nginx configuration and make sure there is no error. Then restart the service.

nginx -t
systemctl restart nginx

Enable LibreNMS vhost in Nginx

- Configure UFW Firewall

Add new ports to the firewall. Add new ssh, http, https and the port used by snmpd 161 udp type to the ufw firewall.

Run the following ufw commands.

ufw allow ssh
ufw allow http
ufw allow https
ufw allow 161/udp

Configure UFW firewall

Start the ufw firewall with the ufw enable command as shown below.

ufw enable

Type 'y' and press 'Enter' to confirm. Start and enable it to launch every time at system boot.

If you want to see the firewall status, run the 'ufw status' command.

ufw status

You will get the firewall status - active or inactive - and the list of ports and service added to the firewall.

List firewall configuration

Step 6 - LibreNMS Web Installer

After all the above steps are complete, we need to install LibreNMS through the web browser. Open your web browser, type the LibreNMS domain name 'librenms.hakase-labs.co' in the address bar and press Enter.

- Checking PHP Modules

You will be redirected to the install.php page showing the result of PHP module support checks. Make sure all status is green as shown below.

LibreNMS web installer

Click 'Next Stage' to continue.

- Database Configuration

Fill all the database info with your own db.

  • DB User: librenms
  • DB Pass: hakase-labs123
  • DB Name: librenms

Set database details

And click 'Next Stage'.

- Importing MySQL Database

Wait for the installer script to import sample of database to our database - do not close the browser tab during this process.

Stage 2 completed

After all db is imported, click 'Goto Add User'.

- Add Admin User

Here, type your admin user, email, and password.

Add the admin user

Click 'Add User'.

- Generate LibreNMS Config

Generate LibreNMS config

Click the 'Generate Config' button.

And you will get a config file similar to the one shown below.

Save config to file

Copy the php config script, and come back to your ssh session. Goto the '/opt/librenms' directory and create the 'config.php' file manually using vim.

cd /etc/librenms/
vim config.php

Paste the configuration there, and change the ownership of the file to librenms user and group.

chown librenms:librenms config.php

Chown config file to librensm user and group

Back to your web browser and click the 'Finish Install' button.

Now you get to the last page from the librenms web installer - see below.

Installation finished

Step 7 - Final configuration

After the installation through web browser is complete, we need to do some other steps.

- Configure SNMP

Backup default configuration file and copy the sample configuration to the '/etc/snmp/' directory.

mv /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.aseli
cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf

Edit the new configuration with vim.

vim /etc/snmp/snmpd.conf

Replace the 'RANDOMSTRINGGOESHERE' line with your own community name 'hakaselabs', as shown below.

com2sec readonly  default         hakaselabs

Save and exit.

SNMP config file

Next, we need to download the distro detection script. Download it using curl, then make the script executable, then finally, restart the snmp service.

curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
chmod +x /usr/bin/distro
systemctl restart snmpd

Restart smtpd

- Crontab and Logrotate Configuration

Goto the librenms directory and copy sample configuration for Crontab and Logrotate.

cd /opt/librenms/

Copy the configuration.

cp librenms.nonroot.cron /etc/cron.d/librenms
cp misc/librenms.logrotate /etc/logrotate.d/librenms

Now restart the cron service and reload the logrotate configuration.

systemctl restart cron
logrotate -f /etc/logrotate.conf

Crontab configuration

- Validate Configuration

Wait for some time until the cron script is running on the system. Once that is done, validate with 'validate.php' script.

Goto the librenms directory and run the validate script.

cd /opt/librenms/
./validate.php

If your installation is correct, you will get the result as shown below.

Validate LibreNMS setup

LibreNMS Installation with Nginx Webserver on Ubuntu 16.04 is complete.

Reference

About Muhammad Arul

Muhammad Arul is a freelance system administrator and technical writer. He is working with Linux Environments for more than 5 years, an Open Source enthusiast and highly motivated on Linux installation and troubleshooting. Mostly working with RedHat/CentOS Linux and Ubuntu/Debian, Nginx and Apache web server, Proxmox, Zimbra Administration, and Website Optimization. Currently learning about OpenStack and Container Technology.

Share this page:

Suggested articles

3 Comment(s)

Add comment

Comments

By: John at: 2018-01-10 17:56:28

Couple of issues:

1. I had to delete /etc/nginx/sites-enabled/default before I could get install script to load in browser.

2. On the part where libreNMS generates the config.php file. The instructions say:

cd /etc/librenms/vim config.php

etc should be opt

This is still by far the easiest tutorial on setting up LibreNMS that I have found.

Thanks!

By: kris at: 2018-01-31 22:44:31

Thnx! Had that issue too.

By: Evo at: 2018-04-09 19:48:42

did not finish, tired, one thing so far

installing maria-db, I think there should not be `mariadb` amongst the packages, it does not exist and we are already installing server and client