Set up Nagios Core 4.5 monitoring system with Apache web interface to monitor servers, network devices, and services. Configure hosts, services, and email notifications for comprehensive infrastructure monitoring.
Prerequisites
- Root or sudo access
- 2GB+ RAM recommended
- Basic Linux command line knowledge
- Email server access for notifications
What this solves
Nagios Core provides comprehensive monitoring for your infrastructure by tracking servers, network devices, and services. This tutorial sets up Nagios with a web interface for viewing status dashboards, configuring monitoring checks, and receiving alerts when systems go down or performance degrades.
Step-by-step installation
Update system packages
Update your package manager to ensure you get the latest versions and security patches.
sudo apt update && sudo apt upgrade -y
Install required dependencies
Install Apache web server, PHP, development tools, and libraries needed to compile Nagios Core from source.
sudo apt install -y apache2 apache2-utils php php-gd libgd-dev
sudo apt install -y build-essential unzip wget openssl libssl-dev
Create Nagios user and group
Create dedicated system accounts for Nagios to run securely without root privileges.
sudo useradd -r -s /bin/false nagios
sudo usermod -aG nagios www-data
Download and compile Nagios Core
Download Nagios Core 4.5 source code and compile it with the necessary configuration options.
cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.5.5.tar.gz
tar -xzf nagios-4.5.5.tar.gz
cd nagios-4.5.5
Configure the build with proper paths and user settings.
./configure --with-httpd-conf=/etc/apache2/sites-enabled \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--with-command-user=nagios \
--with-command-group=nagios
Compile and install Nagios Core.
make all
sudo make install
sudo make install-daemoninit
sudo make install-commandmode
sudo make install-config
sudo make install-webconf
Download and install Nagios plugins
Install monitoring plugins that provide the actual check commands for services like HTTP, SSH, disk usage, and load average.
cd /tmp
wget https://github.com/nagios-plugins/nagios-plugins/releases/download/release-2.4.10/nagios-plugins-2.4.10.tar.gz
tar -xzf nagios-plugins-2.4.10.tar.gz
cd nagios-plugins-2.4.10
Configure and compile the plugins.
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
sudo make install
Configure Apache web server
Enable required Apache modules and configure authentication for the Nagios web interface.
sudo a2enmod rewrite cgi
sudo systemctl restart apache2
Create Nagios admin user
Set up HTTP basic authentication for accessing the Nagios web interface.
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Enter a strong password when prompted. This account will have full administrative access to Nagios.
Configure file permissions
Set proper ownership and permissions for Nagios files and directories.
sudo chown -R nagios:nagios /usr/local/nagios/
sudo chmod 755 /usr/local/nagios/var/rw
sudo chmod 644 /usr/local/nagios/etc/*.cfg
Configure Nagios main configuration
Edit the main Nagios configuration file to enable the web interface and set basic monitoring parameters.
# Enable web interface
check_external_commands=1
command_check_interval=10s
command_file=/usr/local/nagios/var/rw/nagios.cmd
Log settings
log_file=/usr/local/nagios/var/nagios.log
log_initial_states=1
log_current_states=1
Object configuration files
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
Configure email notifications
Set up email alerts by editing the contacts configuration file.
define contact {
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email admin@example.com
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
}
define contactgroup {
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}
Create host monitoring configuration
Configure monitoring for additional servers by creating a hosts configuration file.
define host {
use linux-server
host_name web-server-01
alias Web Server 01
address 203.0.113.10
max_check_attempts 3
check_period 24x7
notification_interval 30
notification_period 24x7
contact_groups admins
}
define service {
use generic-service
host_name web-server-01
service_description HTTP
check_command check_http
max_check_attempts 3
normal_check_interval 5
retry_check_interval 2
contact_groups admins
}
define service {
use generic-service
host_name web-server-01
service_description SSH
check_command check_ssh
max_check_attempts 3
normal_check_interval 5
retry_check_interval 2
contact_groups admins
}
Add this file to the main configuration by editing nagios.cfg.
sudo bash -c 'echo "cfg_file=/usr/local/nagios/etc/objects/servers.cfg" >> /usr/local/nagios/etc/nagios.cfg'
Install and configure email system
Install a mail transfer agent to send notification emails.
sudo apt install -y mailutils postfix
During postfix installation, select "Internet Site" and enter your domain name.
Start and enable Nagios services
Enable Nagios to start automatically on boot and start the monitoring service.
sudo systemctl enable nagios
sudo systemctl start nagios
sudo systemctl enable apache2
sudo systemctl restart apache2
For Red Hat systems, use httpd instead of apache2.
Configure firewall access
Allow HTTP traffic through firewall
Configure your firewall to allow web traffic to the Nagios interface.
sudo ufw allow 'Apache Full'
sudo ufw reload
Verify your setup
Check that Nagios is running correctly and accessible through the web interface.
sudo systemctl status nagios
sudo systemctl status apache2
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Open your web browser and navigate to http://your-server-ip/nagios. Log in with the nagiosadmin username and password you created earlier. You should see the Nagios Core dashboard with monitoring status for localhost and any additional hosts you configured.
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| Web interface shows "Forbidden" | Apache authentication not configured | Verify htpasswd.users file exists and Apache config is correct |
| "Configuration Error" in web interface | Syntax error in configuration files | Run sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg |
| Services show "PENDING" status | Nagios hasn't run checks yet | Wait 5-10 minutes or restart nagios service |
| Email notifications not working | Mail system not configured | Test with echo "test" | mail -s "Test" admin@example.com |
| Permission denied errors in logs | Incorrect file ownership | Run sudo chown -R nagios:nagios /usr/local/nagios/ |
Next steps
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Default variables
NAGIOS_VERSION="4.5.5"
PLUGINS_VERSION="2.4.10"
NAGIOS_USER="nagios"
ADMIN_USER="nagiosadmin"
# Usage function
usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -h, --help Show this help message"
echo " -p, --password Set admin password (will prompt if not provided)"
exit 1
}
# Cleanup function
cleanup() {
echo -e "${RED}[ERROR] Installation failed. Cleaning up...${NC}"
systemctl stop nagios 2>/dev/null || true
rm -rf /tmp/nagios-* /tmp/nagios-plugins-* 2>/dev/null || true
exit 1
}
trap cleanup ERR
# Parse arguments
ADMIN_PASSWORD=""
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
-p|--password)
ADMIN_PASSWORD="$2"
shift 2
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
usage
;;
esac
done
# Check if running as root or with sudo
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}This script must be run as root or with sudo${NC}"
exit 1
fi
# Auto-detect distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_INSTALL="apt install -y"
PKG_UPDATE="apt update && apt upgrade -y"
WEB_USER="www-data"
WEB_SERVICE="apache2"
APACHE_CONF_DIR="/etc/apache2/sites-enabled"
;;
almalinux|rocky|centos|rhel|ol|fedora)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
PKG_UPDATE="dnf update -y"
WEB_USER="apache"
WEB_SERVICE="httpd"
APACHE_CONF_DIR="/etc/httpd/conf.d"
;;
amzn)
PKG_MGR="yum"
PKG_INSTALL="yum install -y"
PKG_UPDATE="yum update -y"
WEB_USER="apache"
WEB_SERVICE="httpd"
APACHE_CONF_DIR="/etc/httpd/conf.d"
;;
*)
echo -e "${RED}Unsupported distribution: $ID${NC}"
exit 1
;;
esac
else
echo -e "${RED}Cannot detect distribution${NC}"
exit 1
fi
echo -e "${GREEN}Installing Nagios Core 4.5 on $PRETTY_NAME${NC}"
# Step 1: Update system packages
echo -e "${YELLOW}[1/10] Updating system packages...${NC}"
$PKG_UPDATE
# Step 2: Install dependencies
echo -e "${YELLOW}[2/10] Installing dependencies...${NC}"
if [[ "$PKG_MGR" == "apt" ]]; then
$PKG_INSTALL apache2 apache2-utils php php-gd libgd-dev build-essential unzip wget openssl libssl-dev
else
$PKG_INSTALL httpd php php-gd gd-devel unzip wget openssl-devel
dnf groupinstall -y "Development Tools" 2>/dev/null || yum groupinstall -y "Development Tools"
fi
# Step 3: Create Nagios user and group
echo -e "${YELLOW}[3/10] Creating Nagios user and group...${NC}"
if ! id "$NAGIOS_USER" &>/dev/null; then
useradd -r -s /bin/false "$NAGIOS_USER"
fi
usermod -aG "$NAGIOS_USER" "$WEB_USER"
# Step 4: Download and compile Nagios Core
echo -e "${YELLOW}[4/10] Downloading and compiling Nagios Core...${NC}"
cd /tmp
wget -q "https://assets.nagios.com/downloads/nagioscore/releases/nagios-${NAGIOS_VERSION}.tar.gz"
tar -xzf "nagios-${NAGIOS_VERSION}.tar.gz"
cd "nagios-${NAGIOS_VERSION}"
./configure --with-httpd-conf="$APACHE_CONF_DIR" \
--with-nagios-user="$NAGIOS_USER" \
--with-nagios-group="$NAGIOS_USER" \
--with-command-user="$NAGIOS_USER" \
--with-command-group="$NAGIOS_USER" >/dev/null
make all >/dev/null
make install >/dev/null
make install-daemoninit >/dev/null
make install-commandmode >/dev/null
make install-config >/dev/null
make install-webconf >/dev/null
# Step 5: Download and install Nagios plugins
echo -e "${YELLOW}[5/10] Installing Nagios plugins...${NC}"
cd /tmp
wget -q "https://github.com/nagios-plugins/nagios-plugins/releases/download/release-${PLUGINS_VERSION}/nagios-plugins-${PLUGINS_VERSION}.tar.gz"
tar -xzf "nagios-plugins-${PLUGINS_VERSION}.tar.gz"
cd "nagios-plugins-${PLUGINS_VERSION}"
./configure --with-nagios-user="$NAGIOS_USER" --with-nagios-group="$NAGIOS_USER" >/dev/null
make >/dev/null
make install >/dev/null
# Step 6: Configure Apache web server
echo -e "${YELLOW}[6/10] Configuring Apache web server...${NC}"
if [[ "$PKG_MGR" == "apt" ]]; then
a2enmod rewrite cgi >/dev/null
systemctl restart apache2
else
systemctl enable httpd
systemctl start httpd
# Configure SELinux for Nagios
if command -v setsebool &>/dev/null; then
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_enable_cgi 1
fi
fi
# Step 7: Create Nagios admin user
echo -e "${YELLOW}[7/10] Creating Nagios admin user...${NC}"
if [[ -z "$ADMIN_PASSWORD" ]]; then
echo "Enter password for Nagios admin user ($ADMIN_USER):"
read -s ADMIN_PASSWORD
fi
echo "$ADMIN_PASSWORD" | htpasswd -c -i /usr/local/nagios/etc/htpasswd.users "$ADMIN_USER"
# Step 8: Configure file permissions
echo -e "${YELLOW}[8/10] Setting file permissions...${NC}"
chown -R "$NAGIOS_USER":"$NAGIOS_USER" /usr/local/nagios/
chmod 755 /usr/local/nagios/var/rw
find /usr/local/nagios/etc -name "*.cfg" -exec chmod 644 {} \;
# Step 9: Configure Nagios main configuration
echo -e "${YELLOW}[9/10] Configuring Nagios...${NC}"
cat >> /usr/local/nagios/etc/nagios.cfg << 'EOF'
# Web interface settings
check_external_commands=1
command_check_interval=10s
# Log settings
log_initial_states=1
log_current_states=1
EOF
# Enable and start services
systemctl enable nagios
systemctl start nagios
systemctl enable "$WEB_SERVICE"
systemctl restart "$WEB_SERVICE"
# Configure firewall
if command -v ufw &>/dev/null && ufw status | grep -q "Status: active"; then
ufw allow 'Apache Full' >/dev/null 2>&1 || true
elif command -v firewall-cmd &>/dev/null; then
firewall-cmd --permanent --add-service=http >/dev/null 2>&1 || true
firewall-cmd --permanent --add-service=https >/dev/null 2>&1 || true
firewall-cmd --reload >/dev/null 2>&1 || true
fi
# Step 10: Verify installation
echo -e "${YELLOW}[10/10] Verifying installation...${NC}"
if systemctl is-active --quiet nagios && systemctl is-active --quiet "$WEB_SERVICE"; then
HOSTNAME=$(hostname -f 2>/dev/null || hostname)
echo -e "${GREEN}✓ Nagios Core installation completed successfully!${NC}"
echo -e "${GREEN}✓ Web interface available at: http://$HOSTNAME/nagios${NC}"
echo -e "${GREEN}✓ Login with username: $ADMIN_USER${NC}"
echo -e "${GREEN}✓ Services are running and enabled${NC}"
else
echo -e "${RED}✗ Installation completed but services may not be running properly${NC}"
exit 1
fi
# Cleanup temp files
rm -rf /tmp/nagios-* /tmp/nagios-plugins-*
echo -e "${YELLOW}Note: Configure your hosts and services in /usr/local/nagios/etc/objects/${NC}"
Review the script before running. Execute with: bash install.sh