Install and Configure Nagios on Ubuntu 24.04

By Anurag Singh

Updated on Jul 18, 2024

Install and Configure Nagios on Ubuntu 24.04

In this tutorial, we'll explain how to install and configure Nagios on Ubuntu 24.04 with Apache as a web server and securing with SSL certificate Let's Encrypt Certbot.

Nagios is a powerful monitoring system that enables organizations to identify and resolve IT infrastructure problems before they affect critical business processes. This tutorial will guide you through the steps of installing and configuring Nagios on an Ubuntu server.

Prerequisites

  • An Ubuntu 24.04 dedicated server or KVM VPS (Ubuntu 20.04 or later is recommended)
  • A non-root user with sudo privileges
  • Basic knowledge of terminal commands

Install and Configure Nagios on Ubuntu

Step 1: Update System Packages

Before installing any software, it's important to update the system packages to the latest versions.

sudo apt update
sudo apt upgrade -y

Step 2: Install Required Dependencies

Nagios requires several packages to be installed. Install these dependencies using the following commands:

sudo apt install -y autoconf gcc make libgd-dev libmcrypt-dev libssl-dev apache2 php libapache2-mod-php7.4 build-essential unzip

Step 3: Create Nagios User and Group

Create a user and group for Nagios to run under:

sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd www-data

Step 4: Download and Install Nagios Core

Download the latest stable release of Nagios Core from the official website. At the time of writing, the latest version is 4.5.3.

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

Compile and install Nagios:

sudo ./configure --with-command-group=nagcmd
sudo make all
sudo make install
sudo make install-init
sudo make install-config
sudo make install-commandmode
sudo make install-webconf

Install Nagios Plugins

Nagios uses plugins to monitor services. Download and install the latest Nagios plugins:

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

Compile and install the plugins:

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

Step 5: Configure Nagios Web Interface

Set up the Nagios web interface by creating an admin user for accessing the web UI:

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

Start the Nagios service and enable it to start on boot:

sudo systemctl start nagios
sudo systemctl enable nagios

Step 6: Configure Firewall

We need to add HTTP and HTTPS ports in the firewall. 

ufw allow 80/tcp
ufw allow 443/tcp
ufw reload

Step 7: Configure Apache Web Server

Configure Apache for Your Domain

Modify exisiting a Virtual Host File for Your Domain:

sudo nano /etc/apache2/sites-available/nagios.conf

Note: Replace yourdomain.com with your domain name and admin@yourdomain.com with your email id. Add the following configuration:

<VirtualHost *:80>
    ServerAdmin admin@yourdomain.com
    ServerName yourdomain.com
    DocumentRoot /usr/local/nagios/share

    ErrorLog ${APACHE_LOG_DIR}/nagios_error.log
    CustomLog ${APACHE_LOG_DIR}/nagios_access.log combined

    <-- All existing content goes here -->
    
</VirtualHost>

Enable the site and the required Apache modules:

a2enmod cgi
sudo a2ensite nagios.conf
sudo a2enmod cgi rewrite
sudo systemctl restart apache2

Step 8: Install and Configure SSL with Let's Encrypt

Install Certbot:

sudo apt install certbot python3-certbot-apache -y

Obtain and Install the SSL Certificate:

sudo certbot --apache -d yourdomain.com

Follow the prompts to complete the SSL installation. Certbot will automatically configure Apache to use the new SSL certificate.

Step 9: Access Nagios Web Interface

Open your web browser and navigate to the Nagios web interface:

http://<domain-name>/

Log in with the username nagiosadmin and the password you set up earlier.

Step 10: Verify Nagios Configuration

To ensure Nagios is working correctly, you can check its configuration:

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Output: 

Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2024-06-11
License: GPL

Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...

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 global event 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

Add Monitoring Services

You can add hosts and services to monitor by editing the configuration files in /usr/local/nagios/etc/objects/. Here's an example of how to add a new host:

Open the hosts.cfg file:

sudo nano /usr/local/nagios/etc/objects/hosts.cfg

Add a new host definition:

define host {
    use                     linux-server
    host_name               example-host
    alias                   Example Host
    address                 192.168.1.100
    max_check_attempts      5
    check_period            24x7
    notification_interval   30
    notification_period     24x7
}

Save and close the file.

Define a New Service

Services are the individual aspects of a host that you want to monitor (e.g., HTTP, PING).

Open the services.cfg file:

sudo nano /usr/local/nagios/etc/objects/services.cfg

Add a service definition:

define service {
    use                     generic-service
    host_name               example-host
    service_description     PING
    check_command           check_ping!100.0,20%!500.0,60%
    max_check_attempts      5
    normal_check_interval   5
    retry_check_interval    1
    check_period            24x7
    notification_interval   30
    notification_period     24x7
}

Save and close the file.

Restart Nagios to apply the changes:

sudo systemctl restart nagios

Conclusion

You have successfully seen how to install and configure Nagios on Ubuntu 24.04 server. You can now start adding hosts and services to monitor your network and system infrastructure effectively. For more detailed configurations and customizations, refer to the official Nagios documentation.