How to install Nagios on Linux
How to Install Nagios on Linux: A Complete Step-by-Step Guide
Nagios is one of the most powerful and widely-used open-source network monitoring solutions available today. This comprehensive guide will walk you through the complete process of installing and configuring Nagios Core on a Linux system, from initial setup to advanced configuration options.
What You'll Learn
By the end of this tutorial, you'll have:
- A fully functional Nagios Core installation
- Understanding of Nagios architecture and components
- Knowledge of basic configuration and monitoring setup
- Troubleshooting skills for common installation issues
- Best practices for maintaining your Nagios installation
Table of Contents
1. [Prerequisites and System Requirements](#prerequisites-and-system-requirements)
2. [Understanding Nagios Architecture](#understanding-nagios-architecture)
3. [Installing Dependencies](#installing-dependencies)
4. [Creating Nagios User and Group](#creating-nagios-user-and-group)
5. [Downloading and Compiling Nagios Core](#downloading-and-compiling-nagios-core)
6. [Installing Nagios Plugins](#installing-nagios-plugins)
7. [Configuring Apache Web Server](#configuring-apache-web-server)
8. [Initial Nagios Configuration](#initial-nagios-configuration)
9. [Starting Nagios Services](#starting-nagios-services)
10. [Web Interface Access](#web-interface-access)
11. [Common Issues and Troubleshooting](#common-issues-and-troubleshooting)
12. [Best Practices and Security](#best-practices-and-security)
13. [Next Steps](#next-steps)
Prerequisites and System Requirements
Before beginning the Nagios installation process, ensure your system meets the following requirements:
System Requirements
- Operating System: Linux distribution (Ubuntu 18.04+, CentOS 7+, RHEL 7+, Debian 9+)
- RAM: Minimum 1GB (2GB recommended for production)
- Disk Space: At least 2GB free space
- CPU: 1 core minimum (2+ cores recommended)
- Network: Stable internet connection for downloading packages
User Privileges
You'll need:
- Root access or sudo privileges
- Basic command-line knowledge
- Understanding of Linux file permissions
Supported Distributions
This guide covers installation on:
- Ubuntu/Debian-based systems
- CentOS/RHEL-based systems
- Generic Linux distributions
Understanding Nagios Architecture
Before diving into installation, it's crucial to understand Nagios's core components:
Core Components
1. Nagios Core Engine: The main monitoring daemon that schedules checks and processes results
2. Nagios Plugins: Executable scripts that perform actual monitoring checks
3. Web Interface: PHP-based web frontend for viewing monitoring data
4. Configuration Files: Text-based configuration defining what to monitor
How Nagios Works
```
[Nagios Core] → [Plugins] → [Remote Hosts/Services]
↓
[Results Processing] → [Notifications] → [Web Interface]
```
Installing Dependencies
The installation process varies slightly depending on your Linux distribution. Follow the appropriate section for your system.
Ubuntu/Debian Systems
First, update your package repository:
```bash
sudo apt update && sudo apt upgrade -y
```
Install required dependencies:
```bash
sudo apt install -y autoconf gcc libc6 make wget unzip apache2 apache2-utils php libgd-dev
```
Install build dependencies:
```bash
sudo apt install -y build-essential libgd-dev openssl libssl-dev xinetd apache2-dev
```
CentOS/RHEL Systems
Update your system:
```bash
sudo yum update -y
For CentOS 8/RHEL 8, use: sudo dnf update -y
```
Install dependencies:
```bash
sudo yum install -y gcc glibc glibc-common wget unzip httpd php gd gd-devel perl postfix
For CentOS 8/RHEL 8, use: sudo dnf install -y gcc glibc glibc-common wget unzip httpd php gd gd-devel perl postfix
```
Install development tools:
```bash
sudo yum groupinstall -y "Development Tools"
For CentOS 8/RHEL 8, use: sudo dnf groupinstall -y "Development Tools"
```
Creating Nagios User and Group
Create a dedicated user account for Nagios:
```bash
sudo useradd nagios
sudo groupadd nagcmd
```
Add the nagios user to the nagcmd group:
```bash
sudo usermod -a -G nagcmd nagios
```
Add the web server user to the nagcmd group:
```bash
For Ubuntu/Debian (Apache user is www-data)
sudo usermod -a -G nagcmd www-data
For CentOS/RHEL (Apache user is apache)
sudo usermod -a -G nagcmd apache
```
Downloading and Compiling Nagios Core
Download Nagios Core
Navigate to a temporary directory and download the latest Nagios Core:
```bash
cd /tmp
wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.14.tar.gz
tar xzf nagioscore.tar.gz
```
Compile and Install Nagios Core
Navigate to the extracted directory:
```bash
cd /tmp/nagioscore-nagios-4.4.14/
```
Configure the build:
```bash
sudo ./configure --with-httpd-conf=/etc/apache2/sites-enabled
```
Note: For CentOS/RHEL, use:
```bash
sudo ./configure --with-httpd-conf=/etc/httpd/conf.d
```
Compile Nagios:
```bash
sudo make all
```
Install binaries, scripts, and configuration files:
```bash
sudo make install
```
Install service/daemon files:
```bash
sudo make install-daemoninit
```
Install command mode:
```bash
sudo make install-commandmode
```
Install configuration files:
```bash
sudo make install-config
```
Install Apache configuration files:
```bash
sudo make install-webconf
```
Configure Apache Authentication
Create a nagiosadmin user for web interface access:
```bash
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
```
You'll be prompted to enter and confirm a password for the nagiosadmin user.
Installing Nagios Plugins
Nagios plugins are essential for monitoring functionality. Download and install them:
```bash
cd /tmp
wget --no-check-certificate -O nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/archive/release-2.4.6.tar.gz
tar zxf nagios-plugins.tar.gz
```
Navigate to the plugins directory:
```bash
cd /tmp/nagios-plugins-release-2.4.6/
```
Configure and compile plugins:
```bash
sudo ./tools/setup
sudo ./configure
sudo make
sudo make install
```
Configuring Apache Web Server
Enable Apache Modules
Enable required Apache modules:
```bash
Ubuntu/Debian
sudo a2enmod rewrite
sudo a2enmod cgi
CentOS/RHEL - modules are typically enabled by default
```
Start and Enable Apache
```bash
Ubuntu/Debian
sudo systemctl restart apache2
sudo systemctl enable apache2
CentOS/RHEL
sudo systemctl restart httpd
sudo systemctl enable httpd
```
Configure Firewall (if applicable)
```bash
Ubuntu/Debian (UFW)
sudo ufw allow Apache
sudo ufw allow 'Apache Full'
CentOS/RHEL (firewalld)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
```
Initial Nagios Configuration
Verify Configuration Syntax
Before starting Nagios, verify the configuration:
```bash
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
```
You should see output ending with:
```
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
```
Configure Sample Monitoring
Edit the main configuration file:
```bash
sudo nano /usr/local/nagios/etc/nagios.cfg
```
Ensure the following line is uncommented to enable sample configuration:
```
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
```
Set Proper Permissions
Set correct ownership and permissions:
```bash
sudo chown -R nagios:nagios /usr/local/nagios/
sudo chown -R nagios:nagcmd /usr/local/nagios/var/rw/
```
Starting Nagios Services
Start Nagios Service
```bash
sudo systemctl start nagios
sudo systemctl enable nagios
```
Verify Service Status
Check if Nagios is running properly:
```bash
sudo systemctl status nagios
```
You should see output indicating the service is active and running.
Check Nagios Logs
Monitor the Nagios log file for any issues:
```bash
sudo tail -f /usr/local/nagios/var/nagios.log
```
Web Interface Access
Access the Nagios Web Interface
Open your web browser and navigate to:
```
http://your-server-ip/nagios
```
Login Credentials
- Username: nagiosadmin
- Password: The password you set earlier with htpasswd
Web Interface Overview
Once logged in, you'll see the main Nagios interface with:
- Tactical Overview: Summary of host and service status
- Host Detail: Information about monitored hosts
- Service Detail: Information about monitored services
- Monitoring: Real-time monitoring views
- Reporting: Historical data and reports
Common Issues and Troubleshooting
Issue 1: Apache Configuration Problems
Symptoms: Cannot access web interface, 404 errors
Solutions:
```bash
Check Apache error logs
sudo tail -f /var/log/apache2/error.log # Ubuntu/Debian
sudo tail -f /var/log/httpd/error_log # CentOS/RHEL
Restart Apache
sudo systemctl restart apache2 # Ubuntu/Debian
sudo systemctl restart httpd # CentOS/RHEL
Verify Apache configuration
sudo apache2ctl configtest # Ubuntu/Debian
sudo httpd -t # CentOS/RHEL
```
Issue 2: Nagios Service Won't Start
Symptoms: Service fails to start, configuration errors
Solutions:
```bash
Check Nagios configuration
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Check system logs
sudo journalctl -u nagios -f
Verify file permissions
sudo chown -R nagios:nagios /usr/local/nagios/
```
Issue 3: Plugin Execution Errors
Symptoms: Services show "UNKNOWN" status, plugin errors
Solutions:
```bash
Test plugin manually
sudo -u nagios /usr/local/nagios/libexec/check_http -H localhost
Check plugin permissions
sudo chmod +x /usr/local/nagios/libexec/check_*
Verify plugin path in configuration
grep command_line /usr/local/nagios/etc/objects/commands.cfg
```
Issue 4: SELinux Issues (CentOS/RHEL)
Symptoms: Permission denied errors, service failures
Solutions:
```bash
Check SELinux status
sestatus
Set SELinux to permissive mode temporarily
sudo setenforce 0
For permanent solution, configure SELinux policies
sudo setsebool -P httpd_can_network_connect 1
sudo setsebool -P nagios_run_sudo 1
```
Issue 5: Firewall Blocking Access
Symptoms: Cannot access web interface from remote hosts
Solutions:
```bash
Ubuntu/Debian (UFW)
sudo ufw status
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
CentOS/RHEL (firewalld)
sudo firewall-cmd --list-all
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
```
Best Practices and Security
Security Hardening
1. Change Default Passwords:
```bash
sudo htpasswd /usr/local/nagios/etc/htpasswd.users nagiosadmin
```
2. Use HTTPS:
Configure SSL certificates for Apache to encrypt web traffic.
3. Restrict Access:
Configure Apache to restrict access by IP address:
```apache
Require ip 192.168.1.0/24
Require ip 10.0.0.0/8
```
4. Regular Updates:
Keep Nagios Core, plugins, and system packages updated.
Performance Optimization
1. Adjust Check Intervals:
```cfg
In service definitions
normal_check_interval 5
retry_check_interval 1
```
2. Enable Performance Data:
```cfg
In nagios.cfg
process_performance_data=1
```
3. Optimize Configuration:
- Use host and service templates
- Group related configurations
- Regular configuration cleanup
Backup Strategy
Create regular backups of:
- Configuration files: `/usr/local/nagios/etc/`
- Historical data: `/usr/local/nagios/var/`
- Custom plugins: `/usr/local/nagios/libexec/`
Example backup script:
```bash
#!/bin/bash
BACKUP_DIR="/backup/nagios-$(date +%Y%m%d)"
mkdir -p $BACKUP_DIR
cp -r /usr/local/nagios/etc/ $BACKUP_DIR/
cp -r /usr/local/nagios/var/ $BACKUP_DIR/
tar -czf $BACKUP_DIR.tar.gz $BACKUP_DIR/
rm -rf $BACKUP_DIR/
```
Monitoring Best Practices
1. Start Simple: Begin with basic host and service monitoring
2. Use Templates: Create reusable host and service templates
3. Implement Escalations: Configure notification escalations for critical issues
4. Regular Maintenance: Schedule regular maintenance windows
5. Documentation: Document your monitoring setup and procedures
Advanced Configuration Examples
Adding a Remote Host
Create a new host configuration file:
```bash
sudo nano /usr/local/nagios/etc/objects/remote-host.cfg
```
Add host definition:
```cfg
define host {
use linux-server
host_name remote-server
alias Remote Linux Server
address 192.168.1.100
max_check_attempts 5
check_period 24x7
notification_interval 30
notification_period 24x7
}
define service {
use generic-service
host_name remote-server
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service {
use generic-service
host_name remote-server
service_description SSH
check_command check_ssh
}
```
Update main configuration to include the new file:
```bash
sudo nano /usr/local/nagios/etc/nagios.cfg
```
Add:
```cfg
cfg_file=/usr/local/nagios/etc/objects/remote-host.cfg
```
Custom Command Definition
Add custom commands in commands.cfg:
```cfg
define command {
command_name check_disk_space
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
}
```
Next Steps
After successfully installing Nagios, consider these next steps:
Immediate Actions
1. Configure Additional Hosts: Add more servers to monitoring
2. Set Up Notifications: Configure email/SMS alerts
3. Create Custom Checks: Develop application-specific monitoring
4. Implement Security: Harden the installation and enable HTTPS
Advanced Features
1. NRPE Installation: Enable remote plugin execution
2. SNMP Monitoring: Monitor network devices
3. Database Integration: Store historical data in databases
4. Mobile Access: Configure mobile-friendly interfaces
Learning Resources
- Official Nagios Documentation
- Community forums and support
- Third-party plugins and addons
- Monitoring best practices guides
Conclusion
You've successfully installed and configured Nagios Core on your Linux system. This powerful monitoring solution will help you maintain visibility into your infrastructure's health and performance. The installation provides a solid foundation that you can expand with additional hosts, services, and advanced monitoring capabilities.
Remember to:
- Regularly update your Nagios installation
- Monitor the monitoring system itself
- Backup your configuration files
- Stay engaged with the Nagios community for tips and updates
With Nagios properly installed and configured, you're well-equipped to maintain a robust monitoring infrastructure that will help prevent downtime and ensure optimal system performance.
Start with basic monitoring and gradually expand your setup as you become more comfortable with Nagios's capabilities. The investment in proper monitoring will pay dividends in system reliability and peace of mind.