Installing Nagios with Nginx, PHP-FPM and Nagiosgraph on Ubuntu 16.04

Update Your System

Always ensure your system is up-to-date before installing new packages by running:

Please note, this guide assumes you already have Nginx and PHP-FPM installed.

Versions

For the sake of clarity, the server I’m installing this on has the following versions of Ubuntu, Nginx and PHP-FPM:

Nagios User and Group

We’ll start by creating a new user and group specific to Nagios:

Required Dependencies

Next, we’ll install the dependencies we need:

Install Nagios Core

Then, we want to install Nagios Core. To get the latest version, you’ll have to check the Nagios website to see what the latest stable release is, and change the version number accordingly:

At the time of writing, this is v4.3.2. Once downloaded, we need to extract the archive:

Now this has been extracted, we can delete the archive and change into the extracted directory:

Before we actually go ahead and install Nagios, we need to configure it. We’re going to install Nagios with postfix, which we’ve already installed. To do this, we run the following command:

If you read the output, which should be rather verbose, it should let you know where everything is and how to continue:

As it says, review the options above, and if they look okay, run the following command to compile the main program and CGIs:

Installing from source always scares me at this point, as there’s quite a lot going on in the console. Don’t worry, this is normal. If this finishes and ends with ‘Enjoy’, then you’re doing well.

Now, we can get down to actually installing Nagios along with some initial scripts and sample configuration files:

In order for us to make external commands via the web interface to Nagios, we’ll need to sort out permissions. To do this, we’ll add the web server user to our nagcmd group:

Nagios Plugins

Just like Nagios Core, you’ll need to consult the Nagios website to find the latest version of the Nagios Plugins download:

At the time of writing, this is v2.2.1. Once downloaded, we need to extract the archive:

Now this has been extracted, we can delete the archive and change into the extracted directory:

Before installing Nagios Plugins, we’ll need to configure it:

Now, compile Nagios Plugins:

Then, install it:

To finish off our installation, we’ll need to copy over the check_* files to our Nagios configuration, so we can use them later for monitoring:

Install NRPE

Find the latest stable version of NRPE from the NRPE downloads page. If you’re starting to see a trend, you’ll be right. Please note, you may need to manually download the file from their website, as Sourceforce don’t seem to provide direct links. Go ahead and change the version number in the following command if necessary:

At the time of writing, this is v3.1.1. Once downloaded, we need to extract the archive:

If the above command fails with:

Then you may need to manually upload the tar.gz file, after downloading it from the Sourceforce website. Once downloaded to your machine, you can use scp or rsync to upload the file to your server:

After transferring the correct file to the server, go ahead and run the tar command again, as described above.

Now this has been successfully extracted, we can delete the archive and change into the extracted directory:

Before installing NRPE, we’ll need to configure it:

Now build and install NRPE:

Enable the NRPE service:

If you have xinetd installed, you’ll need to configure your /etc/xinetd.d/nrpe file to only include the IP address of the Nagios Server. For more information, Google it.

Now that Nagios, Nagios Plugins and NRPE are installed, we need to configure Nagios.

Configure Nagios

Open the main Nagios configuration file in your favourite text editor:

And remove the following comment:

Pro tip: to search in vim, use the / command following by the string you want to search for, and hit enter.

Then save and exit.

Next, we want to create the directory where all our server configuration files will live in:

Configure Nagios Contacts

Open the Nagios contacts configuration in your favourite text editor:

And change the default email address to your email address:

Then save and exit.

Configure check_nrpe Command

Let’s go ahead and add a new command to our Nagios configuration using our favourite text editor:

And add the following to the end of the file:

Then, save and exit. This allows you to use the check_nrpe command in your Nagios service definitions.

Configure Nginx

Up until now, you may have found most installation guides show you how to install Nagios, Nagios Plugins and NRPE. However, very few show you how to set this up on an Nginx and PHP-FPM environment.

We’ll start by installing the fcgiwrap dependancy, so we can run the Nagios CGI scripts:

Next, you’ll need to create a new virtual host for Nagios. Depending on your current setup, these are usually found in /etc/nginx/sites-available. If that’s the case, create a new configuration file for Nagios using your favourite text editor:

And use the following Nginx virtual host configuration:

Then, save and quit. Once that’s done, go ahead and add the symlink needed for the sites-enabled directory:

Please note, the above configuration assumes a few things. First, you have a self-signed certificate installed and a dhparam.pem file. If you don’t have these, you can generate them with the following commands:

You’ll notice this also includes location blocks for nagiosgraph, which we will install later within this guide, so don’t worry about those for now.

Finally, you’ll need to change the nagioshost.local server name to the actual virtual host URL you’ll be hosting this from.

Next, start the Nagios service and restart Nginx to take the configuration files into effect:

If there are no errors, then we can go ahead and add Nagios to our startup commands:

Now, we can go ahead and visit the public URL we’ve setup for Nagios. At this point, if you see a 404, 502 or other error, check your Nginx and virtual host error logs to see what isn’t happy.

If it’s all looking good, and your public URL looks something like this:

Nagios Core

Configuring Nagios Hosts

The final part to our configuration is setting up our hosts. Let’s make a new file for our server configuration:

And enter the following:

Then, save and quit. With the above configuration file, Nagios can work out if your host is up or down. If this is enough for you, then great. Otherwise, we’ll go ahead and add some more configuration files to check additional services such as ping and SSH. Simply add the following at the end of the same file:

Install Nagiosgraph

If you want to go one step further, you can install Nagiosgraph. This enables you to view pretty graphs which show the history of your statistics. To install Nagiosgraph, download the latest version from the Sourceforce website:

At the time of writing, this is v1.5.2. Once downloaded, we need to extract the archive:

Now this has been successfully extracted, we can delete the archive and change into the extracted directory:

Before installing Nagiosgraph, we can make some pre-flight checks:

Please be aware: because we’re using Nginx, expect to get some errors. If everything else is okay, you should end up with an output such as:

If all looks good, we can go ahead run the installation:

Tidying up

Now that everything’s up and running, so we can go ahead and do some tidying up. Simply remove the folders from your home directory which we downloaded:

And we’re done. Enjoy!

3 thoughts on “Installing Nagios with Nginx, PHP-FPM and Nagiosgraph on Ubuntu 16.04”

    1. Hey Youri, check your error log. The location of these files may change depending on your setup, but would be something like this:
      tail -f /var/log/nginx/error.log /var/log/nginx/nagios.appnetwork.nl.com-error.log

Leave a Reply

Your email address will not be published. Required fields are marked *