How to Install and Configure Nagios on Debian 9

By 

Published on

5 min read

Install and Configure Nagios on Debian

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:

Terminal
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 :

Terminal
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 :

Terminal
sudo tar zxf nagios-*.tar.gz

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

Terminal
cd nagioscore-nagios-*/

2. Compiling Nagios

Start the build process by running the configure script:

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

Upon successful completion, you will see the following message:

output
*** 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:

Terminal
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:

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

3. Creating Nagios User And Group

To create a new system nagios user and group type:

Terminal
sudo make install-groups-users

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

Terminal
sudo usermod -a -G nagios www-data

4. Install Nagios Binaries

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

Terminal
sudo make install

You should see the following output:

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:

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

6. Install Nagios Configuration Files

Install the sample Nagios configuration files with:

Terminal
sudo make install-config
output
*** 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:

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

Make sure Apache rewrite and cgi modules are enabled:

Terminal
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.

Terminal
sudo make install-daemoninit
output
...
*** 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:

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

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

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

Restart the Apache service for changes to take effect:

Terminal
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:

Terminal
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 :

Terminal
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:

Terminal
sudo tar zxf nagios-plugins.tar.gz

Change to the plugins source directory:

Terminal
cd nagios-plugins-release-2.2.1

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

Terminal
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:

Terminal
sudo systemctl start nagios

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

Terminal
sudo systemctl status nagios

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

output
● 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:

url
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 Debian on Debian

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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page