Install and configure Nagios on Ubuntu 18.04 Bionic Beaver
A working install of Ubuntu 18.04 with root privileges
# - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user
What is Nagios
Nagios is one of the best enterprise grade server monitoring solutions available. Unlike cPanel, it's available across a wide range of Linux distributions, including Debian and Ubuntu.
Since Ubuntu 18.04 is an LTS release, upgrading your servers and running Nagios on them could be a great idea. Even though Nagios isn't up to date in the Ubuntu repositories, and setup isn't turn-key, it's still not too hard to get it running.
Install The Requirements
Before you can install Nagios on your Ubuntu server, you're going to need to install the build dependencies. Yes, that means that you're going to be building Nagios from source, but the process isn't too complex. Begin by installing the packages you need.
SUBSCRIBE TO NEWSLETTER Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.
Create A Nagios User
It's best for security reasons to create a user and group specifically to run Nagios. Start by making both the new user and the new group.
$ sudo useradd nagios
$ sudo groupadd nagcmd
Add your nagios user to the nagcmd group.
$ sudo usermod -a -G nagcmd nagios
Next, you're going to need to download and build the Nagios source. Go the the Nagios Core download page, and download the latest stable release of Nagios 4.
Change in the directory where you downloaded Nagios. Unpack it with tar.
$ tar xpf nagios-*.tar.gz
Now, change into the resulting directory.
$ cd nagios-4.3.4
Prepare the Nagios source. Configure it to use the user and group that you created.
That will only take a couple of seconds. Review the output. Make sure that nothing looks ridiculously out of place.
Build everything. Don't forget to use the -j flag with the number of cores on your machine to speed up the process.
$ make -j4 all
It should be a fairly fast compile on modern hardware. When it's finished, you can install all of the Nagios components that you just compiled.
$ sudo make install
$ sudo make install-commandmode
$ sudo make install-init
$ sudo make install-config
Before you leave the folder, you need to copy the provided Apache configuration over to Apache's sites-available folder.
Nagios has loads of great plugins that enhance its functionality. It's best to get those and install them now too. Download them from their download page.
Change into the directory where you downloaded them, and unpack them.
$ tar xpf nagios-plugins-*.tar.gz
Change into the resulting directory.
$ cd nagios-plugins-2.2.1
Configure them like you did Nagios, but include OpenSSL support too.
When the configuration finishes, make and install the plugins.
$ make -j4
$ sudo make install
Before you can start using Nagios, you're going to need to tweak the base configurations that you already installed.
Start by opening up the main configuration file. It's located at /usr/local/nagios/etc/nagios.cfg
Find the line below, and uncomment it. When you're gone, save and exit.
Now, make that directory.
$ sudo mkdir /usr/local/nagios/etc/servers
Open up the contacts configuration. You should change it to refelct your actual email address. The file is at /usr/local/nagios/etc/objects/contacts.cfg. Change the line below to match your email.
email nagios@localhost; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
In order for Apache to serve the Nagios interface, you need to enable a couple of Apache modules. Begin with that.
$ sudo a2enmod rewrite
$ sudo a2enmod cgi
Now, create an admin password for a Nagios admin user. After you enter the command, you'll be prompted to create you password.
Its a good idea to restrict access to your Nagios server. It's best to only allow access from localhost and your own computer. Open you Apache Nagios configuration file, and find the line Order allow,deny. Make it look like the example below.
Deny from all
Allow from 127.0.0.1 172.86.186.XXX
Obviously, substitute your actual external IP for the final IP. The XXX part is just there to obscure the example. Change this for both entries. Save it and exit.
Start up Apache.
$ sudo systemctl start apache2
Create the Service
Nagios doesn't automatically come with a Systemd service file. You need to make it at /etc/systemd/system/nagios.service. Copy the one below.
Nagios is now available at http://your_ip/nagios. When you arrive, you'll be prompted to enter the password that you created for nagiosadmin. From there, you'll be able to browse the Nagios admin interface.
Your Nagios installation is now up and running. That Ubuntu server will be able to monitor itself, and you can see the data presented in real time through the web interface.