zabbix--CentOS7 原始碼安裝Zabbix6 LTS版本
環境說明
這裡使用為 CentOS 7.6
版本進行測試驗證,Zabbix Server
採用原始碼包部署,資料庫採用 MySQL8.0
版本,zabbix-web
使用 nginx+php
來實現。具體資訊如下:
軟體名 | 版本 | 安裝方式 |
---|---|---|
Zabbix Server | 6.0.3 | 原始碼安裝 |
Zabbix Agent | 6.0.3 | 原始碼安裝 |
MySQL | 8.0.28 | yum安裝 |
Nginx | 1.20.1 | yum安裝 |
Php | 7.4.29 | yum安裝 |
官網參考文件:https://www.zabbix.com/documentation/6.0/en/manual/installation/install
安裝MySQL
1、下載軟體包
cat << EOF >download.sh #!/bin/bash wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-client-8.0.28-1.el7.x86_64.rpm wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-devel-8.0.28-1.el7.x86_64.rpm wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-embedded-compat-8.0.28-1.el7.x86_64.rpm wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-libs-8.0.28-1.el7.x86_64.rpm wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-libs-compat-8.0.28-1.el7.x86_64.rpm wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-server-8.0.28-1.el7.x86_64.rpm wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-common-8.0.28-1.el7.x86_64.rpm wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-8.0/mysql-community-icu-data-files-8.0.28-1.el7.x86_64.rpm EOF bash download.sh
2、安裝
yum localinstall ./mysql-community-* -y
3、修改配置檔案(更改資料存放路徑)
mkdir /data/mysql -p # grep -v -E '^#|^$' /etc/my.cnf [mysqld] port=3306 default-storage-engine=InnoDB max_connections=1200 datadir=/data/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid log_slave_updates=ON log_bin=mysql-bin binlog_format=ROW
4、啟動並加入開機自啟
systemctl enable --now mysqld
5、修改密碼
# grep password /var/log/mysqld.log
2022-04-13T07:31:54.168545Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: RP-u?qo.,4hD
# mysql -uroot -p
Enter password:
......
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY "Admin@zaq213";
Query OK, 0 rows affected (0.01 sec)
6、建立初始資料庫及使用者
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> create user 'zabbix'@'%' identified by 'Zabbix';
mysql> grant all privileges on zabbix.* to 'zabbix'@'%' with grant option;
mysql> alter user 'zabbix'@'%' identified with mysql_native_password by 'Zabbix';
mysql> select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| zabbix | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)
安裝Zabbix
下載軟體包:https://www.zabbix.com/cn/download_sources
1、解壓軟體包
tar -zxvf zabbix-6.0.3.tar.gz
2、建立 zabbix
使用者
groupadd --system zabbix
useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix
3、初始化資料(進入解壓目錄下的 database
目錄下)
cd zabbix-6.0.3/database/mysql
mysql -uzabbix -pZabbix zabbix < schema.sql
mysql -uzabbix -pZabbix zabbix < images.sql
mysql -uzabbix -pZabbix zabbix < data.sql
4、安裝編譯所需的依賴,這裡根據啟用的模組不一樣,所需要的依賴也不一樣
yum install gcc mysql-devel libevent-devel libcurl-devel libxml2-devel libssh2-devel OpenIPMI-devel net-snmp-devel go java-devel -y
5、進行編譯安裝,去到軟體包解壓目錄zabbix-6.0.3
中
cd zabbix-6.0.3
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2 --with-openipmi --with-ssh2 --enable-java
配置完成如下圖所示,這裡可以看到我們啟用了java gateway
、agent
等等
接著進行安裝
make install
安裝完成後,可以看下所安裝的目錄的內容
# tree /usr/local/zabbix/
/usr/local/zabbix/
├── bin
│ ├── zabbix_get
│ ├── zabbix_js
│ └── zabbix_sender
├── etc
│ ├── zabbix_agent2.conf
│ ├── zabbix_agentd.conf
│ ├── zabbix_agentd.conf.d
│ ├── zabbix_server.conf
│ └── zabbix_server.conf.d
├── lib
│ └── modules
├── sbin
│ ├── zabbix_agentd
│ ├── zabbix_java
│ │ ├── bin
│ │ │ └── zabbix-java-gateway-6.0.3.jar
│ │ ├── lib
│ │ │ ├── android-json-4.3_r3.1.jar
│ │ │ ├── logback-classic-1.2.9.jar
│ │ │ ├── logback-console.xml
│ │ │ ├── logback-core-1.2.9.jar
│ │ │ ├── logback.xml
│ │ │ └── slf4j-api-1.7.32.jar
│ │ ├── settings.sh
│ │ ├── shutdown.sh
│ │ └── startup.sh
│ └── zabbix_server
└── share
├── man
│ ├── man1
│ │ ├── zabbix_get.1
│ │ └── zabbix_sender.1
│ └── man8
│ ├── zabbix_agentd.8
│ └── zabbix_server.8
└── zabbix
├── alertscripts
└── externalscripts
17 directories, 22 files
6、修改配置檔案
cp /usr/local/zabbix/etc/zabbix_server.conf{,.bak}
# grep -v -E "^#|^$" /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/usr/local/zabbix/log/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=127.0.0.1
DBPort=3306
DBName=zabbix
DBUser=zabbix
DBPassword=Zabbix
StartPollers=12
StartTrappers=30
StartDiscoverers=50
StartHTTPPollers=100
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
CacheSize=4G
CacheUpdateFrequency=1200
ValueCacheSize=12G
Timeout=4
AlertScriptsPath=/usr/local/zabbix/alertscripts
ExternalScripts=/usr/local/zabbix/externalscripts
FpingLocation=/sbin/fping
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
7、建立相關配置目錄及許可權設定
mkdir -p /usr/local/zabbix/{alertscripts,externalscripts,log}
mkdir /var/run/zabbix
chown zabbix.zabbix /var/run/zabbix/
chown zabbix.zabbix /usr/local/zabbix/ -R
8、配置systemd啟動
cat <<EOF > /etc/systemd/system/zabbix-server.service
[Unit]
Description=Zabbix Server
After=syslog.target network.target network-online.target
Wants=network.target network-online.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/zabbix/sbin/zabbix_server -c /usr/local/zabbix/etc/zabbix_server.conf
RemainAfterExit=yes
PIDFile=/var/run/zabbix/zabbix_server.pid
[Install]
WantedBy=multi-user.target
EOF
9、加入開機啟動並啟動
systemctl daemon-reload
systemctl enable zabbix-server --now
安裝zabbix-web
這裡採用
nginx+php
部署來支撐zabbix-web
,這樣在後期如果有漏洞,也方便對應升級維護,如nginx
有漏洞,可以直接把nginx
升級即可。
安裝php
1、配置yum
,這裡使用清華大學的yum
源
yum install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm -y
2、安裝php
yum install php74-php php74-php-fpm php74-php-ldap php74-php-bcmath php74-php-mbstring php74-php-common php74-php-gd php74-php-mysql php74-php-xml php74-php-cli php74-php-devel php74-php-pecl-memcached php74-php-pecl-redis php74-php-opcache -y
3、配置php.ini
,(使用清華大學yum
安裝的php
配置檔案路徑在/etc/opt/remi/php74/
)
cp /etc/opt/remi/php74/php.ini{,.bak}
vim /etc/opt/remi/php74/php.ini
[PHP] #該標籤已有,只是將下列引數加到(或修改)該標籤下。
expose_php = Off #禁止顯示php版本的資訊
short_open_tag = On #取消前面的;,修改為On。表示支援php短標籤
cgi.fix_pathinfo=1 #將註釋去掉,開啟PHP的pathinfo偽靜態功能。
max_execution_time = 300 #指令碼執行的最長時間,預設30秒
max_input_time = 300 #指令碼可以消耗的時間,預設60秒
memory_limit = 256M #指令碼執行最大消耗的記憶體,根據你的需求更改數值,預設128M
post_max_size = 100M #單提交的最大資料,此項不是限制上傳單個檔案的大小,而是針對整個表單的提交資料進行限制的。限制範圍包括表單提交的所有內容.例如:發表貼子時,貼子標題,內容,附件等…預設8
upload_max_filesize = 10M #上載檔案的最大許可大小 ,預設2M
[Date] #該標籤已有,只是修改下方的時區
date.timezone = Asia/Shanghai
4、配置php-fpm.conf
vim /etc/opt/remi/php74/php-fpm.conf
include=/etc/opt/remi/php74/php-fpm.d/*.conf
error_log = /var/opt/remi/php74/log/php-fpm/error.log
5、啟動php
並加入開機啟動
systemctl enable --now php74-php-fpm.service
備註:如果需要解除安裝老版本php
可以使用以下命令進行解除安裝
rpm -qa |grep php|xargs -i rpm -e {} --nodeps
安裝nginx
1、安裝yum
源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2、安裝nginx
yum install nginx -y
3、將 Zabbix
原始碼包中的 PHP
檔案複製到 Nginx
的根目錄下。
mkdir -p /usr/share/nginx/html/zabbix
cp -r zabbix-6.0.3/ui/* /usr/share/nginx/html/zabbix/
4、配置nginx
cp /etc/nginx/nginx.conf{,.bak}
# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html/zabbix;
index index.php index.html index.htm;
include /etc/nginx/default.d/*.conf;
location ~ \.php$ {
root /usr/share/nginx/html/zabbix;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
5、啟動nginx
並加入開機啟動
systemctl enable --now nginx
到這裡就可以進行訪問配置的地址和埠進行 zabbix-web
安裝了
Web介面配置
1、訪問Web介面,如下圖,並選擇自己所使用的語言
2、基礎環境檢測
3、配置資料庫連線資訊
4、配置主機名稱和時區
5、檢查配置
6、如果出現下圖,按照圖中提示,下載檔案,儲存到對應提示路徑中。
7、配置完成後如下圖所示,點選完成。
8、進入登陸介面(預設賬號/密碼:Admin/zabbix)
啟用zabbix-agent
啟用 zabbix-server
伺服器本身的 agnet
,這裡我們上面安裝的時候已經把模組安裝上了,這裡只需要配置下啟動即可。
這裡由於在一臺機器,不用修改什麼,直接啟動即可
1、配置systemd
cat <<EOF > /etc/systemd/system/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=syslog.target network.target network-online.target
Wants=network.target network-online.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
2、加入開機啟動並啟動
systemctl daemon-reload
systemctl enable zabbix-agent --now
3、檢視狀態
# systemctl status zabbix-agent
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/etc/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
Active: active (exited) since Wed 2022-04-13 18:56:50 CST; 7s ago
Process: 2243 ExecStart=/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf (code=exited, status=0/SUCCESS)
Main PID: 2243 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/zabbix-agent.service
├─2245 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
├─2246 /usr/local/zabbix/sbin/zabbix_agentd: collector [idle 1 sec]
├─2247 /usr/local/zabbix/sbin/zabbix_agentd: listener #1 [waiting for connection]
├─2248 /usr/local/zabbix/sbin/zabbix_agentd: listener #2 [waiting for connection]
├─2249 /usr/local/zabbix/sbin/zabbix_agentd: listener #3 [waiting for connection]
└─2250 /usr/local/zabbix/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Apr 13 18:56:50 test systemd[1]: Started Zabbix Agent.
4、再次檢查狀態(此時可以看到可用的機器數為1了)