How to Install and Configure Nagios on Ubuntu 16

Try it in our public cloud & Get $5 Credit
CLAIM NOW

Nagios is a proven monitoring solution for your IT infrastructure. If you run complex networks of services and hardware, many subtle problems might arise and remain undetected. Unfortunately, when the effects of these issues manifest, they usually do so in ways that negatively impact or take down critical services on which you depend for work or revenue. Often, spotting these issues in advance makes them easier to mitigate, and even when it doesn’t, it’s better to detect problems yourself than have your customers do so for you. If you run production infrastructure and are not monitoring it, then you should start doing so today. If unsure of what to use, it is difficult to go wrong with Nagios.

Getting Started

Before you begin, make sure you have the following prepared:
• 1 Remote server (Cloud Server or Dedicated Server) running Ubuntu 16
• All commands should be run as the root user

Tutorial

Begin by installing the basic packages on which Nagios depends.

apt-get install gcc unzip make

Nagios also needs the Apache web server and PHP programming language. Continue by installing both.

apt-get install apache2 php libapache2-mod-php php-mcrypt

Now that PHP is installed, you’ll need to tell Apache how to run scripts. Here we configure it to do so.

nano /etc/apache2/mods-enabled/dir.conf

By default, many PHP apps use the index.php script as an entrypoint. Ensure that index.php is listed first in the below line from your configuration file:

<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Apache is now configured to run PHP scripts. Let’s restart it so the configuration changes take effect.

systemctl restart apache2.service

Next we’ll add a Nagios user for Apache, and for the nacmd utility.

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

In order to get the latest Nagios, we’ll download it directly from the official site. We’ll also retrieve some of the more popular plugins you’ll likely want running in your installation.

cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.1.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz

Unzip the archive you’ve just downloaded and install Nagios itself.

tar -zxvf nagios-4.2.1.tar.gz
cd nagios-4.2.1/
./configure --with-nagios-group=nagios --with-command-group=nagcmd --with-httpd_conf=/etc/apache2/sites-enabled/
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf

You’ll now need to edit the contact file, changing the email address to one that you monitor.

nano /usr/local/nagios/etc/objects/contacts.cfg

[...]
# This contact definition inherits a lot of default values from the 'generic-contact'
# template which is defined elsewhere.
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
[...]

Nagios needs a password to be set in order for you to access the web interface. Since monitoring is a critical aspect of your infrastructure, you’ll want to pick a strong password and keep it secure. Anyone who has access to Nagios can gain vital insight into how your systems are set up, and might use said knowledge to orchestrate attacks.

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
a2enmod cgi
systemctl restart apache2.service

Next, unzip and install the archive containing Nagios’ plugins.

cd /tmp
tar -zxvf nagios-plugins-2.1.2.tar.gz
cd nagios-plugins-2.1.2/
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

It is easy to make mistakes when editing the Nagios config file. Let’s ensure that it has no errors.

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Nagios Core 4.2.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 09-06-2016
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

Nagios does not ship a systemd unit file, a specification needed to start and stop Nagios. Create one with these contents.

nano /etc/systemd/system/nagios.service

[Unit]
Description=Nagios
BindTo=network.target
[Install]
WantedBy=multi-user.target
[Service]
User=nagios
Group=nagios
Type=simple
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg

With the unit file in place, we can now use systemd to enable Nagios to start on boot. We can also start it immediately.

systemctl enable /etc/systemd/system/nagios.service
systemctl start nagios.service

Nagios should be up and running. To check its status, access the built-in web interface. Visit http://your_ip/nagios/ to continue. The default credentials are “nagiosadmin” for the username, and your password set previously as the password.

Conclusion

That was easy. You’re now keeping tabs on this server with Nagios, and can detect many significant issues before they become critical. Many system administrators fail to be proactive about watching their servers, so share this article with anyone you know who isn’t sold on the need for good monitoring. If you found this article helpful, feel free to share it with your friends and let us know in the comments below!