Nagios 4 and NGiNX

I decided to write this post after a weekend of wanting to set up Nagios using NGiNX on my Debian system. There are one or two guides out there which document how this is done but I had to go a little farther than either of them to get it working, and thought I’d make my own blog post to fill in the missing pieces. I’m retracing my steps from memory; I don’t think I missed anything, but if I did let me know. 🙂

Prerequisites: a system set up with the following packages:

nginx php5-fpm apache2-utils build-essential spawn-fcgi fcgiwrap

Step 1: go get nagios and nagios-plugins from the nagios website. untar both into /tmp; they’ll create their own directories.

Step 2: Create the nagios user if you don’t have one already:

adduser --system --no-create-home --disabled-login --group nagios

You can also create a group for users authorized to perform commands in nagios, which is a clever idea from this blog:

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

Step 3: Build and compile nagios; on a Debian system you want something like this:

./configure --sysconfdir=/etc/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-user=nagios --with-command-group=nagcmd

(remember to change the command-group to nagios if you didn’t use a seperate group for nagios commands)

After the configure, do:

make all
make install
make install-init
make install-config
make install-commandmode

Step 4: Now build nagios-plugins.

./configure --with-nagios-user=nagios --with-nagios-group=nagios && make && make install

Step 5: Now do some post-compile config, namely, we need to tell nagios to log in /var/log/nagios, not /usr/local/nagios/var/nagios like it’s going to want to do:

mkdir /var/log/nagios
touch /var/log/nagios/nagios.log
chown nagios:nagios /var/log/nagios
then edit /etc/nagios/nagios.cfg with your favorite editor and change log_file to /var/log/nagios

Next, change url_html_path in /etc/nagios/cgi.cfg to a single slash, since will be our hostname for nagios:


Lastly, set a password for the nagiosadmin user and make sure your init script is properly permissioned and the system knows about it:

htpasswd -cb /etc/nagios/htpasswd.users nagiosadmin !AGoodPassword
chmod +x /etc/init.d/nagios && /usr/sbin/update-rc.d -f nagios defaults

Now let’s set up nginx:

Installing fcgiwrap doesn’t actually put the fcgiwrap config where it needs to be. The nginx wiki has the solution:

cp /usr/share/doc/fcgiwrap/examples/nginx.conf /etc/nginx/fcgiwrap.conf

Make sure that in /etc/php5/fpm/pool.d/www.conf your socket is set so that nginx can access it:

listen.owner = nginx = www-data

Do the same for fcgiwrap’s socket in /etc/init.d/fcgiwrap:


Make a symlink called fastcgi.conf that goes to fastcgi_params in /etc/nginx:

ln -s /etc/nginx/fastcgi_params /etc/nginx/fastcgi.conf

And lastly, you want to create /etc/nginx/conf.d/nagios.conf:

server {
access_log /var/log/nginx/nagios.access.log;
error_log /var/log/nginx/nagios.error.log;

auth_basic "Restricted Nagios Area!";
auth_basic_user_file /etc/nagios/htpasswd.users;

root /usr/local/nagios/share;
index index.php index.html;

location / {
try_files $uri $uri/ index.php;

location /nagios {
alias /usr/local/nagios/share/;

location ~ \.php$ {
root html;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/share$fastcgi_script_name;
fastcgi_split_path_info ^(.+.php)(.*)$;

location ~ ^/nagios/(.*\.php)$ {
alias /usr/nagios/share/$1;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;

location ~ \.cgi$ {
root /usr/local/nagios/sbin/;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_param AUTH_USER $remote_user;
fastcgi_param REMOTE_USER $remote_user;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/fcgiwrap.socket;

location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;

Lastly, restart some things, in this order:

service nginx restart
service php5-fpm restart
service fcgiwrap restart
service nagios restart

At this point everything should be working. You can now begin configuring hosts in nagios.