Installing Zabbix for Monitoring from Source Code - Compilation and Installation Guide

I’ve noticed that I have a lot of servers now. I successfully obtained a server from Oracle (they’re really generous, giving me a 4-core 24GB server just like that). Since I have so many servers, I think it’s necessary to monitor them. I’ve used Nagios at work before, so this time I’m going to use Zabbix to monitor all my servers. This article will describe how to install Zabbix from source code, and the next article will cover how to monitor other servers and encrypt communication.

Sigh, it’s 520 time of the year again. The handsome guys and beautiful girls are all in pairs, while us singletons can only write documents and deepen our memories on our own. Oh well, I’m used to it…(English version Translated by GPT-3.5, 返回中文)

Environment Setup

I will install Zabbix server on my own Alibaba Cloud. Since I bought a Tencent Cloud server last month, my Alibaba Cloud has become my local area network (LAN) tunneling machine. This article will document the compilation and installation of Zabbix, and the subsequent article will cover the monitoring and installation process on the Oracle server. This article only covers the installation completion part.

1
2
3
4
5
6
服务器:Aliyun(China) 1 core 1G 2M bandwidth
系统:Ubuntu 20.04
Kernel: 5.4.0-110-generic
数据库:Aliyun RDS (自己准备一个MySQL数据库,我用的是MySQL 8.0)
Docker:apt安装最新版本
下面文本中,数据库名称 rt_zabbix,myzabbix,zabbix视为同一个数据库,因为这篇文章我不是一口气写完的,为了最大程度安全,因此所有表单信息,数据库信息均经过修改,公网环境我也不会使用root/root这么简单的密码。

Download the Source Code

The first step is, of course, to download the source code. I’m not comfortable with using the latest version zabbix-6.0.4.tar.gz - zabbix.com. I downloaded it to the server.

1
wget -c https://cdn.zabbix.com/zabbix/sources/stable/6.0/zabbix-6.0.4.tar.gz

Console content:

1
2
3
4
5
--2022-05-19 19:39:06--  https://cdn.zabbix.com/zabbix/sources/stable/6.0/zabbix-6.0.4.tar.gz
......
zabbix-6.0.4.tar.gz 100%[===================>] 31.86M 11.6MB/s in 2.8s

2022-05-19 19:39:10 (11.6 MB/s) - ‘zabbix-6.0.4.tar.gz’ saved [33412454/33412454

Then, unzip it.

1
2
tar -zxvf zabbix-6.0.4.tar.gz 
cd zabbix-6.0.4

Compile the Source Code - Configure

Following the instructions in the official documentation - 17.Encryption - zabbix.com, use the following content to configure the source code for the Zabbix server and agent proxies using OpenSSL. I need to add the following parameters during compilation.

1
./configure --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2 --with-openssl

So, let’s run it. I like to add –prefix at the end, so that the installed content will be in one directory, making it easier to delete if needed.

1
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2 --with-openssl

After waiting for a while, I received a prompt.

1
2
3
4
5
6
7
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... no
...
checking for mariadb_config... no
configure: error: MySQL library not found

It seems that the MySQL library is missing, so let’s install it.

1
apt install -y libmysqlclient-dev

Run the configure command again and received another prompt.

1
2
3
4
5
6
......
checking for multirow insert statements... yes
checking for pkg-config... no
checking for pkg-config... no
configure: error: LIBXML2 library not found
configure: error: LIBXML2 library not found

So…

1
apt install -y libxml2-dev

Run it again… still missing? I looked it up and found this Compiling Zabbix 5.2 with libxml2 support. The first answer mentioned that I need to install pkg-config. Coincidentally, the error message for libxml2 says checking for pkg-config... no, so let’s install that.

1
apt install -y pkg-config

Indeed, the error changed to:

1
2
3
4
5
6
......
checking pkg-config is at least version 0.9.0... yes
checking for xmlReadMemory in -lxml2... yes
checking for net-snmp-config... no
configure: error: Invalid Net-SNMP directory - unable to find net-snmp-config
root@iZbp12wn36q08um7s5aap5Z:~/download/zabbix-6.0.4# apt search Net-SNMP

Try again.

1
apt install -y libsnmp-dev

Prompt…

1
2
3
.....
checking for libevent support... no
configure: error: Unable to use libevent (libevent check failed)

Install…

1
apt install -y libevent-dev

Prompt…

1
2
3
4
.....
checking for gawk... (cached) mawk
checking for curl-config... no
configure: error: Curl library not found

Install…

1
apt install -y libcurl4-openssl-dev

The pre-compiled environment configuration is completed when you see the following information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Configuration:

Detected OS: linux-gnu
Install path: /usr/local/zabbix
Compilation arch: linux

Compiler: cc
Compiler flags: -g -O2

Library-specific flags:
database: -I/usr/include/mysql
libXML2: -I/usr/include/libxml2
Net-SNMP: -I/usr/local/include -I/usr/lib/x86_64-linux-gnu/perl/5.30/CORE -I. -I/usr/include
TLS: -I/usr/include

Enable server: yes
Server details:
With database: MySQL
WEB Monitoring: cURL
SSL certificates: /usr/local/zabbix/share/zabbix/ssl/certs
SSL keys: /usr/local/zabbix/share/zabbix/ssl/keys
SNMP: yes
IPMI: no
SSH: no
TLS: OpenSSL
ODBC: no
Linker flags: -L/usr/lib/x86_64-linux-gnu -L/usr/lib/x86_64-linux-gnu -L/usr/lib -rdynamic
Libraries: -lmysqlclient -lpthread -ldl -lz -lssl -lcrypto -lresolv -lm -lrt -lnetsnmp -lz -lpthread -levent -lssl -lcrypto -lcurl -lm -ldl -lresolv -lxml2 -lpcre
Configuration file: /usr/local/zabbix/etc/zabbix_server.conf
External scripts: /usr/local/zabbix/share/zabbix/externalscripts
Alert scripts: /usr/local/zabbix/share/zabbix/alertscripts
Modules: /usr/local/zabbix/lib/modules

Enable proxy: no

Enable agent: yes
Agent details:
TLS: OpenSSL
Modbus: no
Linker flags: -L/usr/lib -rdynamic
Libraries: -lz -lpthread -lssl -lcrypto -lcurl -lm -ldl -lresolv -lxml2 -lpcre
Configuration file: /usr/local/zabbix/etc/zabbix_agentd.conf
Modules: /usr/local/zabbix/lib/modules

Enable agent 2: no

Enable web service: no

Enable Java gateway: no

LDAP support: no
IPv6 support: yes

***********************************************************
* Now run 'make install' *
* *
* Thank you for using Zabbix! *
* <http://www.zabbix.com> *
***********************************************************

Execute Compilation and Installation - Make

Compilation time: approximately 2 minutes and 34 seconds

1
make -j1

The -jN option specifies how many cores to use for compilation. Using -j alone indicates using all cores. If the memory is smaller, it may cause OOM failure. This step may take a while, so get a cup of coffee and take it slow.

Import the Database

According to the instructions in the official documentation - 1 Create database - zabbix.com, the Zabbix MySQL server must run the init script.

Create the Database

You can first take a look at the official documentation - 1 Create database - zabbix.com, and then proceed with creating and importing the database.

If Using Alibaba Cloud RDS

I will be using Alibaba Cloud RDS MySQL for Zabbix, so I will follow these steps. Note that the character set should be set to utf8mb4 or utf8.
Create a new utf8mb4 database
Then, grant permissions to this newly created database on the left side. Let’s assume the database name is rt_zabbix, even though I later changed it to myzabbix.
Grant account permissions
Next, connect to the database using a tool (I use Navicat to connect to RDS). You should see that Zabbix is already using this account. Here, we need to make some modifications because the default database created by RDS uses the utf8_general_ci collation rule, but we need to change it to utf8_bin. The reason is mentioned in the official documentation:

1
Zabbix Server/Proxy 完美适配 MySQL 数据库的重要前提是 MySQL 数据库的字符集必须使用 utf8(又名 utf8mb3)或 utf8mb4(分别使用 utf8_bin 和 utf8mb4_bin 排序规则)。建议全新安装时使用 utf8mb4 。

Modify database collation

If Using a Self-Deployed Database

I won’t attempt this step.

1
2
3
4
mysql -uroot -p<password>
create database zabbix character set utf8mb4 collate utf8mb4_bin;
create user 'zabbix'@'localhost' identified by '<password>';
grant all privileges on zabbix.* to 'zabbix'@'localhost';

Import the Data

If you are installing Zabbix from source code, continue with the following steps to import the data into the database. These database files are located in the /zabbix-6.0.4/database/mysql directory in the Zabbix compile directory (not in the final installation directory /usr/local/zabbix).
Copy the [schema.sql, images.sql, data.sql] files (note that the SQL files must be imported in the order mentioned) from the Zabbix compile directory (/root/download/zabbix-6.0.4) to your local machine. You can use Navicat to import them (the file size is 32MB, it is recommended to compress them into a zip file if you are sending them, or you can import them directly if you have a local MySQL server).

After importing, there should be a total of 173 tables. I did not encounter any errors during the import process.
Zabbix tables

Configure Zabbix

1
vi /usr/local/zabbix/etc/zabbix_server.conf

Modify the first section (skip if the database is local).

1
DBHost=你的数据库地址

Modify the second section.

1
DBName=创建的zabbix数据库,我这里假设是 myzabbix,上面由于后来改过数据库名了,从rt_zabbix改成了myzabbix

Modify the third section.

1
DBUser=数据库用户名,假设这里是 root

Modify the fourth section.

1
DBPassword=你的密码

Other configurable options.

1
2
3
ListenPort=10051 #这里修改其zabbix与agent通讯端口,默认是10051
DBPort=3306 # 默认注释

Start the Server and Agent

1
2
/usr/local/zabbix/sbin/zabbix_server
/usr/local/zabbix/sbin/zabbix_agentd

The logs are located in /tmp. After the server prints the above content,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
14212:20220519:203804.857 server #36 started [trapper #5]
14214:20220519:203804.861 server #38 started [alert syncer #1]
14215:20220519:203804.864 server #39 started [history poller #1]
14216:20220519:203804.867 server #40 started [history poller #2]
14217:20220519:203804.868 server #41 started [history poller #3]
14218:20220519:203804.870 server #42 started [history poller #4]
14219:20220519:203804.871 server #43 started [history poller #5]
14220:20220519:203804.872 server #44 started [availability manager #1]
14221:20220519:203804.876 server #45 started [trigger housekeeper #1]
14222:20220519:203804.877 server #46 started [odbc poller #1]
14201:20220519:203805.834 Zabbix agent item "vm.memory.size[total]" on host "Zabbix server" failed: first network error, wait for 15 seconds
.......
14193:20220519:203858.925 item "Zabbix server:zabbix[process,report writer,avg,busy]" became not supported: No "report writer" processes started.
14193:20220519:203859.934 item "Zabbix server:zabbix[process,report manager,avg,busy]" became not supported: No "report manager" processes started.
14207:20220519:203950.941 enabling Zabbix agent checks on host "Zabbix server": interface became available

Agent logs.

1
2
3
4
5
6
7
8
9
10
11
12
14229:20220519:203936.410 Starting Zabbix Agent [Zabbix server]. Zabbix 6.0.4 (revision 3d787ff402e).
14229:20220519:203936.410 **** Enabled features ****
14229:20220519:203936.410 IPv6 support: YES
14229:20220519:203936.410 TLS support: YES
14229:20220519:203936.410 **************************
14229:20220519:203936.410 using configuration file: /usr/local/zabbix/etc/zabbix_agentd.conf
14229:20220519:203936.411 agent #0 started [main process]
14233:20220519:203936.413 agent #4 started [listener #3]
14232:20220519:203936.413 agent #3 started [listener #2]
14234:20220519:203936.414 agent #5 started [active checks #1]
14231:20220519:203936.415 agent #2 started [listener #1]
14230:20220519:203936.418 agent #1 started [collector]

It seems that both are up and running.

Prepare the Docker PHP Environment

Here, since Zabbix Web requires a PHP environment, it would be too cumbersome to install from scratch. So let’s use Docker to complete it. Let’s pull the image first.

Starting from Zabbix version 5.0, the Zabbix frontend requires PHP version 7.2 or higher. Let’s install version 7.4.29. Avoid version 8, I’ll explain why later. Then, combine php-fpm with Nginx for interaction.

1
docker pull php:7.4.29-fpm

Pay attention to the requirements below, and repack the php:7.4.29-fpm according to the requirements.

PHP Version 7.2.5
PHP memory_limit Option 128MB In php.ini: memory_limit = 128M
PHP post_max_size Option 16MB In php.ini: post_max_size = 16M
PHP upload_max_filesize Option 2MB In php.ini: upload_max_filesize = 2M
PHP max_execution_time Option 300 seconds (allowed values: 0 and -1) In php.ini: max_execution_time = 300
PHP max_input_time Option 300 seconds (allowed values: 0 and -1) In php.ini: max_input_time = 300
PHP session.auto_start Option Must be disabled In php.ini: session.auto_start = 0
Database Support One of the following: MySQL, Oracle, PostgreSQL Must install one of the following modules: mysql, oci8, pgsql
bcmath php-bcmath
mbstring php-mbstring
PHP mbstring.func_overload Option Must be disabled In php.ini: mbstring.func_overload = 0
sockets php-net-socket. Requires user script support.
gd 2.0.28 php-gd. PHP GD extension must support PNG images (–with-png-dir), JPEG images (–with-jpeg-dir), and FreeType 2 (–with-freetype-dir).
libxml 2.6.15 php-xml
xmlwriter php-xmlwriter
xmlreader php-xmlreader
ctype php-ctype
session php-session
gettext php-gettext. Starting from Zabbix 2.2.1, PHP gettext extension is not a mandatory requirement for installing Zabbix. If gettext is not installed, the frontend will still work, but translation will not be available.

I have prepared the modified php-template.ini based on php.ini-production for PHP 7.4.29-fpm as requested. Let’s create a directory first, assuming I created it at /root/download/docker-php.

1
2
mkdir -p /home/docker-php
cd /home/docker-php

First, download the php-template file. I have already modified the php.ini-production based on the given parameters. Alternatively, you can extract it from /usr/local/etc/php/php.ini-production.

1
wget https://file.ruterfu.com/file/2022/05/19/20220519-php7.4.29-fpm-template.ini -O php-template.ini

Then, write the following content to the sources.list file (skip this if you are not in China; don’t forget to remove the COPY sources.list /etc/apt/sources.list line from the Dockerfile, otherwise it will be stuck for users in China).

1
2
3
4
5
6
7
8
deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb https://mirrors.aliyun.com/debian-security/ bullseye-security main
deb-src https://mirrors.aliyun.com/debian-security/ bullseye-security main
deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib

Next, create a Dockerfile and write the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 基于php:7.4.29-fpm构建docker,别用php8,zabbix不支持。
FROM php:7.4.29-fpm
# 复制当前目录下的sources.list到docker中的/etc/apt/sources.list
# 如果上一步sources.list文件不写即使用原来的sources.list,下面用#注释掉
COPY sources.list /etc/apt/sources.list
# 复制当前目录的php-template.ini到docker中的/usr/local/etc/php/php.ini
COPY php-template.ini /usr/local/etc/php/php.ini
# 然后执行下面的命令,根据https://hub.docker.com/_/php重新编辑的,加上了zabbix最低要求支持
# 最后4行表示支持中文,英语,日语的语言选择,如果需要在配置中选择其他语言,请自行加入,否则构建好后只能选择中文英文和日语这3个
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libjpeg-dev \
gettext \
libldap2-dev \
locales \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install gettext \
&& docker-php-ext-install ldap \
&& docker-php-ext-install sockets \
&& docker-php-ext-install bcmath \
&& docker-php-ext-install mysqli \
&& echo 'zh_CN.UTF-8 UTF-8' > /etc/locale.gen \
&& echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen \
&& echo 'ja_JP.UTF-8 UTF-8' >> /etc/locale.gen \
&& locale-gen

Do not use PHP 8, as Zabbix does not support it yet. Otherwise, you’ll end up with a mess like mine in the screenshot below.

Php8 with a lot of red

Finally, execute Docker build ..

1
docker build .

This may take some time. After it finishes, the console will output:

1
2
3
4
5
6
7
8
9
10
11
12
.......
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp.la modules/* libs/*
Generating locales (this might take a while)...
zh_CN.UTF-8... done
en_US.UTF-8... done
ja_JP.UTF-8... done
Generation complete.
Removing intermediate container 0e992fd8595d
---> 0b79b4c11d3c
Successfully built 0b79b4c11d3c

Then, execute the following command. The value after -v /usr/local/zabbix/php-webui:/var/www/zabbix should match the value from Successfully built 0b79b4c11d3c above.

In the command below, the part before the colon (:), is the host machine directory, and the part after the colon is the directory within the Docker container.

1
docker run -itd -p 9000:9000 -v /usr/local/zabbix/php-webui:/var/www/zabbix --name php-fpm 0b79b4c11d3c

Output:

1
f7dbe3921462ae42891fef72bb48311f5a7e661486f7e6be485739fc89cf9bb4

Then, copy the zabbix-ui files into /usr/local/zabbix/php-webui. The /root/download/zabbix-6.0.4 directory is the directory where Zabbix was compiled.

1
cp -rf /root/download/zabbix-6.0.4/ui/* /usr/local/zabbix/php-webui/

Remember, in Docker, php-webui corresponds to /var/www/zabbix.

Configure Nginx by modifying the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
server {
listen 8999;
server_name 填上自己的域名;

location ~ (\/zabbix).*\.php {
# 这里的/var/www指的是docker里面的目录
root /var/www;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /zabbix/assets {
alias /usr/local/zabbix/php-webui/assets;
expires 30d;
}
location /zabbix/js {
alias /usr/local/zabbix/php-webui/js;
expires 30d;
}
location /zabbix/favicon.ico {
alias /usr/local/zabbix/php-webui/favicon.ico;
expires 30d;
}

}

Configure the Web Environment

Welcome

Open your own webpage. Let’s assume mine is http://127.0.0.1:8999. After opening it, you should see the Zabbix homepage. Choose Chinese as the language pack.

Open the homepage

Prerequisite Checks

The preparations we made when building Docker are sufficient. You can see that all requirements are supported here.

Support for all requirements

Configure DB Connection

Follow the configuration method mentioned above for Zabbix. If you can’t connect, try disabling the Database TLS encryption. If you can’t connect to the database, the installation won’t proceed.

Configure DB Connection

Set Settings

Here, set the Zabbix hostname and select the timezone for Shanghai in the UTC+8 time zone. These are self-explanatory.

Set Settings

Installation Summary

Check if there are any issues before proceeding. Review them if anything is incorrect.

Installation Summary

Installation

I encountered an issue where it couldn’t write to a file, probably due to insufficient permissions. So, manually download this file will suffice.

1
cp /tmp/zabbix.conf.php /usr/local/zabbix/php-webui/conf/zabbix.conf.php

The reason for uploading this file to /usr/local/zabbix/php-webui instead of the recommended /var/www/zabbix/conf/zabbix.conf.php is because when running docker run, I mapped /usr/local/zabbix/php-webui to /var/www/zabbix, so modifying the former directory is equivalent to modifying the latter directory within Docker.

Final step

After the upload is complete and overwritten, click “Finish” to complete the installation.

All configurations completed

Login to Zabbix Web

The default username is Admin and the password is zabbix.

After logging in, you may see an error message in red.

1
2
3
4
Connection to Zabbix server "localhost" failed. Possible reasons:
1. Incorrect server IP/DNS in the "zabbix.conf.php";
2. Incorrect DNS server configuration.
Cannot assign requested address

It is clear that localhost cannot be resolved to the correct IP address (in most cases, the system recognizes localhost as 127.0.0.1 because it is configured in /etc/hosts file). To fix this, modify the zabbix.conf.php file uploaded earlier. Find the following content:

1
2
3
4
5
6
7
8
9
// 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;

// Uncomment and set to desired values to override Zabbix hostname/IP and port.
# 取消注释这2行,然后分别填上 172.17.0.1 和 10051
// $ZBX_SERVER = '';
// $ZBX_SERVER_PORT = '';

Remove the comment sign (#) in front of $ZBX_SERVER and $ZBX_SERVER_PORT, and fill in the values 172.17.0.1 and 10051 respectively. Save the file. No need to restart Docker. The reason we need to fill in 172.17.0.1 is that by default, Docker will generate an IP responsible for communication between Docker containers, which is 172.17.0.1/24. Among them, the host machine IP is fixed at 172.17.0.1. So, filling in 172.17.0.1 is equivalent to accessing the host machine’s port 10051. And when we compiled and ran the Zabbix server, it was listening on port 10051.

Installation Completed

With that, the installation process is complete. Don’t forget to change the password. I’ll cover how to start Zabbix automatically in the next article, as well as how to establish a secure connection between the Zabbix agent and the host machine. As for why I couldn’t cover everything in one article, I’m still digging my own pit. For example, how to install all the software in one go while building Docker. Also, it took me a while to figure out the Chinese part. I ended up using PHP 8.4, resulting in a bunch of errors.

Regarding Oracle Cloud, I recently heard in a Telegram group that Oracle Cloud is offering free servers. I successfully obtained one as well. If you’re interested, you can register at Oracle Cloud Free Tier - oracle.com. Please handle any issues or questions related to this on your own, as I can’t provide any advice or guidance on this matter.
“””