2026年11月13日

2026年11月13日

Nagios Core Uptime Monitoring and Email Alerts on Ubuntu

This article is a localized edition of the Japanese original. Read the Japanese original

Nagios Core is monitoring software that checks servers and services at regular intervals and sends notifications when something stops responding or looks wrong. This article shows how to install Nagios Core on an Ubuntu server, monitor a web server running WordPress for availability (ping), HTTPS responses and certificate expiry, and receive outage alerts by email.

The steps follow Nagios's official installation guide and the sample configuration included in the Nagios Core source code. As of November 2026, the latest versions are Nagios Core 4.5.14 and Nagios Plugins 2.5.

Run monitoring on a separate server from the web server being monitored. If you monitor from the same server, notifications stop when that server goes down.

Install Nagios Core (Ubuntu 22.04/24.04)

Required packages

sudo apt-get update
sudo apt-get install -y autoconf gcc libc6 make wget unzip apache2 php libapache2-mod-php libgd-dev openssl libssl-dev

Because notifications go out by email, install the mail command first as well. Nagios Core's configure writes the path of the mail command it finds at that point into the notification settings.

sudo apt-get install -y mailutils

Installing mailutils may also install an MTA for sending mail (usually Postfix). If the Postfix configuration screen appears, choose the option that fits your environment.

Download and install from source

cd /tmp
wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.5.14/nagios-4.5.14.tar.gz
tar xzf nagioscore.tar.gz
cd /tmp/nagios-4.5.14

sudo ./configure --with-httpd-conf=/etc/apache2/sites-enabled
sudo make all

In the summary that configure prints at the end, check that Mail program: shows something like /usr/bin/mail.

# Create the nagios user and group, and add the Apache user to the nagios group
sudo make install-groups-users
sudo usermod -a -G nagios www-data

# Install the binaries, service startup files, external command directory and sample config
sudo make install
sudo make install-daemoninit
sudo make install-commandmode
sudo make install-config

# Install the Apache config for the web interface
sudo make install-webconf
sudo a2enmod rewrite
sudo a2enmod cgi

If a newer version is out, replace the version number in the URL and directory name with the latest one from the Nagios Core releases page.

Create the web interface login account

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

-c creates a new file. If you use it when adding a second or later user, the existing accounts are wiped out.

Install Nagios Plugins

The actual checks (ping, HTTP and so on) are performed by the individual Nagios Plugins commands.

sudo apt-get install -y autoconf gcc libc6 libmcrypt-dev make libssl-dev wget bc gawk dc build-essential snmp libnet-snmp-perl gettext iputils-ping

cd /tmp
wget -O nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/releases/download/release-2.5/nagios-plugins-2.5.tar.gz
tar zxf nagios-plugins.tar.gz
cd /tmp/nagios-plugins-2.5

sudo ./configure
sudo make
sudo make install

The plugins are installed in /usr/local/nagios/libexec/.

Start the services

sudo systemctl restart apache2
sudo systemctl enable --now nagios

If you use a firewall (ufw), allow connections to Apache.

sudo ufw allow Apache
sudo ufw reload

Open http://MONITORING_SERVER_IP/nagios in a browser and log in as nagiosadmin with the password you set. Monitoring of the monitoring server itself (localhost) is configured out of the box.

The web interface is protected by Basic authentication, but over plain HTTP the password is sent unencrypted. If you expose it to the internet, switch to HTTPS or restrict which IPs can connect.

Where the configuration files are

With a source install, the configuration lives in /usr/local/nagios/etc/.

File Contents
nagios.cfg Main configuration. Specifies the object definition files to load with cfg_file and cfg_dir
objects/contacts.cfg Notification recipients (contacts) and contact groups
objects/templates.cfg Templates for hosts and services (linux-server, generic-service and others)
objects/commands.cfg Definitions of check and notification commands
objects/localhost.cfg Monitoring settings for the monitoring server itself

Set the notification email address

In /usr/local/nagios/etc/objects/contacts.cfg, change the email of nagiosadmin to the address that should receive notifications.

define contact {
    contact_name            nagiosadmin
    use                     generic-contact
    alias                   Nagios Admin
    email                   you@example.com
}

The contact group admins in the same file contains nagiosadmin. The sample templates (linux-server, generic-service) send notifications to admins.

Notification emails are sent by notify-host-by-email and notify-service-by-email in commands.cfg. They call the mail command, so confirm first that mail can send email.

echo "test" | mail -s "Nagios mail test" you@example.com

If it doesn't arrive, check your MTA settings. On cloud servers, outbound port 25 is often blocked, and you may need to relay through an external SMTP server (see "Relaying Postfix through an external SMTP server").

Add the web server to monitor

Keep the settings for monitored servers in their own files. Create /usr/local/nagios/etc/servers/ and have nagios.cfg load that directory.

sudo mkdir -p /usr/local/nagios/etc/servers
sudo chown nagios:nagios /usr/local/nagios/etc/servers

In /usr/local/nagios/etc/nagios.cfg, remove the # at the start of the following line (add the line if it isn't there).

cfg_dir=/usr/local/nagios/etc/servers

Create /usr/local/nagios/etc/servers/web01.cfg and define the host and services. Replace address and the domain after -H with those of the server you want to monitor.

define host {
    use                     linux-server
    host_name               web01
    alias                   WordPress Web server
    address                 203.0.113.20
    notification_period     24x7
}

define service {
    use                     generic-service
    host_name               web01
    service_description     HTTPS
    check_command           check_http!-H www.example.com -S --sni -u /
}

define service {
    use                     generic-service
    host_name               web01
    service_description     SSL certificate
    check_command           check_http!-H www.example.com -S --sni -C 21,7
    check_interval          720
}
  • Host availability is checked by check-host-alive (check_ping) from the linux-server template. A server that doesn't answer ping (because ICMP is blocked) will show as "DOWN". In that case, use a different check as the host's check_command.
  • In the sample config, the notification period (notification_period) of linux-server is workhours (weekday daytime). To get notifications at night and on weekends too, the example above overrides it with 24x7.
  • For check_http, -S connects over HTTPS, --sni sends the host name via SNI, and -u is the path to check. A 2xx or 3xx response is OK, 4xx is a warning, and 5xx or no response is critical.
  • -C 21,7 raises a warning when the certificate has fewer than 21 days left and a critical state at fewer than 7. With -C, only the certificate is checked, not the page content, so it's a separate service from the HTTPS response check. Certificate expiry doesn't change often, so check_interval is set to 720 minutes (12 hours).

To check disk space or load on the monitored server, you need an agent such as NRPE installed on it. That isn't covered in this article.

Verify and apply the configuration

After changing the configuration, verify it before applying.

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

If it ends with Total Warnings: 0 and Total Errors: 0, you're fine. If there are errors, it shows hints such as the file name and line. Once verification passes, restart.

sudo systemctl restart nagios

Test that notifications arrive

Open a service under "Services" in the web interface and use "Send custom service notification" to send a notification even when nothing is wrong. Confirm that the email arrives.

To also test real outage detection, stop the web server on a test server (for example sudo systemctl stop nginx). With the sample generic-service settings, Nagios checks every 10 minutes, re-checks every 2 minutes once it sees a problem, and notifies after 3 consecutive failures. So it takes anywhere from a few minutes to over ten minutes for the notification to arrive after you stop it. Afterwards, start the web server again and confirm that the recovery notification (RECOVERY) arrives too.

Troubleshooting

  • Status pages and similar screens in the web interface won't open: check that you ran sudo a2enmod cgi and restarted Apache.
  • Every service shows "(No output on stdout)" or UNKNOWN: Nagios Plugins may not be installed. Check that the plugins exist with ls /usr/local/nagios/libexec/.
  • Emails don't arrive: check whether you can send directly with the mail command, and if not, look at the MTA log (for Postfix, sudo journalctl -u postfix or /var/log/mail.log). Also check that the notification period (notification_period) isn't still workhours.

Cautions

  • Put the monitoring server somewhere other than the servers it monitors. If it shares a server or network with them, monitoring and notifications fail along with them.
  • Use a hard-to-guess password for the web interface account, and if you expose it, set up HTTPS and restrict the allowed source addresses.
  • Very short check intervals add load and log volume on the monitored side. Adjust them to the size of your site.

Summary

  • Install Nagios Core and Nagios Plugins from source following the official guide. Install the mail command used for notifications before running configure
  • Set the email of nagiosadmin in contacts.cfg to the notification address
  • Keep monitored servers in a directory loaded with cfg_dir, and use check_http to check HTTPS responses and certificate expiry
  • The sample linux-server template only notifies during the day, so set notification_period to 24x7 if needed
  • Verify the configuration with nagios -v before restarting, and confirm with a test notification that alerts arrive

An example of monitoring a WordPress site with Nagios is in "Monitoring a WordPress site with Nagios", and how to write your own check commands is in "Writing a custom Nagios plugin".

Questions about WordPress? The publisher of this site is based in Japan and replies in Japanese. Contact (Japanese)

お気軽にご相談ください

お見積りへ お問い合わせへ
▲