Configure Nagios 4 with Nginx on Ubuntu 20.04

Install Nagios 4 + Nginx + Ubuntu 20.04

System Requirements

  • Ubuntu Server 20.04
  • 2gb RAM, 2 cores
  • 40gb storage
  • Nginx

Step 1: Update the System

sudo apt update
sudo apt upgrade -y

Step 2: Install required packages

sudo apt install wget unzip curl openssl build-essential libgd-dev libssl-dev php7.4 php7.4-fpm fcgiwrap php7.4-gd -y

Step 3: Download and install Nagios

  • Download Nagios Core setup files
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.6.tar.gz
  • Extract the downloaded files
sudo tar -zxvf nagios-4.4.6.tar.gz
  • Navigate to the setup directory
cd nagios-4.4.6 
  • Run the Nagios Core configure script.
sudo ./configure
  • Compile the main program and CGIs.
sudo make all
  • Now lets create a nagios user and group, and also add the www-data user to the nagios group.
sudo make install-groups-users
sudo usermod -a -G nagios www-data
  • Install Nagios
sudo make all
  • Initialize all the installation configuration scripts.
sudo make install-init
  • Install Command Mode
sudo make install-commandmode
  • Install Configuration Files
sudo make install-config

Step 4: Configure Nginx

  • Create a file called nagios in /etc/nginx/sites-available using the command below.
sudo nano /etc/nginx/sites-available/nagios
  • Paste the following content and replace the server_name with your actual domain
server {
server_name nagios.your-domain.com;
root /usr/local/nagios/share;
listen 80;
index index.php index.html index.htm;
access_log /var/log/nginx/nagios.access.log;
error_log /var/log/nginx/nagios.error.log;
auth_basic "Nagios Access";
auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
# Fixes frames not working
add_header X-Frame-Options "ALLOW";
location ~ \.php$ {
try_files $uri = 404;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
include fastcgi.conf;
}
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 fastcgi.conf;
fastcgi_pass unix:/run/fcgiwrap.socket;
}
# Fixes the fact some links are expected to resolve to /nagios, see here.
location /nagios {
alias /usr/local/nagios/share;
}
}
  • Save and exit
  • Now lets symlink /etc/nginx/sites-available/nagios to /etc/nginx/sites-enabled/nagios
sudo ln -s /etc/nginx/sites-available/nagios /etc/nginx/sites-enabled/nagios
  • Now lets restart nginx
sudo systemctl reload nginx 

Step 5: Add a User for Nagios

sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

Step 6: Install Nagios Plugins

  • Download the Nagios core plugin
cd ~/
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
  • Extract the downloaded plugin
sudo tar -zxvf nagios-plugins-2.3.3.tar.gz
  • Navigate to the plugins directory
cd nagios-plugins-2.3.3
  • Run the plugin configure script.
sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios
  • Compile Nagios core plugins
sudo make
  • Install the plugins
sudo make install

Step 7: Verify Nagios Configuration

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Verify that nagios is running correctly
  • Now lets start and enable nagios to run at system startup
sudo systemctl start nagios
sudo systemctl enable nagios

Step 8: Access the Web Interface

  • Restart nagios
sudo systemctl reload nagios
  • Open your web browser and access the Nagios web interface via the url http://nagios.your-domain.com
Nagios admin panel
Nagios admin dashboard
authorized_for_system_information=nagiosadmin, username
authorized_for_configuration_information=nagiosadmin, username
authorized_for_system_commands=nagiosadmin, username
authorized_for_all_services=nagiosadmin, username
authorized_for_all_hosts=nagiosadmin, username
authorized_for_all_service_commands=nagiosadmin, username
authorized_for_all_host_commands=nagiosadmin, username
  • Now Click on Hosts to see the servers that are being monitored.
Nagios monitored hosts

Step 9: Adding Hosts to Nagios Server For Monitoring

  • To create new object definitions, you can create a sub-directory to place your files as below:
sudo mkdir /usr/local/nagios/etc/objects/servers
  • Open main Nagios configuration file and insert the line, cfg_dir=/usr/local/nagios/etc/objects/servers under the Object Configuration File(s) section.
sudo nano /usr/local/nagios/etc/nagios.cfg
nagios host object definition
  • Next create your host object definition file
sudo nano /usr/local/nagios/etc/objects/servers/hosts.cfg
  • define you hosts details such as hostname, IP address and the host template to use.
                                                                      # REMOTE HOST DEFINITION                                                                                                                            define host {
use servers
host_name server1
alias my_first_server
address 192.168.20.21
}
define host {
use servers
host_name server2
alias my_second_server
address 192.168.20.29
}
# REMOTE HOST GROUP DEFINITION

define hostgroup {
hostgroup_name my-linux-hosts
alias Linux Hosts
members server1,server2
}
  • Lets create a custom host and service definition template as shown below.
sudo nano /usr/local/nagios/etc/objects/servers/hosts-service-template.cfg
  • Paste the below content and edit accordingly.
# Host Template Definition 
define host{
name servers
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
check_command check-host-alive
check_interval 5
max_check_attempts 2
notification_interval 0
notification_period 24x7
notification_options d,u,r
contact_groups server-admins
register 0
}
# Service Template definition
define service{
name hosts-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
notification_interval 0
is_volatile 0
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 2
notification_period 24x7
notification_options w,u,c,r
contact_groups server-admins
register 0
}
  • Save and exit the config
sudo nano /usr/local/nagios/etc/objects/servers/server-admins-contacts.cfg
  • Paste the following content and edit accordingly.
# Define Your Contacts Here 
define contact {
contact_name your-name ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias your-full-name ; Full name of user
email your-name@your-email.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
# Create Contact Group
define contactgroup {
contactgroup_name server-admins
alias server Administrators
members your-name
}
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Verify that nagios is running correctly
  • Restart Nagios
sudo systemctl reload nagios

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store