In this tutorial we will learn, how to install Nagios 4 from source on Ubuntu 14.04 LTS. In this practical I have seen some problems while Nagios core 4 installation. Because in Ubuntu 14.04 LTS is shipped with new Apache 2.4 version ,it was quite obvious some extra steps we have to do.

I will also share some problems which I faced during the Nagios installation. More clearly it requires basic knowledge on Apache.I hope it will not help you to install Nagios whereas will get some information on Apache as well.

Description of Server and Software used

Operating System : Ubuntu 14.04 LTS Server edition
Arch : x86_64
Web Server : Apache 2.4
Nagios : Nagios Core 4.0.8 (Source Code)
Nagios Plugin : nagios-plugins 2.0.3 (Source Code)
Server IP Address : 192.168.56.102

Steps to install Nagios core 4.0 from source on Ubuntu 14.04 LTS

First login into your server and follow the given below steps to install Nagios core 4 on Ubuntu.

Step 1: Install Apache, PHP, OpenSSL and other requirements

Run the below given command to install Apache, PHP and other requirements.
Note: On Ubuntu 14.04 , apache 2.4 is default web server

sudo apt-get install wget build-essential apache2 php5 openssl perl make php5-gd wget  libgd2-xpm-dev libapache2-mod-php5 libperl-dev libssl-dev daemon

Step 2: Create user and group for Nagios

Run below given command to create user and group used for Nagios application.

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

Step 3: Download latest Nagios Core source package

You can download the current stable package from Nagios site.

At the time of writing this post Nagios Core version 4.0.8 was available.
Note: You can also download latest stable package. Hence, have to do slight changes in command with download URL.

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.8.tar.gz

Note: Nagios is also available at GitHub

Step 4: Extract downloaded Nagios core package

Now we are extracting the downloaded Nagios core package by using tar command
Note: Do the change with nagios package name, in case you have downloaded other version

tar -xvzf nagios-4.0.8.tar.gz

Step 5: Installing Nagios Core by compilation

Now install Nagios Core 4.x with compilation method.

First change to extracted out Nagios directory (From step 4)

cd nagios-4.0.8

Now start compiling the Nagios core package

sudo ./configure --with-command-group=nagcmd

sudo make all

sudo make install
sudo make install-init
sudo make install-config
sudo sudo make install-commandmode
sudo make install-webconf

IMPORTANT NOTE: When I run the command sudo make install-webconf. It gave some error with Apache path. Because we are working on Ubuntu 14.04 , apache path is /etc/apache2

See the below given error, which I faced

sharad@ubuntu:~/nagios-4.0.8$ sudo make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
/usr/bin/install: cannot create regular file ‘/etc/httpd/conf.d/nagios.conf’: No such file or directory
make: *** [install-webconf] Error 1
sharad@ubuntu:~/nagios-4.0.8$

Solution Instead of using sudo make install-webconf , use below given command . You should be inside nagios-4.0.8 directory.(I have just changed the path of Apache)

sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-enabled/nagios.conf

Now you can check, the nagios.conf file will be available at /etc/apache2/sites-enabled. Reference from my system

sharad@ubuntu:~/nagios-4.0.8$ ls -l /etc/apache2/sites-enabled/
total 4
lrwxrwxrwx 1 root root  35 Aug 25 02:34 000-default.conf -> ../sites-available/000-default.conf
-rw-r--r-- 1 root root 982 Aug 25 02:46 nagios.conf
sharad@ubuntu:~/nagios-4.0.8$ 

Step 6: Copy eventhandlers

Copy eventhandlers directory to /usr/local/nagios/libexec/

sudo cp -rvf contrib/eventhandlers/ /usr/local/nagios/libexec/

Now change the ownership of /usr/local/nagios/libexec/eventhandlers

sudo chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers

Step 7: Create Nagios init script

Create nagios init script to start/stop/restart the nagios with service command

sudo vi /etc/init/nagios.conf

Now paste the below given contents in /etc/init/nagios.conf file

description     "nagios 4.x core"
 
start on filesystems
stop on runlevel [1246]
 
respawn
 
setuid nagios
setgid nagcmd
console log
 
script
        exec /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
end script

Save and exit from file. It will create a file /etc/init/nagios.conf with above given contents.

Step 8: Enable apache rewrite and cgi module

Enable Apache’s rewrite and cgi module. And then restart the Apache2 service. Use below given command for this

sudo a2enmod rewrite
sudo a2enmod cgi
sudo service apache2 restart

Step 9: Start Nagios service

Start the nagios service

sudo service nagios start

Additional Information : From nagios init script you can start/stop/restart/status of nagios service by using below given commands

sudo service nagios start
sudo service nagios stop
sudo service nagios restart
sudo service nagios status

Step 10: Change the permission of nagios.cmd file

Change the permission of nagios.cmd file

sudo chmod 666 /usr/local/nagios/var/rw/nagios.cmd

Step 11: Set Nagiosadmin user’s password

While setting htpasswd for nagiosadmin user, I faced the problem with sudo: htpasswd: command not found
To solve this problem, install apache2-utils

sudo apt-get install apache2-utils

Now htpasswd is available. We are now setting nagiosadmin user’s password.
Type the password of nagiosadmin of your interest.

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

You may be wondered, why I only selected nagiosadmin user. The answer is hidden in /usr/local/nagios/etc/cgi.cfg file.

See below given reference. The nagiosadmin user is mentioned in the file and has many privileges on Nagios system. That is the reason we are setting nagiosadmin user password, so that it can login into Nagios Dashboard and take control.

sharad@ubuntu:~$ sudo grep nagiosadmin /usr/local/nagios/etc/cgi.cfg 
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin
sharad@ubuntu:~$

Step 12 : Access Nagios Server Dashboard

Now access the Nagios Server dashboard. Get ready with nagiosadmin password which we will use here

Open the web browser and type http://SERVER_IP_ADDRESS/nagios on URL field.

SERVER_IP_ADDRESS = Give IP address of your Server (Use ifconfing command to find ip address)

In our practical, we are using the IP Address 192.168.56.102 and suffix is /nagios

http://192.168.56.102/nagios

nagios-dashboard

Give username and password

Username : nagiosadmin
Password : ****** (Which you have set at Step 11)

After successful login, you will see the Nagios Dashboard.

nagios4-error

We will see lots of Red color alerts on screen. It is because of Nagios-plugin is not installed.
Now to clear these alerts, in next section we are installing Nagios-plugins on same Nagios Server

Install Nagios Plugins on Nagios server

Now install the Nagios plugins on newly built Nagios server. You can get latest nagios plugins information from Nagios Plugins community site.You can download and install latest from Nagios plugins site.

Right now, I got the new version as nagios-plugins-2.0.3

Download nagios-plugins-2.0.3 package.

wget http://www.nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz

Extract the package

tar -xvzf nagios-plugins-2.0.3.tar.gz

Change to extracted out directory

cd nagios-plugins-2.0.3/

Compiling the Nagios plugins

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

The nagios is default updated at every 90 seconds. Wait for a few minutes, you will get all alerts monitored and green in color.

See the below given reference.
Nagios 4

Read Some More Articles

22 Comments

  1. Much Appreciated…
    Very Easy and Understable steps….
    Easily Configured with your help.

    As I am new on nagios So, can you please tell me how to use it..
    Thankew so Much

    1. Hi AJ Sharma,

      Nagios is a server and it require client or medium to communicate with client machines.
      NRPE is widely used as nagios client. As you are beginner hence suggest to try first with NRPE.

      Regards
      Sharad

  2. Thank you for this tutorial. It was very clear for me and worked without any problem. This is the best instructions I have found to install Nagios 4 on Ubuntu. Thank you!

  3. Hi Sharad,

    Would you like to share document regarding NAGIOS email alert sending configuration? I would like to have a tested document. I am having LTS 14.04 installed.

    Expecting your kind reply!

    1. Hello Khaled,

      LAMP stands for –

      L = linux
      A = Apache
      M = MySQL
      P = PHP

      You can setup Nagios Server on Unix like Operating System.

      To setup the Nagios server, only two essential component is required that is Apache and php. I should not be loud be Apache here, but with some skill you can setup Nagios on other webserver as well for eg. Nginx .

      Nagios is using PHP code hence php is required.

      MySQL Database is required when you want to keep alerts in database . For eg Nagios + ndoutils . example – Opsview is another fork of nagios which works with database.

      I hope the answers will help you.

      Regards
      Sharad

  4. hello. First of all, thank you for doing this guide.
    I am stuck at the point where you define permissions for the nagcmd file. In my case it’s not where it’s supposed to be (/usr/local/nagios/var/rw/) . Any ideas?

      1. Since I followed your instructions, I had already started the nagios service at the previous step (step9 in your instructions).

        1. I reproduced the issue. And I am getting it. Try to reschedule any alert from Dashboard and check once again.

          1. I don’t understand what exactly you mean by rescheduling alerts from Dashboard.
            I have also reproduced the issue by a totaly new installation and copy pasted your instructions, but nothing changed. It’s the same error, the nagios.cmd file is not there. I have different nagios installations (nagios 3.5 on ubuntu 12.04, both installations have been installed by the same way, compiling from source and following the old ubuntu guide in the nagios doc) and the file is there. I have not noticed any serious error during the installation. I will try another installation guide and get back to you.

          2. I followed this guide and everything worked fine :
            I now got a working installation.
            I have to say that I was doing everything with a root account not with a regular one using sudo. Also this method defines nagios user and group as system (which I have not noticed elsewhere). I do have to modify my processes checks but it’s looking good!
            Also with that method I didn’t get the error you mentioned (with make install-webconf).

          3. Hi,

            It is good when you do not get any error.
            To solve this error, we can also define parameter “–with-httpd_conf=/etc/apache2/sites-available ” with ./configure script.

            using –system with useradd command does not make any difference, it is by default parameter. And it create system user.

            Regards
            Sharad

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.