一、Web環境搭建部署Zabbix時,選擇合適的MariaDB、PHP和Nginx版本非常重要,以確保兼容性和最佳性能。以下是建議版本:Zabbix 6.4
MariaDB:官方文檔推薦使用MariaDB 10.3或更高版本。對于CentOS Stream 9,建議使用MariaDB 10.5或更新版本。
PHP:Zabbix 6.4支持PHP 8.0或更高版本。根據您的需求,可以選擇安裝PHP 8.0、8.1或8.2。請注意,PHP 8.3雖然也可能是可用的,但在撰寫本文時可能尚未被所有官方渠道支持。
Nginx:官方文檔中提到Nginx 1.18或更高版本是兼容的。對于CentOS Stream 9,默認提供的Nginx版本應該滿足要求。1、切換CentOS -stream-9源為阿里源
備份原有的yum源文件,以便需要時恢復:sudo mv /etc/yum.repos.d/CentOS-* /tmp/
centos.repo[baseos]
name=CentOS Stream $releasever - BaseOS
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1[baseos-debug]
name=CentOS Stream $releasever - BaseOS - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[baseos-source]
name=CentOS Stream $releasever - BaseOS - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/BaseOS/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[appstream]
name=CentOS Stream $releasever - AppStream
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1[appstream-debug]
name=CentOS Stream $releasever - AppStream - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[appstream-source]
name=CentOS Stream $releasever - AppStream - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/AppStream/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[crb]
name=CentOS Stream $releasever - CRB
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0[crb-debug]
name=CentOS Stream $releasever - CRB - Debug
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[crb-source]
name=CentOS Stream $releasever - CRB - Source
baseurl=https://mirrors.aliyun.com/centos-stream/$stream/CRB/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0
?centos-addons.repo
[highavailability]
name=CentOS Stream $releasever - HighAvailability
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0[highavailability-debug]
name=CentOS Stream $releasever - HighAvailability - Debug
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[highavailability-source]
name=CentOS Stream $releasever - HighAvailability - Source
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/HighAvailability/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[nfv]
name=CentOS Stream $releasever - NFV
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/NFV/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0[nfv-debug]
name=CentOS Stream $releasever - NFV - Debug
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/NFV/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[nfv-source]
name=CentOS Stream $releasever - NFV - Source
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/NFV/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[rt]
name=CentOS Stream $releasever - RT
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/RT/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0[rt-debug]
name=CentOS Stream $releasever - RT - Debug
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/RT/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[rt-source]
name=CentOS Stream $releasever - RT - Source
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/RT/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[resilientstorage]
name=CentOS Stream $releasever - ResilientStorage
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0[resilientstorage-debug]
name=CentOS Stream $releasever - ResilientStorage - Debug
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/$basearch/debug/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[resilientstorage-source]
name=CentOS Stream $releasever - ResilientStorage - Source
baseurl=http://mirrors.aliyun.com/centos-stream/$stream/ResilientStorage/source/tree/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0[extras-common]
name=CentOS Stream $releasever - Extras packages
baseurl=http://mirrors.aliyun.com/centos-stream/SIGs/$stream/extras/$basearch/extras-common/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1[extras-common-source]
name=CentOS Stream $releasever - Extras packages - Source
baseurl=http://mirrors.aliyun.com/centos-stream/SIGs/$stream/extras/source/extras-common/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0
生成緩存并且更新源:yum clean all && yum makecacheyum -y update
2、關閉防火墻,SELINUX
# 關閉防火墻(臨時關閉,系統重啟后會開啟)
systemctl stop firewalld
# 查看防火墻狀態
systemctl status firewalld
# 重啟系統后不開啟防火墻
systemctl disable firewalldSELinux是一種強制訪問控制的安全模塊,它被集成到了Linux操作系統中;在SELinux下,所有進程和用戶都必須明確獲得執行特定操作的權限。# 查看selinux狀態
getenforce
# 臨時關閉selinux
setenforce 0
# 永久關閉selinux,將SELINUX=disabled
vim /etc/selinux/config
3、web環境搭建(安裝LNMP)
LAMP和LNMP是常見的web環境,它們具體代表如下:
L:linux,操作系統
A或N:Apache或Nginx,Web服務器軟件
M:MariaDB或MySQL,數據庫軟件
P:PHP、Perl、Python,腳本軟件
?使用yum源,向系統安裝MariaDB、PHP、hpptd或者nginx,并借助yum解決LAMP各組件間的依賴關系
yum -y install mariadb mariadb-server php php-mysqlnd nginx# 啟動nginx
systemctl start nginx
# 設置開機自啟
systemctl enable nginx# 啟動mariadb
systemctl start mariadb
# 設置開機自啟
systemctl enable mariadb # 查看nginx的版本,默認版本已經夠用了
nginx -v
[root@localhost yum.repos.d]# nginx -v
nginx version: nginx/1.20.1# 查看php的版本
php -v[root@localhost yum.repos.d]# php -v
PHP 8.0.30 (cli) (built: Aug 3 2023 17:13:08) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologieswith Zend OPcache v8.0.30, Copyright (c), by Zend Technologies# 查看mariadb版本,10.5.27
mysql -V 或者 mysql --version[root@localhost yum.repos.d]# mysql -V
mysql Ver 15.1 Distrib 10.5.27-MariaDB, for Linux (x86_64) using EditLine wrapper
4、測試nginx,輸入地址:http://你的主機地址,看到ngin歡迎頁面,說明測試成功
5、配置數據庫
安全初始化數據庫
[root@localhost yum.repos.d]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
## 改變使用Unix套接字文件來進行用戶認證的方式 否
Switch to unix_socket authentication [Y/n] n... skipping.
You already have your root account protected, so you can safely answer 'n'.## 修改了root用戶的密碼,看個人需要 是
Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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.
## 是否移除匿名用戶 是
Remove anonymous users? [Y/n] 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.
## 不允許遠程連接 否
Disallow root login remotely? [Y/n] n... skipping.
By default, MariaDB 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? [Y/n] y- Dropping test database...... Success!- Removing privileges on test database...... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
## 是否重新加載權限表 是
Reload privilege tables now? [Y/n] y... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!
數據庫安裝并初始化完成
6、創建Zabbix數據庫及用戶
[root@localhost yum.repos.d]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.5.27-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
## 建zabbix庫,并設定utf8字符集
MariaDB [(none)]> create database zabbix character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.001 sec)
## 授權zabbix用戶給zabbix庫權限并修改密碼
MariaDB [(none)]>grant all privileges on zabbix.* to zabbix@localhost identified by '123456';
Query OK, 0 rows affected (0.001 sec)
## 然后刷新權限以使更改生效
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> exit
Bye
退出數據庫,測試zabbix用戶
[root@localhost yum.repos.d]# mysql -uzabbix -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.5.27-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
## 查看數據庫
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| zabbix |
+--------------------+
2 rows in set (0.001 sec)MariaDB [(none)]> exit
Bye
二、Server端安裝配置
1、添加Zabbix官方倉庫
yum -y install https://repo.zabbix.com/zabbix/6.4/rhel/9/x86_64/zabbix-release-6.4-1.el9.noarch.rpm
yum clean all && yum makecache
2、安裝Zabbix Server、Web前端和Agent
yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-sql-scripts zabbix-agent
3、配置數據庫
?配置Zabbix Server連接到MariaDB: 編輯/etc/zabbix/zabbix_server.conf
文件,設置數據庫連接參數,如DBHost
、DBName
、DBUser
和DBPassword
。
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix
修改配置文件 ,添加DBPassword=123456
vim /etc/zabbix/zabbix_server.conf
# 在文件的最后添加
NodeAddress=localhost:10051
AllowUnsupportedDBVersions=1
DBSocket=/var/lib/mysql/mysql.sock
更改時區vim /etc/php-fpm.d/zabbix.conf
php_value[date.timezone] = Asia/Shanghai # 有的話修改,沒有的可以添加啟動Zabbix Server和Agent,并設置開機自啟systemctl restart zabbix-server zabbix-agent nginx php-fpm
systemctl enable zabbix-server zabbix-agent nginx php-fpm
三、 Zabbix訪問測試,http://你的地址/zabbix
訪問過程中出現的問題
解決過程
配置Nginx以托管Zabbix前端:
編輯或創建一個Nginx配置文件(例如/etc/nginx/conf.d/zabbix.conf),并將內容設置為適合Zabbix的配置。請確保配置中包含正確的PHP處理指令以及指向Zabbix文檔根目錄的路徑。一個基本的Nginx配置示例如下:
server {listen 80;server_name yourdomain.com;root /usr/share/zabbix;index index.php;location / {try_files $uri $uri/ /index.php?$query_string;}location ~ \.php$ {fastcgi_pass unix:/run/php-fpm/www.sock;fastcgi_index index.php;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}
}
開始測試:
為了確保Zabbix在CentOS Stream 9上的正常運行,需要調整PHP的一些配置參數以滿足Zabbix的要求。具體來說,post_max_size
、max_execution_time
和max_input_time
這三個配置項需要設置為至少指定的最小值。
第二步錯誤解決辦法
修改PHP配置
你可以通過編輯PHP的主配置文件來更改這些設置。對于大多數系統,默認的PHP配置文件位于/etc/php.ini。不過,如果你使用的是PHP-FPM,你可能還需要檢查并編輯/etc/php-fpm.d/www.conf中的相關設置。以下是具體的步驟:編輯PHP配置文件打開PHP配置文件進行編輯:sudo vim /etc/php.ini
找到或添加以下配置項,并根據需要修改它們的值:post_max_size:設置為至少16M。post_max_size = 16M
max_execution_time:設置為至少300秒。max_execution_time = 300
max_input_time:設置為至少300秒max_input_time = 300
此外,為了確保上傳文件大小也足夠大,建議同時設置upload_max_filesize為至少16M(這與post_max_size相關聯):upload_max_filesize = 16M
記得重啟PHP-FPM服務以使更改生效:sudo systemctl restart php-fpm