LoginSignup
2
2

CentOS7 zabbixサーバーを構築してみた

Last updated at Posted at 2023-10-14

はじめに

zabbixサーバーを業務で使用することになり、仕組みを理解するために構築をしてみた
検証段階のため、参考程度にしてください。

前提条件
VirtualBox 
CentOS7

zabbixサーバー構築で参考にしたサイト

以下のサイトを参考にしました。
https://infracollege.vamdemicsystem.black/linux/

手順

zabbixサーバー構築前にリポジトリ設定をする

rootユーザーに昇格する

sudo su -

現在のリポジトリ設定を確認した

yum repolist
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
リポジトリー ID                                                   リポジトリー名                                                      状態
base/7/x86_64                                                     CentOS-7 - Base                                                     10,072
extras/7/x86_64                                                   CentOS-7 - Extras                                                      518
updates/7/x86_64                                               CentOS-7 - Updates 

EPEL リポジトリを追加

zabbixを構築するには、CentOS7に追加のパッケージが必要なため、EPELリポジトリを追加する

yum -y install epel-release

yum updateをする

サーバー構築時に脆弱性を防ぐために最初にパッケージをアップデートします。

yum update

remiリポジトリを追加

Zabbixを構築するには、CentOS7に追加のパッケージが必要なため、remiリポジトリを追加する
主にphp関連のパッケージが入っています。

rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

remiリポジトリ GPGキーをインポートする

remiリポジトリを安全に利用するためにはGPGキーをインストールをします。

rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi

mysqlのリポジトリを追加

mysqlのパッケージをインストールするために、mysqlリポジトリを追加する

rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

現在のリポジトリ設定を確認した

remi epel mysqlのリポジトリが追加されました。

# yum repolist
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.riken.jp
 * remi-safe: cdn.centos.no
 * updates: ftp.riken.jp
リポジトリー ID                                                                      リポジトリー名                                                                                                 状態
base/7/x86_64                                                                        CentOS-7 - Base                                                                                                10,072
epel/x86_64    追加                                                                   Extra Packages for Enterprise Linux 7 - x86_64                                                                 13,771
extras/7/x86_64                                                                      CentOS-7 - Extras                                                                                                 518
mysql-connectors-community/x86_64 追加                                                   MySQL Connectors Community                                                                                        227
mysql-tools-community/x86_64  追加                                                       MySQL Tools Community                                                                                             100
mysql80-community/x86_64 追加                                                             MySQL 8.0 Community Server                                                                                        426
remi-safe      追加                                                                      Safe Remi's RPM repository for Enterprise Linux 7 - x86_64                                                      5,538
updates/7/x86_64                                                                     CentOS-7 - Updates                                                                                              5,287
repolist: 35,939

SELinux設定

SElinuxは設定が複雑なため、正しく設定する方法が不明なため動作影響しない様に設定をオフにする

vi /etc/selinux/config
SELINUX=disabled

サーバーの再起動をする

shutdown -r now

SElinuxが無効化されていることを確認する

getenforce
Disabled

apacheをインストールする

yum -y install httpd
systemctl start httpd
systemctl enable httpd

PHP7.2インストール

remiリポジトリからphpパッケージをインストールする

yum --enablerepo=remi-safe -y install php72-php php72-php-mysqlnd php72-php-xml php72-php-bcmath php72-php-ldap php72-php-gd

Apahce設定

/var/www/html配下の.htaccessファイルの利用とそのファイルでオーバライド出来る設定です。

vi /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
AllowOverride All

ファイアウォールの設定

10051はZabbixプロキシーからZabbixサーバーにデータ転送を許可するため
10050はZabbixエージェント(監視される端末)からの通信を許可するため

firewall-cmd --add-service={http,https} --permanent
firewall-cmd --add-port={10051/tcp,10050/tcp} --permanent
firewall-cmd --reload

ファイアウォールの設定を確認

firewall-cmd --list-all

[root@localhost conf]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources:
  services: dhcpv6-client http https ssh ※http httpsを追加
  ports: 10051/tcp 10050/tcp ※10051 10050のポートを追加
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

php動作確認

vi /var/www/html/index.php
<?php phpinfo(); ?>

apacheの再起動をする

systemctl restart httpd
systemctl status httpd

phpが動作していることを確認

ブラウザ
http://zabbixサーバー(ゲストOS)のIPアドレス/index.php

image.png

mysqlのパッケージをインストール

yum -y install mysql-community-devel

エラーの解決は以下のQiitaの記事を参考にしました。
https://qiita.com/ogatamaquzilla/items/7f2e90806f3b6851e7b0

yum -y install mysql-community-server

以下のエラーでインストール失敗
The GPG keys listed for the "MySQL 8.0 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.

2022年のGPGをインポートしたらmysql-community-serverのインストールが成功しました。

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

mysql設定

vi /etc/my.cnf
default-authentication-plugin=mysql_native_password

mysqlサービスを再起動

systemctl start mysqld
systemctl enable mysqld

mysqlのパスワード確認

[root@localhost conf]# grep password /var/log/mysqld.log | awk -F ' ' '{print $13}'
xxxxxxxxxxxx<パスワードが表示される>
will

mysql_secure_installation

最低限のセキュリティ設定をするためにmysql_secure_installationを実行する

参考サイト
https://weblabo.oscasierra.net/mysql-57-init-setup/

# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: ※先程のmyqlのパスワードを入れる

The existing password for the user account root has expired. Please set a new password.

New password: ※新しいパスワードを設定する

Estimated strength of the password: 100

※パスワードを変更しますか
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

anonymous ユーザーを削除しますか
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

リモートからの root ログインを禁止しますか?
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

テスト データベースを削除してアクセスしますか?
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

特権テーブルを今すぐリロードしますか?
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

mysqlにログインできない

エラーが出てmysqlにログイン出来ない

[root@localhost conf]# mysql -uroot -p'パスワード'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost conf]#

mysqlにパスワード無しでログインした

以下のサイトの対策その2を参照しました。
https://onoredekaiketsu.com/can-not-root-login-to-mysql-with-error-1045-28000/

# systemctl stop mysqld
# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
# systemctl start mysqld

上記の方法でmysqlにログイン出来ました。

[root@localhost conf]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

zabbixのデータベースを作成

CREATE DATABASE zabbix;

zabbixユーザー作成した

以下のエラーが出ました。

mysql> CREATE USER zabbix@localhost IDENTIFIED BY '!QAZxsw2';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

以下のQiitaの記事を参考にしました。
https://qiita.com/matsuma/items/fac4581aedeb699c6e9e

mysql> flush privileges;  ※左記のコマンドを実行しました。
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE USER zabbix@localhost IDENTIFIED BY '!QAZxsw2';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL ON zabbix.* TO zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye

zabbixのリポジトリを追加

yum -y install http://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-1.el7.noarch.rpm

リポジトリリストを表示してzabbixリポジトリが登録されていることを確認した

[root@localhost conf]# yum repolist
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.riken.jp
 * remi-safe: ftp.riken.jp
 * updates: ftp.riken.jp
zabbix                                                                                                               | 2.9 kB  00:00:00
zabbix-non-supported                                                                                                 | 2.9 kB  00:00:00
(1/2): zabbix-non-supported/x86_64/primary_db                                                                        | 3.7 kB  00:00:00
(2/2): zabbix/x86_64/primary_db                                                                                      |  71 kB  00:00:01
リポジトリー ID                                       リポジトリー名                                                                  状態
base/7/x86_64                                         CentOS-7 - Base                                                                 10,072
epel/x86_64                                           Extra Packages for Enterprise Linux 7 - x86_64                                  13,771
extras/7/x86_64                                       CentOS-7 - Extras                                                                  518
mysql-connectors-community/x86_64                     MySQL Connectors Community                                                         227
mysql-tools-community/x86_64                          MySQL Tools Community                                                              100
mysql80-community/x86_64                              MySQL 8.0 Community Server                                                         426
remi-safe                                             Safe Remi's RPM repository for Enterprise Linux 7 - x86_64                       5,538
updates/7/x86_64                                      CentOS-7 - Updates                                                               5,287
zabbix/x86_64        ※追加                                  Zabbix Official Repository - x86_64                                                132
zabbix-non-supported/x86_64 ※追加                           Zabbix Official Repository non-supported - x86_64                                    5
repolist: 36,076

zabbixのパッケージをインストール

yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-web-japanese
yum -y install zabbix-agent
yum -y install zabbix-get

zabbix用PHP設定

vi /etc/httpd/conf.d/zabbix.conf
#
php_value date.timezone Asia/Tokyo

DB流し込み

cd /usr/share/doc/zabbix-server-mysql-4.2.8

gzipを解凍
zcat create.sql.gz | mysql -uzabbix -p -D zabbix

zabbixサービスを起動する

systemctl start zabbix-server
systemctl enable zabbix-server

zabbixエージェントを起動する

systemctl start zabbix-agent
systemctl enable zabbix-agent
zabbix_get -s 127.0.0.1 -k agent.version
4.2.8

apacheサービスを再起動する

systemctl restart httpd

zabbixの画面が表示されました

http://zabbix(ゲストOS)のIPアドレス/zabbix/
image.png

「Next step」を押下する
image.png

パスワードを設定して[Next Step」を押下する
image.png

[Next Step」を押下する
image.png

[Next Step」を押下する
image.png

「Finish」を押下する
image.png

ユーザーadmin パスワード zabbixでログインする
image.png

image.png

まとめ

mysqlのパスワード設定が解決できずに、パスワード無しでmysqlに入ってzabbixユーザーを作成しました。
また、zabbixテーブルにDB流し込みのところも正直上手くいっていないです。
そのため再度調査して本記事を更新する予定です。
zabbixサーバーの構築の流れを本記事から参考にしてください
手順に使用されているコマンドの意味を理解しながら構築することをお勧めします。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2