How to Install and Configure Nagios on Ubuntu 18.04

Updated on

6 min read

Install and Configure Nagios on Ubuntu

Nagios is one of the most popular open-source monitoring systems. Nagios keeps an inventory of your entire IT infrastructure and ensures your networks, servers, applications, services, and processes are up and running.

In case of failure or suboptimal performance Nagios will send notification alerts via various methods.

This tutorial will guide you through the steps required to install and configure the latest version of Nagios Core on Ubuntu 18.04.

An older version of Nagios (version 3) is available in the Ubuntu software repository and can be easily installed with apt install nagios3.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges .

Update your Ubuntu system and install the following packages which are necessary to download and compile the Nagios main application and Nagios plugins:

sudo apt update && sudo apt upgradesudo apt install autoconf gcc libc6 make wget unzip apache2 php libapache2-mod-php7.2 libgd-devsudo apt install libmcrypt-dev libssl-dev bc gawk dc build-essential libnet-snmp-perl gettext

Installing Nagios on Ubuntu

The following steps describes how to install the latest version of Nagios Core from source.

1. Downloading Nagios

We are going to download Nagios source in the /usr/src directory which is the common location to place source files, change to the directory with:

cd /usr/src/

Download the latest version of Nagios from the project Github repository using the following wget command :

sudo wget https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.2.tar.gz

When the download is complete extract the tar file with:

sudo tar zxf nagios-*.tar.gz

Before continuing with the next steps, make sure you change to the Nagios source directory by typing:

cd nagioscore-nagios-*/

2. Compiling Nagios

To start the build process run the configure script which will perform a number of checks to make sure all of the dependencies on your system are present:

sudo ./configure --with-httpd-conf=/etc/apache2/sites-enabled

Upon successful completion, the following message will be printed on your screen:

*** Configuration summary for nagios 4.4.2 2018-08-16 ***:

 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagios
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
    Install ${includedir}:  /usr/local/nagios/include/nagios
                Lock file:  /run/nagios.lock
   Check result directory:  /usr/local/nagios/var/spool/checkresults
           Init directory:  /lib/systemd/system
  Apache conf.d directory:  /etc/apache2/sites-enabled
             Mail program:  /bin/mail
                  Host OS:  linux-gnu
          IOBroker Method:  epoll

 Web Interface Options:
 ------------------------
                 HTML URL:  http://localhost/nagios/
                  CGI URL:  http://localhost/nagios/cgi-bin/
 Traceroute (used by WAP):  


Review the options above for accuracy.  If they look okay,
type 'make all' to compile the main program and CGIs.

You can now start the compilation process using the make command:

sudo make all

The compilation may take some time, depending on your system. Once the build process is completed, you will be presented with the following message:

*** Compile finished ***

3. Creating Nagios User And Group

The following command will create a new system nagios user and group:

sudo make install-groups-users

Add the Apache www-data user to the nagios group:

sudo usermod -a -G nagios www-data

4. Install Nagios Binaries

Perform the following command to install Nagios binary files, CGIs, and HTML files:

sudo make install

You should see the following output:

*** Main program, CGIs and HTML files installed ***

5. Creating External Command Directory

Nagios can process commands from external applications. Create the external command directory and set the proper permissions by typing:

sudo make install-commandmode
*** External command directory configured ***

6. Install Nagios Configuration Files

Install the sample Nagios configuration files with:

sudo make install-config
*** Config files installed ***

Remember, these are *SAMPLE* config files.  You'll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.

7. Install Apache Configuration Files

The command below will install the Apache web server configuration files:

sudo make install-webconf
*** Nagios/Apache conf file installed ***

Make sure Apache rewrite and cgi modules are enabled:

sudo a2enmod rewritesudo a2enmod cgi

8. Creating Systemd Unit File

The following command installs a systemd unit file and also configure the nagios service to start on boot.

sudo make install-daemoninit
...
*** Init script installed ***

9. Creating User Account

To be able to access the Nagios web interface you’ll need to create an admin user called nagiosadmin

Use the following htpasswd command to create a user called nagiosadmin

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

You will be prompted to enter and confirm the user’s password.

New password: 
Re-type new password: 
Adding password for user nagiosadmin

Restart the Apache service for changes to take effect:

sudo systemctl restart apache2

10. Configuring Firewall

The firewall will secure your server against unwanted traffic.

If you don’t have a firewall configured on your server, you can check our guide about how to setup a firewall with ufw on ubuntu

Open the Apache ports by typing:

sudo ufw allow Apache

Installing Nagios Plugins

Switch back to the /usr/src directory and download the latest version of the Nagios Plugins from the project Github repository :

cd /usr/src/sudo wget -O nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz

Once the download is complete extract the tar file by typing:

sudo tar zxf nagios-plugins.tar.gz

Change to the plugins source directory:

cd nagios-plugins-release-2.2.1

Run the following commands one by one to compile and install the Nagios plugins:

sudo ./tools/setupsudo ./configuresudo makesudo make install

Starting Nagios

Now that both Nagios and its plugins are installed, start the Nagios service with the following command:

sudo systemctl start nagios

To verify that Nagios is running, check the service status by typing:

sudo systemctl status nagios

The output should look something like below indicating that Nagios service is active and running.

* nagios.service - Nagios Core 4.4.2
   Loaded: loaded (/lib/systemd/system/nagios.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-11-16 14:54:21 PST; 1s ago
     Docs: https://www.nagios.org/documentation
  Process: 18294 ExecStopPost=/bin/rm -f /usr/local/nagios/var/rw/nagios.cmd (code=exited, status=0/SUCCESS)
  Process: 18293 ExecStop=/bin/kill -s TERM ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 18315 ExecStart=/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS)
  Process: 18313 ExecStartPre=/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS)
 Main PID: 18325 (nagios)
    Tasks: 6 (limit: 2319)
   CGroup: /system.slice/nagios.service

Accessing the Nagios Web Interface

To access the Nagios web interface open your favorite browser and type your server’s domain name or public IP address followed by /nagios:

http(s)://your_domain_or_ip_address/nagios

Enter the nagiosadmin user login credentials and you will be redirected to the default Nagios home page as shown on the image below:

Install Nagios on Ubuntu

Conclusion

You have successfully installed the latest Nagios version from source on your Ubuntu system.

You should now check the Nagios Documentation and learn more about how to configure and use Nagios.

If you hit a problem or have feedback, leave a comment below.