注:本文适用于不开放外网环境下zabbix5.4.9的编译安装方式。
软件介绍:centos7.2、zabbix5.4.9、mysql5.7.36、apache2.4.50、php7.4.8
软件版本选择是根据zabbix官方推荐方式,Zabbix 5.4需要MySQL版本5.7.28 - 8.0.X之间,PHP7.2.5 or later但不支持8.0,Apache1.3.12 or later。
一、搭建本地yum源仓库
由于安装操作系统使用了最小化安装的方式,而编译安装方式又依赖很多软件包,故采用本地yum源来安装依赖软件包。
虚拟机光盘选用CentOS-7.2-x86_64-Everything-1511.iso的ISO镜像
安装一些必备工具:
yum -y install net-tools vim unzip zip wget curl lrzsz gcc gcc-c++ make autoconf automake htop iftop sysstat lsof tcpdump nmap dstat iotop strace
修改一下主机名称:
[root@localhost ~]# hostname zabbix [root@localhost ~]# echo "zabbix" > /etc/hostname
安装配置httpd服务:
[root@zabbix ~]# cd /usr/local/src/
[root@zabbix src]# ls -l *.gz |awk '{print $9}'
apr-1.6.5.tar.gz
apr-util-1.6.1.tar.gz
httpd-2.4.50.tar.gz
jpegsrc.v9e.tar.gz
libzip-1.3.2.tar.gz
mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
php-7.4.8.tar.gz
[root@zabbix src]#
[root@zabbix src]# tar zxf httpd-2.4.50.tar.gz
[root@zabbix src]# mkdir httpd-2.4.50/srclib/{apr,apr-util}
[root@zabbix src]# tar zxf apr-1.6.5.tar.gz -C httpd-2.4.50/srclib/apr --strip-components 1
[root@zabbix src]# tar zxf apr-util-1.6.1.tar.gz -C httpd-2.4.50/srclib/apr-util --strip-components 1
[root@zabbix src]# cd httpd-2.4.50
[root@zabbix httpd-2.4.50]# ./configure --prefix=/opt/apache --with-zlib --with-pcre --with-included-apr --with-mpm=prefork --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all
[root@zabbix httpd-2.4.50]# make && make install
创建本地yum源仓库和repo文件
[root@zabbix Packages]# cd /etc/yum.repos.d/ [root@zabbix yum.repos.d]# mkdir -p /opt/apache/htdocs/centos/7/os/x86_64 [root@zabbix yum.repos.d]# cp -ar /mnt/Packages /opt/apache/htdocs/centos/7/os/x86_64/ [root@zabbix yum.repos.d]# cp -ar /mnt/repodata /opt/apache/htdocs/centos/7/os/x86_64/ [root@zabbix yum.repos.d]# cp -ar /mnt/RPM-GPG-KEY-CentOS-7 /opt/apache/htdocs/centos/7/os/x86_64/ [root@zabbix yum.repos.d]# mkdir backup [root@zabbix yum.repos.d]# mv *.repo backup/ [root@zabbix yum.repos.d]# vim CentOS-Local.repo [localbase] name=LocalBase baseurl=http://192.168.xxx.xxx/centos/7/os/x86_64/ gpgcheck=1 gpgkey=file:///opt/apache/htdocs/centos/7/os/x86_64/RPM-GPG-KEY-CentOS-7 [root@zabbix yum.repos.d]# yum clean all [root@zabbix yum.repos.d]# yum makecache [root@zabbix yum.repos.d]# [root@zabbix yum.repos.d]# yum repolist [root@zabbix yum.repos.d]#
二、安装配置MySQL数据库
MySQL数据库的安装可以采用RPM包、源码包和基于特定glibc版本编译的包。这里我使用glibc版的包安装。
[root@zabbix yum.repos.d]# cd /usr/local/src/ # 检查系统是否安装mariadb [root@zabbix src]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x86_64 # 卸载系统自带的mariadb-libs [root@zabbix src]# yum -y remove mariadb-libs # 安装依赖软件包libaio、libaio-devel [root@zabbix src]# yum -y install libaio libaio-devel # 检查系统是否有mysql用户 [root@zabbix src]# id mysql id: mysql: no such user # 创建mysql组和用户,禁止用户登录系统shell [root@zabbix src]# groupadd mysql [root@zabbix src]# useradd -r -g mysql -s /sbin/nologin mysql
# 解压缩数据库文件,并且修改属组属主权限 [root@zabbix src]# tar zxf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz [root@zabbix src]# mv mysql-5.7.36-linux-glibc2.12-x86_64 /opt/mysql [root@zabbix src]# mkdir /opt/mysql/data [root@zabbix src]# chown -R mysql:mysql /opt/mysql # 初始化数据库 [root@zabbix src]# /opt/mysql/bin/mysqld --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data --initialize # 系统生成的随机密码要记下来 2022-03-07T09:01:20.749697Z 1 [Note] A temporary password is generated for root@localhost: ytuwzl*1+jqS # 配置mysql服务启动程序 [root@zabbix src]# cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld [root@zabbix src]# sed -i 's/^basedir=/basedir=\/opt\/mysql/g' /etc/init.d/mysqld [root@zabbix src]# sed -i 's/^datadir=/datadir=\/opt\/mysql\/data/g' /etc/init.d/mysqld # 将mysql的程序目录添加到环境变量 [root@zabbix src]# sed -i '/^export PATH/i\PATH=$PATH:/opt/mysql/bin' /etc/profile [root@zabbix src]# source /etc/profile # 将mysql的lib库添加到共享链接库文件 [root@zabbix src]# echo "/opt/mysql/lib" >> /etc/ld.so.conf [root@zabbix src]# ldconfig # 启动mysql数据库 [root@zabbix src]# /etc/init.d/mysqld start Starting MySQL.Logging to '/opt/mysql/data/zabbix.err'. SUCCESS! [root@zabbix src]# # 登入mysql数据库,首次登录必须修改口令,创建zabbix数据库并授权用户权限 [root@zabbix src]# mysql -u root -p'ytuwzl*1+jqS' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.36 Copyright (c) 2000, 2021, 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> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec) mysql> CREATE DATABASE IF NOT EXISTS zabbix CHARACTER SET utf8 COLLATE utf8_bin; Query OK, 1 row affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'zabbix'@'%' IDENTIFIED BY 'xxxxxx' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> QUIT Bye [root@zabbix src]#
三、安装配置PHP
zabbix官方列出的PHP扩展有如下列表:
接下来安装php扩展软件包和编译安装php:
# yum安装php扩展所需要软件包
[root@zabbix src]# yum -y install gd gd-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel gettext gettext-devel openldap openldap-devel libtool libtool-ltdl libtool-ltdl-devel sqlite sqlite-devel libwebp libwebp-devel
# 安装oniguruma、oniguruma-devel
[root@zabbix src]# rpm -ivh oniguruma-6.8.2-1.el7.x86_64.rpm
[root@zabbix src]# rpm -ivh oniguruma-devel-6.8.2-1.el7.x86_64.rpm
# 安装libcurl-devel
[root@zabbix src]# rpm -ivh libcurl-devel-7.29.0-59.el7_9.1.x86_64.rpm
# 安装openssl、openssl-devel
[root@zabbix src]# rpm -ivh openssl-1.0.2k-24.el7_9.x86_64.rpm
[root@zabbix src]# yum -y install openssl-devel-1.0.2k-24.el7_9.x86_64.rpm
# 编译安装libzip,如果系统自带了低版本的要卸载掉。
[root@zabbix src]# tar zxf libzip-1.3.2.tar.gz
[root@zabbix src]# cd libzip-1.3.2
[root@zabbix libzip-1.3.2]# ./configure
[root@zabbix libzip-1.3.2]# make && make install
# 编译安装jpeg9
[root@zabbix src]# tar zxf jpegsrc.v9e.tar.gz
[root@zabbix src]# cd jpeg-9e
[root@zabbix jpeg-9e]# mkdir /usr/local/jpeg9/{bin,lib,include} -p
[root@zabbix jpeg-9e]# ./configure --prefix=/usr/local/jpeg9 --enable-shared
[root@zabbix jpeg-9e]# make
[root@zabbix jpeg-9e]# make install
# 声明jpeg9的pkgconfig路径
[root@zabbix jpeg-9e]# export PKG_CONFIG_PATH=/usr/local/jpeg9/lib/pkgconfig
# 编译安装php
[root@zabbix src]# cd php-7.4.8
# 将ldap的动态链接库文件拷贝到/usr/lib下
[root@zabbix php-7.4.8]# cp -ar /usr/lib64/libldap* /usr/lib/
# 声明libzip的pkgconfig路径
[root@zabbix php-7.4.8]# export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
[root@zabbix php-7.4.8]# echo $PKG_CONFIG_PATH
/usr/local/jpeg9/lib/pkgconfig:/usr/local/lib/pkgconfig
[root@zabbix php-7.4.8]#
[root@zabbix php-7.4.8]# ./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --with-apxs2=/opt/apache/bin/apxs --with-mysql-sock=/tmp/mysql.sock --with-mysqli=/opt/mysql/bin/mysql_config --with-mysqli=mysqlnd --with-jpeg --with-freetype --with-gettext --with-xmlrpc --with-curl --with-openssl --with-mhash --with-pdo-mysql --with-ldap --with-ldap-sasl --with-zip --with-webp --enable-bcmath --enable-sockets --enable-mbstring --enable-shmop --enable-mysqlnd --enable-ftp --enable-soap --enable-pcntl --enable-sysvsem --enable-gd
# php的Makefile需要添加lber的extra_libs
[root@zabbix php-7.4.8]# sed -i '/^EXTRA_LIBS/s/$/ -llber/g' Makefile
[root@zabbix php-7.4.8]# make && make install
# 整合apache和php
[root@zabbix php-7.4.8]# sed -i 's/^#ServerName www.example.com/ServerName localhost/g' /opt/apache/conf/httpd.conf
[root@zabbix php-7.4.8]# sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' /opt/apache/conf/httpd.conf
[root@zabbix php-7.4.8]# sed -i '/application\/x-gzip .gz/a\ AddType application\/x-httpd-php .php' /opt/apache/conf/httpd.conf
[root@zabbix php-7.4.8]# sed -i '/application\/x-httpd-php/a\ AddType application/x-httpd-php-source .php7' /opt/apache/conf/httpd.conf
[root@zabbix php-7.4.8]# mkdir /opt/php/etc
[root@zabbix php-7.4.8]# cp php.ini-production /opt/php/etc/php.ini
[root@zabbix php-7.4.8]# sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /opt/php/etc/php.ini
[root@zabbix php-7.4.8]# sed -i 's/post_max_size = 8M/post_max_size = 16M/g' /opt/php/etc/php.ini
[root@zabbix php-7.4.8]# sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /opt/php/etc/php.ini
[root@zabbix php-7.4.8]# sed -i 's/max_input_time = 60/max_input_time = 300/g' /opt/php/etc/php.ini
# 重启htppd服务,使整合生效
[root@zabbix php-7.4.8]# /opt/apache/bin/apachectl -k restart
[root@zabbix php-7.4.8]# more /opt/apache/htdocs/info.php
<?php
echo phpinfo();
?>
[root@zabbix php-7.4.8]#
访问phpinfo页面,验证可用性以及扩展。
四、安装配置zabbix server
zabbix官方列出的软件需求列表如下:
其中,libpcre、zip、libcurl和libxml2都已经安装过了,接下来,我们只需要安装libevent、openipmi和net-snmp即可。
# 安装需求软件包 [root@zabbix src]# yum -y install libevent libevent-devel net-snmp net-snmp-devel OpenIPMI OpenIPMI-devel # 编译安装zabbix server和agent [root@zabbix zabbix-5.4.9]# ./configure --prefix=/opt/zabbix --enable-server --enable-agent --with-openipmi --with-libcurl --with-mysql --with-net-snmp --with-libcurl --with-libxml2 [root@zabbix zabbix-5.4.9]# make && make install # 将Frontend页面文件拷贝到httpd的网站根目录 [root@zabbix zabbix-5.4.9]# cp -ar ui /opt/apache/htdocs/zabbix # 创建zabbix的用户和组,并更改Frontend的属主属组权限 [root@zabbix zabbix-5.4.9]# groupadd zabbix [root@zabbix zabbix-5.4.9]# useradd -g zabbix -s /sbin/nologin zabbix [root@zabbix zabbix-5.4.9]# chown -R zabbix:zabbix /opt/apache/htdocs/zabbix # 初始化zabbix数据库 [root@zabbix zabbix-5.4.9]# mysql -uzabbix -pxxxxxx zabbix < database/mysql/schema.sql [root@zabbix zabbix-5.4.9]# mysql -uzabbix -pxxxxxx zabbix < database/mysql/images.sql [root@zabbix zabbix-5.4.9]# mysql -uzabbix -pxxxxxx zabbix < database/mysql/data.sql [root@zabbix zabbix-5.4.9]# # 修改zabbix server的配置 [root@zabbix zabbix-5.4.9]# cd /opt/zabbix/etc/ [root@zabbix etc]# sed -i 's/^# ListenPort=/ListenPort=/g' zabbix_server.conf [root@zabbix etc]# sed -i 's/^# LogType=/LogType=/g' zabbix_server.conf [root@zabbix etc]# sed -i 's/^# PidFile=/PidFile=/g' zabbix_server.conf [root@zabbix etc]# sed -i 's/^# DBHost=/DBHost=/g' zabbix_server.conf [root@zabbix etc]# sed -i 's/^# DBPassword=/DBPassword=zabbix/g' zabbix_server.conf [root@zabbix etc]# sed -i 's/^# DBPort=/DBPort=3306/g' zabbix_server.conf # 修改zabbix agent的配置 [root@zabbix etc]# sed -i 's/^# PidFile=/PidFile=/g' zabbix_agentd.conf [root@zabbix etc]# sed -i 's/^# LogType=/LogType=/g' zabbix_agentd.conf [root@zabbix etc]# sed -i 's/^# ListenPort=/ListenPort=/g' zabbix_agentd.conf [root@zabbix etc]# sed -i 's/^# ListenIP=/ListenIP=/g' zabbix_agentd.conf # 启动zabbix server和zabbix agent服务 [root@zabbix etc]# /opt/zabbix/sbin/zabbix_server -c /opt/zabbix/etc/zabbix_server.conf [root@zabbix etc]# /opt/zabbix/sbin/zabbix_agentd -c /opt/zabbix/etc/zabbix_agentd.conf
现在通过web界面安装配置zabbix
默认使用英文进行配置,可以选择中文,这里我默认了。
检查环境需求是否满足需要,都是绿色就表示满足。
配置数据库,根据实际情况填写端口,用户,数据库和密码等
这里需要输入系统的名称。
默认时区和主题的选择
预安装汇总,确认信息
将配置文件下载下来,上传到服务器的指定目录
[root@zabbix etc]# cd /opt/apache/htdocs/zabbix/conf/ [root@zabbix etc]# ls /opt/apache/htdocs/zabbix/conf/zabbix.conf.php /opt/apache/htdocs/zabbix/conf/zabbix.conf.php
zabbix安装已经完成。接下来,登录zabbix server
默认的用户名和密码是:
用户:Admin
密码:zabbix
登录进去后将界面语言改为中文,然后解决图形中文乱码的问题
[root@zabbix ~]# cd /opt/apache/htdocs/zabbix/assets/fonts [root@zabbix fonts]# mv DejaVuSans.ttf DejaVuSans.ttf_default && ln -s simkai.ttf DejaVuSans.ttf
刷新页面查看
至此,zabbix5.4.9安装配置已经全部完成。
问题列表:
其他可能会存在的问题:
1、如果PHP的编译参数包含--enable-gd-jis-conv的话,那么zabbix5.4的图形中文乱码问题,除了要替换字体外,还需要修改include/graphs.inc.php这个文件。
具体修改方法如下:
function imageText($image, $fontsize, $angle, $x, $y, $color, $string) { if ((preg_match(ZBX_PREG_DEF_FONT_STRING, $string) && $angle != 0) || ZBX_FONT_NAME == ZBX_GRAPH_FONT_NAME) { $ttf = ZBX_FONTPATH.'/'.ZBX_FONT_NAME.'.ttf'; imagettftext($image, $fontsize, $angle, $x, $y, $color, $ttf, to_entities($string)); } elseif ($angle == 0) { $ttf = ZBX_FONTPATH.'/'.ZBX_GRAPH_FONT_NAME.'.ttf'; imagettftext($image, $fontsize, $angle, $x, $y, $color, $ttf, to_entities($string)); } else { $ttf = ZBX_FONTPATH.'/'.ZBX_GRAPH_FONT_NAME.'.ttf'; $size = imageTextSize($fontsize, 0, $string); $imgg = imagecreatetruecolor($size['width'] + 1, $size['height']); $transparentColor = imagecolorallocatealpha($imgg, 200, 200, 200, 127); imagefill($imgg, 0, 0, $transparentColor); imagettftext($imgg, $fontsize, 0, 0, $size['height'], $color, $ttf, to_entities($string)); $imgg = imagerotate($imgg, $angle, $transparentColor); imagealphablending($imgg, false); imagesavealpha($imgg, true); imagecopy($image, $imgg, $x - $size['height'], $y - $size['width'], 0, 0, $size['height'], $size['width'] + 1); imagedestroy($imgg); } }
给函数imageText中所有行尾的$string加个to_entities函数,然后在函数imageText下方定义to_entities函数,如下:
# 解决zabbix图形界面中文乱码的问题 function to_entities($string){ $len = strlen($string); $buf = ""; for($i = 0; $i < $len; $i++){ if (ord($string[$i]) <= 127){ $buf .= $string[$i]; } else if (ord ($string[$i]) <192){ //unexpected 2nd, 3rd or 4th byte $buf .= "?"; } else if (ord ($string[$i]) <224){ //first byte of 2-byte seq $buf .= sprintf("&#%d;", ((ord($string[$i + 0]) & 31) << 6) + (ord($string[$i + 1]) & 63) ); $i += 1; } else if (ord ($string[$i]) <240){ //first byte of 3-byte seq $buf .= sprintf("&#%d;", ((ord($string[$i + 0]) & 15) << 12) + ((ord($string[$i + 1]) & 63) << 6) + (ord($string[$i + 2]) & 63) ); $i += 2; } else { //first byte of 4-byte seq $buf .= sprintf("&#%d;", ((ord($string[$i + 0]) & 7) << 18) + ((ord($string[$i + 1]) & 63) << 12) + ((ord($string[$i + 2]) & 63) << 6) + (ord($string[$i + 3]) & 63) ); $i += 3; } } return $buf; }
保存文件,刷新浏览器,乱码问题即可解决。