Monitoring your servers with Zabbix 6
published on
When you have a number of servers you will end up wanting a way of monitoring them all so that you can keep an eye on them and make sure they are all running properly.
Zabbix is one tool that will get this job done. Now Zabbix doesn't just monitor servers. It can monitor pretty much anything.
This tutorial will just cover how to install Zabbix on Apache and use it to monitor your servers.
Let's get started
First we need to make sure we can get the software from the right places. Run the following commands to make sure we do
sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/apache2
sudo apt-get update
Install PHP
We also install a few other utilities to support tasks that Nextcloud may perform depending on how you choose to use it.
sudo apt install php8.1-{common, cli, bz2, zip, curl, intl, mysql, snmp, memcached, imagick, gd, imap, ldap, soap, tidy, xml, gmp, pspell, mbstring, opcache, fpm, ssh2, imap, redis, apcu, mcrypt, smbclient, bcmath}
Configure PHP
Edit /etc/php/8.1/fpm/pool.d/www.conf
and change lines to read as below
listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.mode = 0660
Edit /etc/php/8.1/fpm/php.ini
and change or add the lines to read as below
output_buffering = Off
zlib.output_compression On
zlib.output_compression_level 5
expose_php = off
max_execution_time = 60
max_input_time = 60
max_input_vars = 1000
memory_limit = 512M
post_max_size = 1M
upload_max_filesize = 2M
allow_url_fopen = Off
date.timezone = Europe/London
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200
opcache.save_comments=1
Lastly, run sudo systemctl restart php8.1-fpm
to put all your changes into effect.
It's time to install the MariaDB database
Run sudo apt install mariadb-server mariadb-backup
to install everything you need and then make sure to secure it by running sudo mysql_secure_installation
.
Edit the file at /etc/mysql/mariadb.conf.d/50-server.cnf
and make the changes below
bind-address 127.0.0.1
local-infile=0
Once this is done run sudo mysql -u root -p
and create a database with the following commands
CREATE DATABASE zabbix CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost';
FLUSH PRIVILEGES;
exit
Install and configure Apache
Start by running the following commands to install the Apache
sudo apt-get install apache2 apache2-utils libapache2-mod-fcgid
sudo a2enmod mime http2 rewrite deflate expires headers ssl
sudo a2enmod proxy_fcgi setenvif
Now edit /etc/apache2/conf-enabled/security.conf
and add the following lines
ServerTokens Prod
ServerSignature Off
SecServerSignature " "
TraceEnable Off
Header unset ETag
FileETag None
Header unset Server
Header always unset X-Powered-By
Header unset X-CF-Powered-By
Header unset X-Mod-Pagespeed
Header unset X-Pingback
Now edit /etc/apache2/apache2.conf
and add the following lines
Timeout 120
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 10
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
ProxyRequests Off
ProxyVia Off
#Only allow proxies from local network
<Proxy "*">
Order Deny,Allow
Deny from all
Allow from 192.168.0
# Require ip 192.168.0
</Proxy>
Now run the following two commands to disable the default site and put your changes into effect
sudo a2dissite 000-default.conf
sudo systemctl restart apache2
Now it is time to install the Zabbix server
Run the following set of commands to download and install the Zabbix server, agent, frontend software, Apache configuration files and SQL scriptscd ~
wget https://repo.zabbix.com/zabbix/6.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.2-4+ubuntu20.04_all.deb
sudo dpkg -i zabbix-release_6.2-4+ubuntu20.04_all.deb
sudo apt update
sudo apt install zabbix-server-mysql zabbix-frontend-php zabbix-apache-conf zabbix-sql-scripts zabbix-agent
sudo zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -u zabbix -p zabbix
Update the Zabbix configuration files
Edit /etc/zabbix/zabbix_server.conf
and update the database details based on the database name, username and password you used earlier. Also edit /usr/share/zabbix/conf/zabbix.conf.php
and make sure that the ZBX_SERVER
variable is set to 127.0.0.1
, the ZBX_SERVER_PORT
is set to 10051
and the ZBX_SERVER_NAME
is set.
Now restart Zabbix with sudo systemctl restart zabbix-server
.
Now edit /etc/zabbix/zabbix_agentd.conf
and change the hostname to match the name of the server Zabbix is installed on.
Lastly, make sure to allow the ports 10050 and 10051 in your firewall and run the following two commands to ensure that all your changes to Zabbix or Apache so far are now in effectsudo systemctl restart zabbix-server zabbix-agent apache2
sudo systemctl enable zabbix-server zabbix-agent apache2
Note that the Zabbix agent will either be called zabbix-agent or zabbix-agent2.
Set up the Apache virtual host file
Copy the default configuration file using sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/zabbix.conf
and edit the new file at /etc/apache2/sites-available/zabbix.conf
.
You should copy in the contents of /etc/zabbix/apache2.conf
and make sure to update the ServerName
and ServerAdmin
. The DocumentRoot
should also be changed to /usr/share/zabbix
.
Now enable the new virtual host and reload Apache by running
sudo a2ensite zabbix
sudo systemctl reload apache2
Add a TLS certificate to help secure your site
sudo apt install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot -d [domain] --apache
Finish off installing Zabbix
Visit the domain name you assigned to the Zabbix virtual host in your favourite browser and work your way through the setup process.
Once finished you will have a default login name and password which need changing. Log in to Zabbix and go to Administration, then Users, then Admin, then Password and finally Change Password.
You should also go to Configuration then Hosts. You should see the word 'disabled' next to 'localhost'. Click on it once to change it to enabled. This allows Zabbix to monitor the server that it is running on.
If you wish to receive email alerts (optional)
To recieve email alerts when there is an issue login to the Zabbix control panel and select User Settings, then Media, then Add. Now choose 'Email (HTML)' and add your email address. Click on Add then on Update.
To monitor a different server
So far you have installed Zabbix and you can use it to monitor the server you installed it on. If you wish to monitor other servers too then follow along.
In order to monitor another server you need to first install the Zabbix agent on the server you wish to monitor. Run the following on the new server
wget https://repo.zabbix.com/zabbix/6.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.2-4+ubuntu20.04_all.deb
dpkg -i zabbix-release_6.2-4+ubuntu20.04_all.deb
apt update
sudo apt install zabbix-agent2
Now edit the agent configuration file by running sudo nano /etc/zabbix/zabbix_agent2.conf
and change the following settings
Change Server=
[ip address of Zabbix server]
Change ServerActive=
[ip address of Zabbix server] (only if active checking required)
Change Hostname=
[hostname of agent server]
Make sure your firewall allows connections on port 10050 and run the following two commands to enable the Zabbix agent
sudo systemctl restart zabbix-agent2
sudo systemctl enable zabbix-agent2
Now login to the Zabbix server and select Configuration, then Hosts, then Create Host. Fill in the hostname to match what you added to the agent configuration file, change the group to 'Linux server' and add the IP address of the server you wish to monitor.
Next, select the 'Templates' tab and add the 'Linux Zabbix agent' template. Click on update and you should see your server being monitored after a couple of minutes at most.
Conclusion
There is much more to do to harden Zabbix and you will find plenty of information on their website (linked above).
Zabbix can also monitor a lot more than just servers and you will need to experiment to be able to keep an eye on your resources.