Configure Nagios 4 with Nginx on Ubuntu 20.04
Nagios is an open source software for system and network monitoring. It remotely monitors specified hosts and services, alerting the administrator in case critical services in a network, such as SMTP, HTTP, SSH, FTP and others fail. With Nagios, you can also monitor attributes such as CPU load, memory, disk usage, logged in users, running processes, etc.
This article describes how to install Nagios 4 on Ubuntu 20.04 with Nginx webserver rather than the default Apache webserver.
System Requirements
- Ubuntu Server 20.04
- 2gb RAM, 2 cores
- 40gb storage
- Nginx
Step 1: Update the System
Make sure that all Ubuntu OS packages installed on the server are up to date. You can do that by running the following commands:
sudo apt update
sudo apt upgrade -y
Step 2: Install required packages
Now we need to install the required packages with the following command:
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
This installs and configures the external command file.
sudo make install-commandmode
- Install Configuration Files
sudo make install-config
Step 4: Configure Nginx
In order to access the Nagios admin page, we need to configure Nginx to serve it via HTTP/HTTPS.
- 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
Create a user and set the password when prompted.
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Step 6: Install Nagios Plugins
Nagios Core needs plugins to operate properly. The following steps will walk you through installing 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
Use the command below to verify that nagios is running correctly.
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
If nagios is running correctly then it should give the output below
- 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
Firstly, lets edit the url path in /usr/local/nagios/etc/cgi.cfg
from url html path=/nagios to url html path=/
- Restart nagios
sudo systemctl reload nagios
- Open your web browser and access the Nagios web interface via the url http://nagios.your-domain.com
That should load the admin login interface as shown below
To log in, use nagiosadmin as your username and the password you set during the user account creation. You can now access the dashboard and begin configuring Nagios.
NOTE: If you set a username other than nagiosadmin then you need to open the cgi.cfg file in /usr/local/nagios/etc and edit accordingly.
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
Use * to authorize any user who has authenticated to the webserver.
- Now Click on Hosts to see the servers that are being monitored.
Step 9: Adding Hosts to Nagios Server For Monitoring
In order to add hosts to our Nagios server, we have to do the following steps:
Create Nagios Host Object Definition
- 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
- 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
}
Create Host and Service Template Configuration
- 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
Create Contacts and Contact Group
We defined our contact group as server-admins
. Hence, we need to create that contact group.
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
}
Verify Nagios Configuration
Run the following command to check if the configuration file is okay.
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
You should get the output below
- Restart Nagios
sudo systemctl reload nagios
Now visit Nagios web interface and verify that your new server has been added for monitoring: http://nagios.your-domain.com
Click on Hosts on the left pane and you should see the localhost and your remote hosts.
Conclusion
We have successfully installed Nagios Core on Ubuntu 20.04 LTS using nginx as a webserver and added hosts for monitoring.