如何在 Ubuntu 22.04 上安装 Nagios 服务器教程

发布于:2025-02-10 ⋅ 阅读:(85) ⋅ 点赞:(0)

简介

在本教程中,我们将解释如何在 Ubuntu 22.04 上安装和配置 Nagios,使用 Apache 作为 Web 服务器,并通过 Let’s Encrypt Certbot 使用 SSL 证书进行保护。

Nagios 是一个强大的监控系统,它可以帮助组织在 IT 基础设施问题影响关键业务流程之前识别并解决它们。本教程将指导你完成在 Ubuntu 服务器上安装和配置 Nagios 的步骤。

安装和配置步骤

第一步:更新系统软件包

在安装任何软件之前,重要的是将系统软件包更新到最新版本。

sudo apt update
sudo apt upgrade -y

第二步:安装所需依赖

Nagios 需要安装几个软件包。使用以下命令安装这些依赖项:

sudo apt install -y autoconf gcc make libgd-dev libmcrypt-dev libssl-dev apache2 php libapache2-mod-php7.4 build-essential unzip

第三步:创建 Nagios 用户和组

为 Nagios 创建一个用户和组,以便在其中运行:

sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd www-data

第四步:下载并安装 Nagios Core

官方网站 下载 Nagios Core 的最新稳定版本。在撰写本文时,最新版本是 4.5.3

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

编译并安装 Nagios:

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

安装 Nagios 插件

Nagios 使用插件来监控服务。下载并安装 最新的 Nagios 插件

cd /tmp
wget https://nagios-plugins.org/download/nagios-plugins-2.4.10.tar.gz
tar -zxvf nagios-plugins-2.4.10.tar.gz
cd nagios-plugins-2.4.10

编译并安装插件:

sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios
sudo make
sudo make install

第五步:配置 Nagios Web 界面

通过为访问 Web UI 创建管理员用户来设置 Nagios Web 界面:

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

启动 Nagios 服务并使其在启动时启动:

sudo systemctl start nagios
sudo systemctl enable nagios

第六步:配置防火墙

我们需要在防火墙中添加 HTTP 和 HTTPS 端口。

ufw allow 80/tcp
ufw allow 443/tcp
ufw reload

第七步:配置 Apache Web 服务器

为你的域名配置 Apache。

修改你域名的现有虚拟主机文件:

sudo nano /etc/apache2/sites-available/nagios.conf

注意:yourdomain.com 替换为你的域名,并将 admin@yourdomain.com 替换为你的电子邮件 ID。添加以下配置:

<VirtualHost *:80>
    ServerAdmin admin@yourdomain.com
    ServerName yourdomain.com
    DocumentRoot /usr/local/nagios/share

    ErrorLog ${APACHE_LOG_DIR}/nagios_error.log
    CustomLog ${APACHE_LOG_DIR}/nagios_access.log combined

     <-- 所有现有内容都放在这里 -->

</VirtualHost>

启用站点和所需的 Apache 模块:

a2enmod cgi
sudo a2ensite nagios.conf
sudo a2enmod cgi rewrite
sudo systemctl restart apache2

第八步:使用 Let’s Encrypt 安装和配置 SSL

安装 Certbot:

sudo apt install certbot python3-certbot-apache -y

获取并安装 SSL 证书:

sudo certbot --apache -d yourdomain.com

按照提示完成 SSL 安装。Certbot 将自动配置 Apache 以使用新的 SSL 证书。

第九步:访问 Nagios Web 界面

打开你的 Web 浏览器并导航到 Nagios Web 界面:

http://<domain-name>/

使用用户名 nagiosadmin 和你之前设置的密码登录。

第十步:验证 Nagios 配置

要确保 Nagios 正常工作,你可以检查其配置:

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

Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2024-06-11
License: GPL

Website: https://www.nagios.org
Reading configuration data...
    Read main config file okay...
    Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
    Checked 8 services.
    Checked 1 hosts.
    Checked 1 host groups.
    Checked 0 service groups.
    Checked 1 contacts.
    Checked 1 contact groups.
    Checked 24 commands.
    Checked 5 time periods.
    Checked 0 host escalations.
    Checked 0 service escalations.
Checking for circular paths...
    Checked 1 hosts
    Checked 0 service dependencies
    Checked 0 host dependencies
    Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:  0

Things look okay - No serious problems were detected during the pre-flight check

添加监控服务

你可以通过编辑 /usr/local/nagios/etc/objects/ 中的配置文件来添加要监控的主机和服务。以下是如何添加新主机的示例:

打开 hosts.cfg 文件:

sudo nano /usr/local/nagios/etc/objects/hosts.cfg

添加新的主机定义:

define host {
    use                  linux-server
    host_name            example-host
    alias                Example Host
    address              192.168.1.100
    max_check_attempts    5
    check_period          24x7
    notification_interval 30
    notification_period   24x7
}

保存并关闭文件。

定义新服务

服务是你想要监控的主机的各个方面(例如,HTTP、PING)。

打开 services.cfg 文件:

sudo nano /usr/local/nagios/etc/objects/services.cfg

添加服务定义:

define host {
    use                     linux-server
    host_name               example-host
    alias                   Example Host
    address                 192.168.1.100
    max_check_attempts      5
    check_period            24x7
    notification_interval   30
    notification_period     24x7

保存并关闭文件。

重启 Nagios 以应用更改:

sudo systemctl restart nagios

结尾

你已经成功地看到了如何在 Ubuntu 22.04 服务器上安装和配置 Nagios。你现在可以开始添加主机和服务来有效地监控你的网络和系统基础设施。有关更详细的配置和自定义,请参阅官方 Nagios 文档。

我的博客:https://blog.ivwv.site


网站公告

今日签到

点亮在社区的每一天
去签到