Installing Nagios under Nginx on Ubuntu 14.04 LTS

Nagios is an excellent open source monitoring solution that can be configured to monitor pretty much anything. In this article, I’ll describe how to install Nagios under Nginx on Ubuntu 14.04 LTS.

First of all, check that the system is fully up to date:

Next, install the build-essential package so that we can build Nagios and its plugins from source:

Install Nginx, and verify that it has started:

Install libgd2-xpm-dev, php5-fpm, spawn-fcgi and fcgiwrap:

Next, create a nagios user:

Issue the following commands to create a nagcmd group, and add it as a secondary group to both the nagios and www-data users:

Download the latest Nagios core distribution from http://www.nagios.org/download – at the time of writing this was version 4.0.7.

Untar the downloaded source tarball, and configure:

As you can see above, the appropriate command group has been specified, and we’ve indicated that Nagios configuration files should be installed under /etc/nagios.

If the configure returns correctly, make the software and install the appropriate components:

install-init will install an appropriate init script, install-config will install sample configuration files, and install-commandmode will set permissions on the external command directory.

To finish up the Nagios installation, create a symlink to our versioned prefix. This will facilitate easier upgrades:

As Nginx doesn’t come with an htpasswd tool (and we’ll be protecting our Nagios installation with basic HTTP auth for now), create a simple Perl script to do the job:

Next, use this script to create an htpasswd file – this will be the user you’ll access Nagios via:

Modify /etc/nagios/cgi.cfg, and substitute the <user> specified above for nagiosadmin (the default). Make sure you update /etc/nagios/objects/contacts.cfg too:

Create a log directory for Nagios and set appropriate ownership. Update nagios.cfg appropriately:

Next, we need to build and install the Nagios plugins. These can be downloaded from http://www.nagios.org/download/plugins. I’ll install some prerequisites so that the MySQL and SSL-enabled plugins are built:

Untar the source tarball, and configure the plugins:

By default, the prefix will be /usr/local/nagios so we don’t need to specify it here (our earlier symlink takes care of things for us).

Make and install the plugins:

We’re almost ready to start Nagios. Add it to automatically start at the appropriate runlevels:

Run a check of the configuration:

If no errors are reported (the sample configuration monitors a few services on localhost and should be enough to get you started), start up Nagios:

Check that fcgiwrap is up (it should have been started automatically after installation):

And ensure that php5-fpm is running:

It should be noted that the default configurations of fcgiwrap and php5-fpm use UNIX sockets rather than TCP, which should be fine for our purposes.

Verify the status of Nginx, it should be running as it would have been started after installation:

Create a configuration file for your Nagios installation as follows:

Ensure that the correct socket paths are specified.

Disable the default site included with the Nginx installation, and enable the Nagios site:

Reload Nginx:

You should now be able to browse to http://nagios.example.com and access your Nagios installation.

A few things remain to be done. Install Postfix so that Nagios can send email:

Select “Internet Site” when prompted for mailserver type. Next, install heirloom-mailx to make the /usr/bin/mail command available:

Modify /etc/nagios/objects/commands.cfg and update the mail commands with the appropriate path:

Reload Nagios:

And that’s it!

Note: There is an issue when scheduling command checks that results in the following error “Error: Could not open command file ‘/usr/local/nagios-4.0.7/var/rw/nagios.cmd’ for update!” even though the command file permissions and ownership are correct. I have a post open in the Nagios forums to get to the bottom of this, but for now, you can add the following to line 194 of /etc/init.d/nagios and restart Nagios:

so that the command file can be written to by the web server user www-data. I will update this post once I get a solution in the Nagios forums. Another workaround is to change the group ownership of the /usr/local/nagios/var/rw directory to www-data: