Objective

Install and configure Nagios on Ubuntu 18.04 Bionic Beaver

Distributions

Ubuntu 18.04

Requirements

A working install of Ubuntu 18.04 with root privileges

Difficulty

Easy

Conventions

  • # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - requires given linux commands to be executed as a regular non-privileged user

What is Nagios

Nagios is one of the best enterprise grade server monitoring solutions available. Unlike cPanel, it's available across a wide range of Linux distributions, including Debian and Ubuntu. Since Ubuntu 18.04 is an LTS release, upgrading your servers and running Nagios on them could be a great idea. Even though Nagios isn't up to date in the Ubuntu repositories, and setup isn't turn-key, it's still not too hard to get it running.

Install The Requirements

Before you can install Nagios on your Ubuntu server, you're going to need to install the build dependencies. Yes, that means that you're going to be building Nagios from source, but the process isn't too complex. Begin by installing the packages you need.
$ sudo apt install build-essential libgd-dev openssl libssl-dev unzip apache2


Create A Nagios User

It's best for security reasons to create a user and group specifically to run Nagios. Start by making both the new user and the new group.
$ sudo useradd nagios
$ sudo groupadd nagcmd
Add your nagios user to the nagcmd group.
$ sudo usermod -a -G nagcmd nagios

Build Nagios

Next, you're going to need to download and build the Nagios source. Go the the Nagios Core download page, and download the latest stable release of Nagios 4. Change in the directory where you downloaded Nagios. Unpack it with tar.
$ tar xpf nagios-*.tar.gz
Now, change into the resulting directory.
$ cd nagios-4.3.4
Prepare the Nagios source. Configure it to use the user and group that you created.
$ ./configure --with-nagios-group=nagios --with-command-group=nagcmd
Nagios Compile Configuration On Ubuntu 18.04
Nagios Compile Configuration On Ubuntu 18.04
That will only take a couple of seconds. Review the output. Make sure that nothing looks ridiculously out of place. Build everything. Don't forget to use the -j flag with the number of cores on your machine to speed up the process.
$ make -j4 all
It should be a fairly fast compile on modern hardware. When it's finished, you can install all of the Nagios components that you just compiled.
$ sudo make install
$ sudo make install-commandmode
$ sudo make install-init
$ sudo make install-config
Before you leave the folder, you need to copy the provided Apache configuration over to Apache's sites-available folder.
$ sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
Then, add Apache's user to your nagcmd group.
$ sudo usermod -a -G nagcmd www-data


Install The Plugins

Nagios has loads of great plugins that enhance its functionality. It's best to get those and install them now too. Download them from their download page. Change into the directory where you downloaded them, and unpack them.
$ tar xpf nagios-plugins-*.tar.gz
Change into the resulting directory.
$ cd nagios-plugins-2.2.1
Configure them like you did Nagios, but include OpenSSL support too.
$ ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --with-openssl
When the configuration finishes, make and install the plugins.
$ make -j4
$ sudo make install

Basic Configuration

Before you can start using Nagios, you're going to need to tweak the base configurations that you already installed. Start by opening up the main configuration file. It's located at /usr/local/nagios/etc/nagios.cfg Find the line below, and uncomment it. When you're gone, save and exit.
cfg_dir=/usr/local/nagios/etc/servers
Now, make that directory.
$ sudo mkdir /usr/local/nagios/etc/servers
Open up the contacts configuration. You should change it to refelct your actual email address. The file is at /usr/local/nagios/etc/objects/contacts.cfg. Change the line below to match your email.
email     nagios@localhost; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

Apache Setup

In order for Apache to serve the Nagios interface, you need to enable a couple of Apache modules. Begin with that.
$ sudo a2enmod rewrite
$ sudo a2enmod cgi
Now, create an admin password for a Nagios admin user. After you enter the command, you'll be prompted to create you password.
$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
You already copied over the default Apache configuration for Nagios. You just need to enable it. Create the symlink between the copy in sites-available and sites-enabled.
sudo ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/
Nagios Apache Configuration On Ubuntu 18.04
Nagios Apache Configuration On Ubuntu 18.04


Its a good idea to restrict access to your Nagios server. It's best to only allow access from localhost and your own computer. Open you Apache Nagios configuration file, and find the line Order allow,deny. Make it look like the example below.
Order deny,allow
Deny from all
Allow from 127.0.0.1 172.86.186.XXX
Obviously, substitute your actual external IP for the final IP. The XXX part is just there to obscure the example. Change this for both entries. Save it and exit. Start up Apache.
$ sudo systemctl start apache2

Create the Service

Nagios doesn't automatically come with a Systemd service file. You need to make it at /etc/systemd/system/nagios.service. Copy the one below.
[Unit]
Description=Nagios
BindTo=network.target

[Install]
WantedBy=multi-user.target

[Service]
Type=simple
User=nagios
Group=nagcmd
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
Enable and start your service.
$ sudo systemctl enable /etc/systemd/system/nagios.service
$ sudo systemctl start nagios

Open Nagios

Nagios is now available at http://your_ip/nagios. When you arrive, you'll be prompted to enter the password that you created for nagiosadmin. From there, you'll be able to browse the Nagios admin interface.

Closing Thoughts

Your Nagios installation is now up and running. That Ubuntu server will be able to monitor itself, and you can see the data presented in real time through the web interface.
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME or create a JOB ALERT on LinuxCareers.com job portal.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.