How To Install Nagios On CentOS 7

How To Install Nagios On CentOS 7

Reading time1 min
#Monitoring#Linux#OpenSource#Nagios#CentOS#SysAdmin

Step-by-step Guide: Installing Nagios on CentOS 7 for Robust System Monitoring

When the first 2 a.m. outage alert arrives, an on-premises monitoring stack's configuration quickly becomes critical instead of theoretical. Nagios, venerable yet extensible, remains a go-to solution for many data center and virtualized fleet operators. On CentOS 7, Nagios can be installed cleanly from source if you respect its compilation and runtime requirements. Follow these steps for a baseline setup; extensions and tuning can come later.


Requirements

  • CentOS 7 x86_64, fully patched (yum update required)
  • Non-root user with sudo
  • Familiarity with systemd, Apache httpd, and basic compilation
  • Approx. 300MB free disk (logs add up in production)

1. Patch and Prepare

Outdated dependencies introduce risk—patch first.

sudo yum update -y
sudo yum install -y gcc make httpd php wget unzip vim perl-Net-SNMP \
  gd gd-devel fontconfig-devel freetype-devel

If you deploy Nagios on a minimal VM, verify perl-Net-SNMP installs cleanly; EPEL repo sometimes required:

sudo yum install -y epel-release
sudo yum install -y perl-Net-SNMP

Enable and start Apache:

sudo systemctl enable httpd
sudo systemctl start httpd

Firewall specifics—without opening TCP/80, the web interface is unreachable:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

2. Dedicated User/Group Creation

Nagios should never run as root for obvious reasons. Create its runtime user and the nagcmd group (for plugin command delegation):

sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd apache

Note: Add additional plugin users to this group if customizing execution in the future.


3. Acquire and Unpack Source (Nagios Core 4.4.8)

Downloads can break via firewall proxies. If so, fallback to curl:

cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.8.tar.gz
tar xf nagios-4.4.8.tar.gz
cd nagios-4.4.8

4. Build and Install

Configuring with command group is essential for check result forwarding.

./configure --with-command-group=nagcmd
make all
sudo make install
sudo make install-commandmode
sudo make install-config
sudo make install-init
sudo make install-webconf

Gotcha: On dirty or previously failed builds, make clean before re-running.


5. Apache: Adjust for CGI

Nagios still uses CGI for its web UI, so correct module load is non-optional.

sudo sed -i '/^#LoadModule cgi_module/s/^#//' /etc/httpd/conf.modules.d/00-mpm.conf

If /etc/httpd/conf.modules.d/00-cgi.conf exists, ensure it contains:

LoadModule cgi_module modules/mod_cgi.so

Create the admin account—do not lose this password; resetting requires shell access:

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

Restart Apache:

sudo systemctl restart httpd

Known issue: SELinux in enforcing mode can silently block CGI. Add an exception or run:

setsebool -P httpd_execmem 1

(Consider reverting after testing.)


6. Install Nagios Plugins (2.3.3 Recommended)

Without plugins, Nagios only starts; it performs no meaningful checks.

cd /tmp
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
tar xf nagios-plugins-2.3.3.tar.gz
cd nagios-plugins-2.3.3
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd
make
sudo make install

Plugins install to /usr/local/nagios/libexec.


7. Enable and Start Nagios

sudo systemctl enable nagios
sudo systemctl start nagios

After starting, check status:

systemctl status nagios

If debugging a failed start, inspect /usr/local/nagios/var/nagios.log.


8. Web UI Access

Default URL:

http://<server_ip>/nagios/

Use nagiosadmin and the password set earlier.

If the page loads blank or malformed, verify:

  • Apache CGI enabled
  • mod_rewrite not interfering
  • File permissions on /usr/local/nagios/

Next Steps & Operational Guidance

  • To monitor remote servers: deploy NRPE or agentless checks via SSH.
  • Expand configuration by adding .cfg files under /usr/local/nagios/etc/objects/.
  • Apply changes:
    sudo systemctl restart nagios
    
  • Mail alerts: configure contacts.cfg and test with sendmail or mailx.

Example: Adding a new host for basic ping check:

define host {
  use             linux-server
  host_name       web01
  alias           Web Frontend Node
  address         10.0.1.10
}
define service {
  use                     generic-service
  host_name               web01
  service_description     PING
  check_command           check_ping!100.0,20%!500.0,60%
}

Check Nagios config syntax before restarting:

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

Practical Note

  • SSL is strongly recommended for the web UI. Use mod_ssl and update the Nagios Apache conf block to redirect HTTP to HTTPS.
  • Limit access to the /nagios URI with firewall rules or Apache .htaccess as appropriate.
  • If you see “Error: Could not open command file”, double-check nagcmd group permissions on /usr/local/nagios/var/rw—it’s a common first-timer mistake.

Summary Table

ComponentVersionPath
Nagios Core4.4.8/usr/local/nagios/
Nagios Plugins2.3.3/usr/local/nagios/libexec/
Config FilesN/A/usr/local/nagios/etc/
Apache UserapacheN/A

Closing

A streamlined Nagios install on CentOS 7 allows for direct monitoring of key infrastructure, with minimal resource overhead and few moving parts. Enterprise environments may need additional integrations—LDAP authentication, distributed checks—but the above suffices for small to medium estate monitoring. For automated deployments, consider building a simple shell script using these exact commands, or encapsulate the process in a configuration management system (e.g., Ansible, Puppet).

For edge cases and performance tuning, consult the Nagios Core forums or real-world operations threads—default settings often benefit from tailoring.