# Zabbix Server 建置操作手冊
###### tags: `監控工具安裝手冊`
## 第一部分 : 設定時區
```bash=+
# timedatectl set-timezone Asia/Taipei
```
## 第二部分 : 安裝 PHP
* 這裡的 php 版本至少 7.2 (出現錯誤 : Minimum required PHP version is 7.2.0.)
```bash=+
# amazon-linux-extras enable php7.2
# yum install php php-xml php-bcmath php-mbstring php-gd php-mysqli php-ldap
# yum update php
```
## 第三部分 : 安裝及設定 Mariadb
### 安裝 & 啟動
```bash=+
# vim /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.6/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
# yum makecache
# yum install mariadb mariadb-server
# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
# systemctl start mariadb
```
### 初始化設定
```bash=+
# mysql_secure_installation
Enter current password for root (enter for none):
> 輸入 root 密碼,預設 MariaDB 沒有密碼,直接按 enter
Change the root password? [Y/n] y
New password:
> 輸入要設定的 root 帳號密碼
Re-enter new password:
> 再輸入一次輸入要設定的 root 帳號密碼
Remove anonymous users? [Y/n] y
> 移除匿名帳號,輸入 y
Disallow root login remotely? [Y/n] y
> 不允許遠端使用 root 帳號登入,考量安全性應該要輸入 y
Remove test database and access to it? [Y/n] y
> 移除測試資料庫與帳號,輸入 y
Reload privilege tables now? [Y/n] y
> 重新載入權限設定,輸入 y
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
```
### 創建 zabbix 數據庫
```bash=+
# mysql -u root -p
Enter password: (跟上方設定密碼一樣)
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server
MariaDB [(none)]> CREATE DATABASE zabbix CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'zabbix';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES; //系統更新
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> QUIT
Bye
```
### 補充 : 忘記密碼重新設置的方法
```bash=+
MariaDB [(none)]> set password for zabbix@localhost=password('zabbix');
Query OK, 0 rows affected (0.00 sec)
```
## 第四部分 : 安裝 Apache
```bash=+
# yum install httpd
# systemctl enable httpd
# systemctl start httpd
```
## 第五部分 : 安裝 Zabbix
### 下載 & 解壓縮安裝包
```bash=+
# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.0.tar.gz
# tar -zxvf zabbix-5.0.0.tar.gz
```
### 創建 zabbix 用戶
```bash=+
# groupadd --system zabbix
# useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix
# mkdir -m u=rwx,g=rwx,o= -p /usr/lib/zabbix
# chown zabbix:zabbix /usr/lib/zabbix
```
### 安裝其他套件及配置
```bash=+
# yum install gcc mysql-devel libxml2-devel net-snmp-devel OpenIPMI OpenIPMI-devel libevent-devel curl curl-devel automake autoconf libtool make
# cd /home/ec2-user/zabbix-5.0.0
# ./configure --enable-server --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2 --with-openipmi --prefix=/usr/local/zabbix
# make install
```
### 查看是否安裝成功
```bash=+
# cd /usr/local/zabbix/sbin/
# ls
zabbix_server
```
### 創建數據庫表格
```bash=+
# cd /home/ec2-user/zabbix-5.0.0/database/mysql
# mysql -uzabbix -pzabbix zabbix < schema.sql
# mysql -uzabbix -pzabbix zabbix < images.sql
# mysql -uzabbix -pzabbix zabbix < data.sql
```
## 第六部分 : 配置 Zabbix
### 修改 `zabbix_server.conf`
```bash=+
# vim /usr/local/zabbix/etc/zabbix_server.conf
DBUser=zabbix
DBPassword=zabbix
```
### 防火牆配置
```bash=+
# yum install firewalld
# systemctl start firewalld
# firewall-cmd --permanent --add-port=10051/tcp
# firewall-cmd --permanent --add-port=10050/tcp
# systemctl stop firewalld
```
### 將資源包中提供的 `init.d` 啟動腳本拷貝到系統中並修改位置
```bash=+
# cp /home/ec2-user/zabbix-5.0.0/misc/init.d/fedora/core/zabbix_* /etc/init.d/ -v
‘/home/ec2-user/zabbix-5.0.0/misc/init.d/fedora/core/zabbix_agentd’ -> ‘/etc/init.d/zabbix_agentd’
‘/home/ec2-user/zabbix-5.0.0/misc/init.d/fedora/core/zabbix_server’ -> ‘/etc/init.d/zabbix_server’
# vim /etc/init.d/zabbix_server
BASEDIR=/usr/local/zabbix
```
### 啟動服務
```bash=+
# /etc/init.d/zabbix_server start
Reloading systemd: [ OK ]
Starting zabbix_server (via systemctl): [ OK ]
# /etc/init.d/zabbix_server status
● zabbix_server.service - SYSV: Starts and stops Zabbix Server using chkconfig
Loaded: loaded (/etc/rc.d/init.d/zabbix_server; bad; vendor preset: disabled)
Active: active (running) since Tue 2022-10-18 13:44:58 CST; 7s ago
```
### 設定開機啟動服務
```bash=+
# chkconfig --add zabbix_server
# chkconfig --level 35 zabbix_server on
# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
zabbix_agentd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
zabbix_server 0:off 1:off 2:off 3:on 4:off 5:on 6:off
```
## 第七部分 : 配置 Web 介面
* 前置相關設定
```bash=+
# vim /etc/php.ini
date.timezone = "Asia/Taipei" // 前面是 ; 要拿掉
post_max_size = 16M
max_execution_time = 300
max_input_time = 300
```
* 介面設定
```bash=+
# mkdir /var/www/html/zabbix/
# cp -a /home/ec2-user/zabbix-5.0.0/ui/ /var/www/html/zabbix
# chown -R zabbix.zabbix /var/www/html/zabbix/
# systemctl restart httpd
```
## 第八部分 : 連線至 Zabbix 並登入
### 1. 輸入網址 : `http://<IP>/zabbix/ui/setup.php`

### 2. 設定密碼 (這裡設定 "zabbix" 即可)

### 3. 設定服務器名稱 (自訂即可)

### 4. 最終完成設定如圖

### 5. .conf.php 檔案設定
* 下載文件並將內容複製並放在 `/var/www/html/zabbix/ui/conf/zabbix.conf.php` 位置

#### EX.(參考用的 `zabbix.conf.php` )
```
# vim /var/www/html/zabbix/ui/conf/zabbix.conf.php
<?php
// Zabbix GUI configuration file.
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = 'localhost';
$DB['PORT'] = '0';
$DB['DATABASE'] = 'zabbix';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = 'zabbix';
// Schema name. Used for PostgreSQL.
$DB['SCHEMA'] = '';
// Used for TLS connection.
$DB['ENCRYPTION'] = false;
$DB['KEY_FILE'] = '';
$DB['CERT_FILE'] = '';
$DB['CA_FILE'] = '';
$DB['VERIFY_HOST'] = false;
$DB['CIPHER_LIST'] = '';
// Use IEEE754 compatible value range for 64-bit Numeric (float) history values.
// This option is enabled by default for new Zabbix installations.
// For upgraded installations, please read database upgrade notes before enabling this option.
$DB['DOUBLE_IEEE754'] = true;
$ZBX_SERVER = 'localhost';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = 'eva-zabbix';
$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
// Uncomment this block only if you are using Elasticsearch.
// Elasticsearch url (can be string if same url is used for all types).
//$HISTORY['url'] = [
// 'uint' => 'http://localhost:9200',
// 'text' => 'http://localhost:9200'
//];
// Value types stored in Elasticsearch.
//$HISTORY['types'] = ['uint', 'text'];
// Used for SAML authentication.
// Uncomment to override the default paths to SP private key, SP and IdP X.509 certificates, and to set extra settings.
//$SSO['SP_KEY'] = 'conf/certs/sp.key';
//$SSO['SP_CERT'] = 'conf/certs/sp.crt';
//$SSO['IDP_CERT'] = 'conf/certs/idp.crt';
//$SSO['SETTINGS'] = [];
```
### 6. 完成設定 ! ! !

### 7. 登入畫面
* 預設 帳號是`Admin` / 密碼是`zabbix`
* 網址 : `http://IP/zabbix/ui/index.php`

### 8. 初始頁面
