How to serve nagios with nginx

A guide to configuring nginx for serving Nagios3 on Ubuntu 11.10 using fcgiwrap and php-fpm

Nagios is a monitoring system, that enables you to check in on virtually any proces on your server, or on multiple servers. It also enables you to set up email alerts so the system administrators can be alerted in time when something is wrong. This is a guide for setting up Nagios3 with Nginx on Ubuntu 11.10.

Prerequisites

You need to have a server running ubuntu 11.10, nginx, fcgiwrap, spawn-fcgi and php-fpm. Linode has nice guides on setting up LEMP and fcgiwrap. This guide from nas-admin.org has the deetails of how to set up php-fpm. There are tons of good guides out there, this is just what I happened to stumble upon first.

I assume that you are comfortable using the shell. Most of the commands in this guide needs to be run as root or with sudo, it is omitted for brevity.

As always make sure your system is up to date before installing new packages.

$ apt-get update && apt-get upgrade
Install nagios3
$ apt-get install nagios3

You will be prompted for a password during installation, remember it.
Nagios3 is installed to /usr/share/nagios3 and configuration files to in /etc/nagios3/. The bulk of the files nginx needs access to are in /usr/share/htdocs except for the stylesheets that are located in /etc/nagios3/stylesheets we will set up an alias for these later.

Configure nagios

We are going to set up nagios on the subdomain nagios.example.tld but nagios assumes it will run under example.tld/nagios lets cange the following parameter in /etc/nagios3/cgi.cfg

url_html_path=/
Configure nginx

First lets add a few shorthands for our sockets in case we want to configure additional services to run with php-fpm and fcgi this will save us some time. Add two upstream clauses in the http section of /etc/nginx/nginx.conf.

 upstream php {
     server unix:/var/run/php5-fpm.socket;
 }

 upstream fcgiwrap {
     server unix:/var/run/fcgiwrap.socket;
 }

Add a new virtualhost as /etc/nginx/sites-available/nagios

Enable the virtualhost

$ ln -s /etc/nginx/sites-available/nagios /etc/nginx/sites-enabled/nagios

Restart nginx

$ sudo /etc/init.d/nginx restart
Celebrate

Visit your nagios installation at http://nagios.example.tld