How To Install and Configure Nagios on Debian 9

Posted 

5 min read

Nagios is a popular open source monitoring software. It 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 of a critical infrastructure component Nagios sends notification alerts.

This tutorial explains how to install and configure the latest version of Nagios Core on Debian 9.

Prerequisites

You’ll need to be logged in as a user with sudo access to be able to install packages.

Installing Nagios on Debian

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

Start by installing the following packages that are necessary for compiling and running Nagios:

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

1. Downloading Nagios

Navigate to the /usr/src directory and download the latest version of Nagios from the project Github repository with wget:

cd /usr/src/
sudo wget https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.3.tar.gz

Once the download is complete extract the tar file:

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

Start the build process by running the configure script:

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

Upon successful completion, you will see the following message:

*** Configuration summary for nagios 4.4.3 2019-01-15 ***:

 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:  /usr/sbin/sendmail
                  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):  /usr/sbin/traceroute


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

Run the make command to start the build process:

sudo make all

The compilation may take some time, depending on your system. Once the build process is completed, you will see something like this:

*** Compile finished ***
...
Enjoy.

3. Creating Nagios User And Group

To create a new system nagios user and group type:

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

Run 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

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 create the Apache configuration files:

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

Make sure Apache rewrite and cgi modules are enabled:

sudo a2enmod rewrite
sudo a2enmod cgi

8. Creating Systemd Unit File

The following command installs a systemd unit file and enables 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.

In this tutorial we will create a user named 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 Debian

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/setup
sudo ./configure
sudo make
sudo make install

Starting Nagios

Now that You have Nagios installed on your Debian server, start it with:

sudo systemctl start nagios

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

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.3
   Loaded: loaded (/lib/systemd/system/nagios.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-02-10 17:07:21 CST; 9s ago
     Docs: https://www.nagios.org/documentation
  Process: 26241 ExecStart=/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS)
  Process: 26239 ExecStartPre=/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS)
 Main PID: 26243 (nagios)
    Tasks: 6 (limit: 4915)
   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:

Conclusion

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

You should now visit 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.