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 | 服务器:Aliyun(China) 1 core 1G 2M bandwidth |
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 | --2022-05-19 19:39:06-- https://cdn.zabbix.com/zabbix/sources/stable/6.0/zabbix-6.0.4.tar.gz |
Then, unzip it.
1 | tar -zxvf zabbix-6.0.4.tar.gz |
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 | checking for a BSD-compatible install... /usr/bin/install -c |
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 | ...... |
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 | ...... |
Try again.
1 | apt install -y libsnmp-dev |
Prompt…
1 | ..... |
Install…
1 | apt install -y libevent-dev |
Prompt…
1 | ..... |
Install…
1 | apt install -y libcurl4-openssl-dev |
The pre-compiled environment configuration is completed when you see the following information:
1 | Configuration: |
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.
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
.
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 。 |
If Using a Self-Deployed Database
I won’t attempt this step.
1 | mysql -uroot -p<password> |
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.
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 | ListenPort=10051 #这里修改其zabbix与agent通讯端口,默认是10051 |
Start the Server and Agent
1 | /usr/local/zabbix/sbin/zabbix_server |
The logs are located in /tmp
. After the server prints the above content,
1 | 14212:20220519:203804.857 server #36 started [trapper #5] |
Agent logs.
1 | 14229:20220519:203936.410 Starting Zabbix Agent [Zabbix server]. Zabbix 6.0.4 (revision 3d787ff402e). |
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 | mkdir -p /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 | deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib |
Next, create a Dockerfile
and write the following content:
1 | # 基于php:7.4.29-fpm构建docker,别用php8,zabbix不支持。 |
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.
Finally, execute Docker build .
.
1 | docker build . |
This may take some time. After it finishes, the console will output:
1 | ....... |
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 | server { |
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.
Prerequisite Checks
The preparations we made when building Docker are sufficient. You can see that all requirements are supported here.
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.
Set Settings
Here, set the Zabbix hostname and select the timezone for Shanghai in the UTC+8 time zone. These are self-explanatory.
Installation Summary
Check if there are any issues before proceeding. Review them if anything is incorrect.
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.
After the upload is complete and overwritten, click “Finish” to complete the installation.
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 | Connection to Zabbix server "localhost" failed. Possible reasons: |
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 | // Use IEEE754 compatible value range for 64-bit Numeric (float) history values. |
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.
“””