Skip to content

Nagios on Centos 7.x behind Nginx

Nagios is a open-source monitoring tool, can be deployed on ubuntu, centos/rhel machines. In this tutorial we will be deploying Nagios Core on Centos 7.4 cloud vm hosted at Google Cloud Platform, and run it behind Nginx web server.


Procedure:

We shall build a cloud machine on GCP [ Google Cloud Platform ] add the DNS configuration to CloudFlare and create VPN Pool for Firewall configurations on GCP, tackle SELinux and install and configure Nagios.


Disclaimer:

This is just a custom guide to install and configure Nagios on Centos 7.x, this could be more technical and advanced, if you do not understand or don’t know what you are doing, I kindly request you to take extreme caution and the components used here on GCP are billable. SKYDEVOPS/I/WE do not take any responsibility if the guide causes any serious damage or issues, which you have to take full responsibility.


Guide:

Step-1: Creating a Cloud Instance on GCP [ Google Cloud Platform ], following image illustrates building a Centos 7.x image

Centos 7 VM creation on Google Cloud Platform
Centos 7 VM creation on Google Cloud Platform

Step-2: Following is the image which shows the instance details

GCP Instance details
GCP Instance details

Step-3: Adding the DNS and settings FQDN on cloud flare

DNS and FQDN Settings on cloudflare
DNS and FQDN Settings on cloudflare

Step-4: Adding the DNS and settings FQDN on vm instance

$ sudo vi /etc/hosts
IPV4_ADDRESS FQDN SHORT_HOSTNAME

Step-5: Verifying FQDN

$ hostname
$ hostname -i
$ hostname -f

Step-6: Setting Locale

$ sudo timedatectl set-timezone Asia/Kolkata

Step-7: Set SELinux in permissive mode

$ sudo su -
$ setenforce 0

Step-8: Install Dev Tools, PHP, Nginx

$ sudo yum install nginx php php-fpm php-common gcc glibc glibc-common gd gd-devel make net-snmp unzip -y
$ sudo yum groupinstall 'Development Tools' -y

Step-9: Nginx – Nagios Configuration

$ cd /etc/nginx/conf.d
$ sudo vi nagios.conf
server {
	listen 80;
	server_name nagios.yebbare.com;
	access_log /var/log/nginx/nagios-access.log;
	error_log /var/log/nginx/nagios-error.log info;
	root /usr/local/nagios/share;
	index index.php;
	auth_basic "Nagios Restricted Access";
	auth_basic_user_file /usr/local/nagios/passwd;

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

	location ~ .cgi$ {
		root /usr/local/nagios/sbin/;
		include fastcgi_params;
		rewrite ^/nagios/cgi-bin/(.*).cgi /$1.cgi break;
		fastcgi_param AUTH_USER $remote_user;
		fastcgi_param REMOTE_USER $remote_user;
		fastcgi_pass unix:/var/run/fcgiwrap.socket;
		fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin/$fastcgi_script_name;
		fastcgi_param PATH_INFO $fastcgi_script_name;
	}

	location ~ .php$ {
		include fastcgi_params;
		fastcgi_pass unix:/var/run/php-fpm/nagios.socket;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME /usr/local/nagios/share$fastcgi_script_name;
		fastcgi_param PATH_INFO $fastcgi_script_name;
	}
	
	location ~ (.*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf))$ {
		root /usr/local/nagios/share/;
		rewrite ^/nagios/(.*) /$1 break;
		access_log off; expires max;
	}
}

Step-10: Firewall Configuration

sudo firewall-cmd --permanent --add-port=80/tcp --zone=public
sudo firewall-cmd --reload

Step-11: Creating User

useradd nagios
usermod -aG nagios nginx

Step-12: create nagios.conf for PHP-FPM

sudo vi /etc/php-fpm.d/nagios.conf
[nagios]

listen = /var/run/php-fpm/nagios.socket
listen.owner = nginx
listen.group = nginx
listen.mode=0660
listen.allowed_clients = 127.0.0.1

user = nagios
group = nagios
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

slowlog = /var/log/php-fpm/www-slow.log

php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/php-fpm/nagios-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

Step-13: Restarting PHP-FPM and Enabling at start-up

sudo systemctl restart php-fpm
sudo systemctl enable php-fpm

Step-14: Download Nagios Core

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.3.4.tar.gz

Step-15: Extracting the Binaries

tar zxf nagios-4.3.4.tar.gz
cd nagios-4.3.4

Step-16: Compiling the binaries and installing Nagios

sudo su -
cd nagios-4.3.4
./configure
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf

Step-17: Verifying Nagios Configuration

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Step-18: Installing Extra Plugins

yum install nagios-plugins-all -y

Step-19: creating soft link for new pluginas and also remove the default plugins

rm -rf /usr/local/nagios/libexec
ln -s /usr/lib64/nagios/plugins /usr/local/nagios/libexec
chown -R nagios:nagios /usr/local/nagios/libexec

Step-20: Start Nagios and Enable nagios for startup at boot

systemctl start nagios
systemctl enable nagios

Step-21: Creating password for nagiosadmin user

htpasswd -c /usr/local/nagios/passwd nagiosadmin

Step-22: Creating directory for user and creating links

mkdir /usr/local/nagios/share/nagios
cd /usr/local/nagios/share/nagios/
ln -s /usr/local/nagios/share/stylesheets/ stylesheets
ln -s /usr/local/nagios/share/js js

Step-23: Installing spawn-fcgi

yum install fcgi-devel spawn-fcgi -y

Step-24: Installing fcgiwrap and installing by compiling binaries

cd /usr/local/src/
git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i
./configure
make
make install

Step-25: Adding the spawn-fcgi configuration

vim /etc/sysconfig/spawn-fcgi
FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS="-M 0700"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"

Step-26: Starting and Enabling Spawn-fcgi

systemctl start spawn-cgi
systemctl enable spawn-cgi

Shashi View All

A passionate devops and automation engineer

5 thoughts on “Nagios on Centos 7.x behind Nginx Leave a comment

Leave a comment