如何在 CentOS 8/RHEL 8 上安装和配置 Nagios Core
Nagios
Nagios 是一个自由开源的网络和警报引擎，它用于监控各种设备，例如网络设备和网络中的服务器。它支持 Linux 和 Windows，并提供了直观的 Web 界面，可让你轻松监控网络资源。经过专业配置后，它可以在服务器或网络设备下线或者故障时向你发出邮件警报。在本文中，我们说明了如何在 RHEL 8/CentOS 8 上安装和配置 Nagios Core。


![Install-Nagios-Core-RHEL8-CentOS8](/data/attachment/album/201911/13/230150f6q98q96b6ppflqm.jpg)


### Nagios Core 的先决条件


在开始之前，请先检查并确保有以下各项：


* RHEL 8/CentOS 8 环境
* 能通过 SSH 访问该环境
* 快速稳定的互联网连接


满足上述要求后，我们开始吧！


### 步骤 1：安装 LAMP


为了使 Nagios 能够按预期工作，你需要安装 LAMP 或其他 Web 软件，因为它们将在浏览器上运行。为此，请执行以下命令：



```
# dnf install httpd mariadb-server php-mysqlnd php-fpm
```

![Install-LAMP-stack-CentOS8](/data/attachment/album/201911/13/230153m2b7ejzvf7ii7hki.jpg)


你需要确保 Apache Web 服务器已启动并正在运行。为此，请使用以下命令启用并启动 Apache 服务器：



```
# systemctl start httpd
# systemctl enable httpd
```

![Start-enable-httpd-centos8](/data/attachment/album/201911/13/230154iudeuvekvvjeckmu.jpg)


检查 Apache 服务器运行状态：



```
# systemctl status httpd
```

![Check-status-httpd-centos8](/data/attachment/album/201911/13/230157jpcejdlbgjs8ebee.jpg)


接下来，我们需要启用并启动 MariaDB 服务器，运行以下命令：



```
# systemctl start mariadb
# systemctl enable mariadb
```

![Start-enable-MariaDB-CentOS8](/data/attachment/album/201911/13/230200wmkkhrgggr5m3bmr.jpg)


要检查 MariaDB 状态，请运行：



```
# systemctl status mariadb
```

![Check-MariaDB-status-CentOS8](/data/attachment/album/201911/13/230201b5tpood5yjz4upp4.jpg)


另外，你可能会考虑加强或保护服务器，使其不容易受到未经授权的访问。要保护服务器，请运行以下命令：



```
# mysql_secure_installation
```

确保为你的 MySQL 实例设置一个强密码。对于后续提示，请输入 “Y” 并按回车。


![Secure-MySQL-server-CentOS8](/data/attachment/album/201911/13/230204gdgw5gwk54k3wkgd.jpg)


### 步骤 2：安装必需的软件包


除了安装 LAMP 外，还需要一些其他软件包来安装和正确配置 Nagios。因此，如下所示安装软件包：



```
# dnf install gcc glibc glibc-common wget gd gd-devel perl postfix
```

![Install-requisite-packages-CentOS8](/data/attachment/album/201911/13/230207z126a3ow1aiowzh6.jpg)


### 步骤 3：创建 Nagios 用户帐户


接下来，我们需要为 Nagios 用户创建一个用户帐户。为此，请运行以下命令：



```
# adduser nagios
# passwd nagios
```

![Create-new-user-for-Nagios](/data/attachment/album/201911/13/230208engfz01gsgnsg32y.jpg)


现在，我们需要为 Nagios 创建一个组，并将 Nagios 用户添加到该组中。



```
# groupadd nagiosxi
```

现在添加 Nagios 用户到组中：



```
# usermod -aG nagiosxi nagios
```

另外，将 Apache 用户添加到 Nagios 组：



```
# usermod -aG nagiosxi apache
```

![Add-Nagios-group-user](/data/attachment/album/201911/13/230209w3lu0ex64p1945vl.jpg)


### 步骤 4：下载并安装 Nagios Core


现在，我们可以继续安装 Nagios Core。Nagios 4.4.5 的最新稳定版本于 2019 年 8 月 19 日发布。但首先，请从它的官方网站下载 Nagios tarball 文件。


要下载 Nagios Core，请首进入 `/tmp` 目录：



```
# cd /tmp
```

接下来下载 tarball 文件：



```
# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.5.tar.gz
```

![Download-Nagios-CentOS8](/data/attachment/album/201911/13/230211uqt0r2i8geizuti2.jpg)


下载完 tarball 文件后，使用以下命令将其解压缩：



```
# tar -xvf nagios-4.4.5.tar.gz
```

接下来，进入未压缩的文件夹：



```
# cd nagios-4.4.5
```

按此顺序运行以下命令：



```
# ./configure --with-command-group=nagcmd
# make all
# make install
# make install-init
# make install-daemoninit
# make install-config
# make install-commandmode
# make install-exfoliation
```

要配置 Apache，请运行以下命令：



```
# make install-webconf
```

### 步骤 5：配置 Apache Web 服务器身份验证


接下来，我们将为用户 `nagiosadmin` 设置身份验证。请注意不要更改该用户名，否则，可能会要求你进一步的配置，这可能很繁琐。


要设置身份验证，请运行以下命令：



```
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
```

![Configure-Apache-webserver-authentication-CentOS8](/data/attachment/album/201911/13/230213n2uszxt99623tclu.jpg)


系统将提示你输入 `nagiosadmin` 用户的密码。输入并按要求确认密码。在本教程结束时，你将使用该用户登录 Nagios。


为使更改生效，请重新启动 Web 服务器：



```
# systemctl restart httpd
```

### 步骤 6：下载并安装 Nagios 插件


插件可以扩展 Nagios 服务器的功能。它们将帮助你监控各种服务、网络设备和应用。要下载插件的 tarball 文件，请运行以下命令：



```
# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
```

接下来，解压 tarball 文件并进入到未压缩的插件文件夹：



```
# tar -xvf nagios-plugins-2.2.1.tar.gz
# cd nagios-plugins-2.2.1
```

要安装插件，请编译源代码，如下所示：



```
# ./configure --with-nagios-user=nagios --with-nagios-group=nagiosxi
# make
# make install
```

### 步骤 7：验证和启动 Nagios


成功安装 Nagios 插件后，验证 Nagios 配置以确保一切良好，并且配置中没有错误：



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

![Verify-Nagios-settings-CentOS8](/data/attachment/album/201911/13/230216t0hvokvmtv5ctcn7.jpg)


接下来，启动 Nagios 并验证其状态：



```
# systemctl start nagios
# systemctl status nagios
```

![Start-check-status-Nagios-CentOS8](/data/attachment/album/201911/13/230218ry6ty5dytkkdytw3.jpg)


如果系统中有防火墙，那么使用以下命令允许 ”80“ 端口：



```
# firewall-cmd --permanent --add-port=80/tcp# firewall-cmd --reload
```

### 步骤 8：通过 Web 浏览器访问 Nagios 面板


要访问 Nagios，请打开服务器的 IP 地址，如下所示：<http://server-ip/nagios> 。


这将出现一个弹出窗口，提示输入我们在步骤 5 创建的用户名和密码。输入凭据并点击“Sign In”。


![Access-Nagios-via-web-browser-CentOS8](/data/attachment/album/201911/13/230220oq9jz74qic6uzruw.jpg)


这将引导你到 Nagios 面板，如下所示：


![Nagios-dashboard-CentOS8](/data/attachment/album/201911/13/230225finsj8ggtygj0ji6.jpg)


我们终于成功地在 CentOS 8 / RHEL 8 上安装和配置了 Nagios Core。欢迎你的反馈。




---


via: <https://www.linuxtechi.com/install-nagios-core-rhel-8-centos-8/>


作者：[James Kiarie](https://www.linuxtechi.com/author/james/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
**Nagios** is a free and opensource network and alerting engine used to monitor various devices, such as network devices, and servers in a network. It supports both **Linux** and** Windows OS** and provides an intuitive web interface that allows you to easily monitor network resources. When professionally configured, it can alert you in the event a server or a network device goes down or malfunctions via email alerts. In this topic, we shed light on how you can install and configure Nagios core on **RHEL 8** / **CentOS 8**.

#### Prerequisites of Nagios Core

Before we begin, perform a flight check and ensure you have the following:

- An instance of RHEL 8 / CentOS 8
- SSH access to the instance
- A fast and stable internet connection

With the above requirements in check, let’s roll our sleeves!

#### Step 1: Install LAMP Stack

For Nagios to work as expected, you need to install LAMP stack or any other web hosting stack since it’s going to run on a browser. To achieve this, execute the command:

# dnf install httpd mariadb-server php-mysqlnd php-fpm

You need to ensure that Apache web server is up and running. To do so, start and enable Apache server using the commands:

# systemctl start httpd # systemctl enable httpd

To check the status of Apache server run

# systemctl status httpd

Next, we need to start and enable MariaDB server, run the following commands

# systemctl start mariadb # systemctl enable mariadb

To check MariaDB status run:

# systemctl status mariadb

Also, you might consider hardening or securing your server and making it less susceptible to unauthorized access. To secure your server, run the command:

# mysql_secure_installation

Be sure to set a strong password for your MySQL instance. For the subsequent prompts, Type **Yes** and hit **ENTER**

#### Step 2: Install Required packages

Apart from installing the LAMP server, some additional packages are needed for the installation and proper configuration of Nagios. Therefore, install the packages as shown below:

# dnf install gcc glibc glibc-common wget gd gd-devel perl postfix

#### Step 3: Create a Nagios user account

Next, we need to create a user account for the Nagios user. To achieve this , run the command:

# adduser nagios # passwd nagios

Now, we need to create a group for Nagios and add the Nagios user to this group.

# groupadd nagiosxi

Now add the Nagios user to the group

# usermod -aG nagiosxi nagios

Also, add Apache user to the Nagios group

# usermod -aG nagiosxi apache

#### Step 4: Download and install Nagios core

We can now proceed and install Nagios Core. The latest stable version in Nagios 4.4.5 which was released on August 19, 2019. But first, download the Nagios tarball file from its official site.

To download Nagios core, first head to the tmp directory

# cd /tmp

Next download the tarball file

# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.5.tar.gz

After downloading the tarball file, extract it using the command:

# tar -xvf nagios-4.4.5.tar.gz

Next, navigate to the uncompressed folder

# cd nagios-4.4.5

Run the commands below in this order

# ./configure --with-command-group=nagcmd # make all # make install # make install-init # make install-daemoninit # make install-config # make install-commandmode # make install-exfoliation

To setup Apache configuration issue the command:

# make install-webconf

#### Step 5: Configure Apache Web Server Authentication

Next, we are going to setup authentication for the user **nagiosadmin**. Please be mindful not to change the username or else, you may be required to perform further configuration which may be quite tedious.

To set up authentication run the command:

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

You will be prompted for the password of the nagiosadmin user. Enter and confirm the password as requested. This is the user that you will use to login to Nagios towards the end of this tutorial.

For the changes to come into effect, restart your web server.

# systemctl restart httpd

#### Step 6: Download & install Nagios Plugins

Plugins will extend the functionality of the Nagios Server. They will help you monitor various services, network devices, and applications. To download the plugin tarball file run the command:

# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz

Next, extract the tarball file and navigate to the uncompressed plugin folder

# tar -xvf nagios-plugins-2.2.1.tar.gz # cd nagios-plugins-2.2.1

To install the plugins compile the source code as shown

# ./configure --with-nagios-user=nagios --with-nagios-group=nagiosxi # make # make install

#### Step 7: Verify and Start Nagios

After the successful installation of Nagios plugins, verify the Nagios configuration to ensure that all is well and there is no error in the configuration:

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

Next, start Nagios and verify its status

# systemctl start nagios # systemctl status nagios

In case Firewall is running on system then allow “80” using the following command

# firewall-cmd --permanent --add-port=80/tcp # firewall-cmd --reload

#### Step 8: Access Nagios dashboard via the web browser

To access Nagios, browse your server’s IP address as shown

http://server-ip/nagios

A pop-up will appear prompting for the username and the password of the user we created earlier in Step 5. Enter the credentials and hit ‘**Sign In**’

This ushers you to the Nagios dashboard as shown below

We have finally successfully installed and configured Nagios Core on CentOS 8 / RHEL 8. Your feedback is most welcome.

Also Read : [How to Add Windows and Linux host to Nagios Server for Monitoring](https://www.linuxtechi.com/add-windows-linux-host-to-nagios-server/)

Also Read : [How to Setup Multi Node Elastic Stack Cluster on RHEL 8 / CentOS 8](https://www.linuxtechi.com/setup-multinode-elastic-stack-cluster-rhel8-centos8/)

MikeThank you for posting.

It worked for me. I had one small issue near the end:

./configure –with-command-group=nagcmd #nagcmd generated make errors

./configure –with-command-group=nagiosxi #I substituted nagiosxi for nagcmd

Devang Thakkaryes its worked for me after # ./configure –with-command-group=nagiosxi

SteveThis should be on separate lines.

# firewall-cmd –permanent –add-port=80/tcp# firewall-cmd –reload

Pradeep KumarThank You Steve for pointing out the typo, i have correct it now.

Ross KhanHi – great guide – worked first time, but with one tweak. These lines seemed inconsistent…?

usermod -aG nagiosxi nagios

./configure –with-command-group=nagcmd

I think the second line should be:

./configure –with-command-group=nagiosxi

The second line worked for me and nagios now up and running…

rootagreed, good article but +1 on the slight modification

SamsonIt works, I checked it. However, I see no reason to install nagios from source codes, you can easily install the EPEL repo and get everithyng installed with a couple of commands.

Moreover, there is no need to install mariadb, nagios doesn’t use it.

Tim PerkinsFYI, in “Step 7: Verify and Start Nagios” when running “systemctl start nagios”, I got:

Failed to start nagios.service: Unit nagios.service not found.

I had to add a file:

/etc/systemd/system/nagios.service

… in order to get things working

Jhon Robinscon CortesHello, thanks for your contribution.

I have a problem, I installed everything but when I go to the http: // server-ip / nagios browser, the page does not appear. should I configure something else?

James KiarieHey Jhon Robinson, If you are accessing the Nagios server from a different machine , please check the firewall configuration and ensure that you have opened port 80 on the Nagios server.

pujathank you so much..

only one error *./configure –with-command-group=nagcmd

replace above line with ./configure –with-command-group=nagiosxi

KrishaneHow to monitor HCI nodes installed with VMWare ESXi hypervisors ( As there no OS on the host machine, how we can use nagios?/for any failing nodes of any HCI servers?