ObjectiveInstall and configure Nagios on Ubuntu 18.04 Bionic Beaver
RequirementsA 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
- $ - requires given linux commands to be executed as a regular non-privileged user
What is NagiosNagios 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 RequirementsBefore 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.
$ sudo apt install build-essential libgd-dev openssl libssl-dev unzip apache2
Create A Nagios UserIt'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
nagiosuser to the
$ sudo usermod -a -G nagcmd nagios
Build NagiosNext, 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 xpf nagios-*.tar.gzNow, change into the resulting directory.
$ cd nagios-4.3.4Prepare the Nagios source. Configure it to use the user and group that you created.
$ ./configure --with-nagios-group=nagios --with-command-group=nagcmd
-jflag with the number of cores on your machine to speed up the process.
$ make -j4 allIt 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.
Before you leave the folder, you need to copy the provided Apache configuration over to Apache's
$ sudo make install $ sudo make install-commandmode $ sudo make install-init $ sudo make install-config
$ sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.confThen, add Apache's user to your
$ sudo usermod -a -G nagcmd www-data
Install The PluginsNagios 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.gzChange into the resulting directory.
$ cd nagios-plugins-2.2.1Configure them like you did Nagios, but include OpenSSL support too.
$ ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --with-opensslWhen the configuration finishes,
makeand install the plugins.
$ make -j4 $ sudo make install
Basic ConfigurationBefore 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.cfgFind the line below, and uncomment it. When you're gone, save and exit.
cfg_dir=/usr/local/nagios/etc/serversNow, make that directory.
$ sudo mkdir /usr/local/nagios/etc/serversOpen 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 ******
Apache SetupIn order for Apache to serve the Nagios interface, you need to enable a couple of Apache modules. Begin with that.
Now, create an admin password for a Nagios admin user. After you enter the command, you'll be prompted to create you password.
$ sudo a2enmod rewrite $ sudo a2enmod cgi
$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadminYou already copied over the default Apache configuration for Nagios. You just need to enable it. Create the symlink between the copy in
sudo ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/
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.
Obviously, substitute your actual external IP for the final IP. The
Order deny,allow Deny from all Allow from 127.0.0.1 172.86.186.XXX
XXXpart 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 ServiceNagios doesn't automatically come with a Systemd service file. You need to make it at
/etc/systemd/system/nagios.service. Copy the one below.
Enable and start your service.
[Unit] Description=Nagios BindTo=network.target [Install] WantedBy=multi-user.target [Service] Type=simple User=nagios Group=nagcmd ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
$ sudo systemctl enable /etc/systemd/system/nagios.service $ sudo systemctl start nagios
Open NagiosNagios 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.