How To Install Nagios on an Ubuntu 18.04 VPS or Dedicated Server


Introduction

Nagios is an open-source monitoring tool that helps to monitor the services and applications that run on Linux and Windows. It can also be used to monitor network routers including other network devices. It alerts the system administrator every time something goes wrong and sends an alert back when the issue is resolved.

Nagios can monitor private services such as HTTP, SSH, FTP, SMTP and attributes including CPU load, disk usage, running processes, memory usage, logged in users, and more.

In this tutorial, we are going to show you how to install Nagios Core on Ubuntu 18.04 LTS.

Prerequisites

Before you start, you need the following:

  • A server running on Ubuntu 18.04
  • A non-root user with sudo privileges

Step 1 - Installing Nagios

Before you install Nagios on Ubuntu, you will need to install Nagios dependent packages. Start by installing the packages you need.Now, update the system’s repository cache then start installing the dependencies for Nagios.

$ sudo apt-get update 
$ sudo apt-getinstallbuild-essential apache2 php openssl perl make php-gd libgd-dev libapache2-mod-php libperl-dev libssl-dev daemon wget apache2-utils unzip

Next, create Nagios users and nagcmd group to allow the external commands to run through the web interface. For security reasons, it is important to create a user and group specifically to run Nagios.

$ sudo useradd nagios
$ sudo groupadd nagcmd
$ sudo usermod -a -G nagcmd nagios

Next, you need to download and build the Nagios source. To do so, go to Nagios Core download page and download the latest version of Nagios. The version is 4.4.2 at the time of writing this article.

$ wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.2.tar.gz
tar -zxvf /tmp/nagios-4.4.2.tar.gz
$cd /tmp/nagios-4.4.2/

Step 2 - Compiling And Installing Nagios

Now prepare the Nagios source. Then configure it to ensure it uses the user and group that you have already created.

Then start compiling Nagios from the source code.

$ ./configure --with-nagios-group=nagios --with-command-group=nagcmd --with-httpd_conf=/etc/apache2/sites-enabled/
$ sudo make all
$ sudo make install
$ sudo make install-init
$ sudo make install-config
$ sudo make install-commandmode
$ sudo make install-webconf

Now, download the latest Nagios-plugins source then install it using the commands below:

$ wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
$ tar xzf nagios-plugins-2.1.1.tar.gz
$cd nagios-plugins-2.1.1
$ ./configure --with-nagios-user=nagios --with-nagios-group=nagios
$ sudo make
$ sudo make install

Step 3 - Configuring Nagios

At this point, the installer has placed the configuration files in the directory named /usr/local/nagios/etc. These configuration files need to work properly with Nagio. But you need to make some few changes before proceeding to the next step.

Locate the file /usr/local/nagios/etc/objects/contacts.cfg and edit it then change the associated email address with the nagioaadmin contact definition and include the address you want to be receiving alerts.

$ sudo vi /usr/local/nagios/etc/objects/contacts.cfg

Now change the section with the email address to receive the notification:

define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full name of user

        email                           nagios@hostadvice.com      ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        }

Step 4 - Configuring Nagios Web Interface

Create an apache2 configuration file for Nagios using this command:

$ sudo nano /etc/apache2/sites-enabled/nagios.conf

Now edit, the lines below to allow you to access the administrative console for Nagios from a certain IP series:

[...]
## Comment the following lines ##
#   Order allow,deny
#   Allow from all

## Uncomment and Change lines as shown below ##
Order deny,allow
Deny from all
Allow from 127.0.0.1 IP address
[...]

The next step is to create a user account nagioadmin to allow the user to log into the Nagios dashboard. Don’t forget the password you to this account since you will need it later.

$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

Now enable the command below to enable Apache’s rewrite and CGI modules:

$ sudo a2enmod rewrite
$ sudo a2enmod cgi

Now, restart Apache to apply these changes:

$ sudo systemctl restart apache2

Step 5 - Installing Nagios Plugins

Next, download the plugins for Nagios to help in monitoring these services. Save it into the /tmp directory:

$cd /tmp
$ wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
$ tar -zxvf /tmp/nagios-plugins-2.2.1.tar.gz
$cd /tmp/nagios-plugins-2.2.1/

Then start compiling and installing the Nagios plugins:

$ ./configure --with-nagios-user=nagios --with-nagios-group=nagios
$ sudo make
$ sudo make install

Step 6  - Starting Nagios

Next, we have to ensure Nagios starts at boot time. To do so, we need to verify the sample configuration files for Nagios.

$ sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

You should get the output below:

[...]
Checking objects...
    Checked 8 services.
    Checked 1 hosts.
    Checked 1 host groups.
    Checked 0 service groups.
    Checked 1 contacts.
    Checked 1 contact groups.
    Checked 24 commands.
    Checked 5 time periods.
    Checked 0 host escalations.
    Checked 0 service escalations.
Checking for circular paths...
    Checked 1 hosts
    Checked 0 service dependencies
    Checked 0 host dependencies
    Checked 5 timeperiods
Checking globalevent handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check
[...]

Now, set Nagios to start automatically when the system starts.

$ sudo systemctl enable nagios

Next, restart Nagios service:

$ sudo systemctl start nagios

Step 7 - Accessing Nagios

The next step us to access the Nagios dashboard using this URL: http://ip-address/nagios/

You will be asked to provide the username and password you created earlier.

How To Install Nagios on Ubuntu 18

After login successfully, you will see the Nagios’s homepage.

How To Install Nagios on Ubuntu 18

Conclusion

Congratulation! Now you have installed Nagios on Ubuntu 18.04 server successfully. Ubuntu will now be able to monitor itself, and you will be able to see the data in real time via the web interface.

Check out these top 3 VPS services:

Was this article helpful?