Home Installing Zabbix on Ubuntu
Post
Cancel

Installing Zabbix on Ubuntu

Recipe:

  1. Start with a brand new Ubuntu v22 machine, physical or virtual.
  2. Run this script.
  3. Wait a few minutes.
  4. Enjoy your new Zabbix installation.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash

# source: https://itslinuxfoss.com/install-zabbix-ubuntu-22-04/
# source: https://www.zabbix.com/download?zabbix=6.2&os_distribution=ubuntu&os_version=22.04&components=server_frontend_agent&db=mysql&ws=apache
# With lots of my own mods.

function banner1 {
  echo    "============================================================================================"
  echo -e "${GREEN}$1${NC}"
  echo    "--------------------------------------------------------------------------------------------"
  echo
}

set -e

# Just to make sure the password is loaded
sudo echo


# --------------------------------------------------------------------
# Variables - It's all here. Nothing below this section needs to be changed.
Z_NAME=zabbix
Z_DB_NAME=zabbix
Z_DB_USERNAME=zabbix_user
Z_DB_PASSWORD=wordpass
PROXMOX=1
APT="apt-get -y -qq"  # Pretty quiet with a 'yes' to questions

GREEN='\033[0;32m'
NC='\033[0m' # No Color

banner1 "Here we go..."

# --------------------------------------------------------------------
banner1 "Turning off 'needs restart' prompts."
FILE=/etc/needrestart/needrestart.conf
if [ -f "$FILE" ]; then
  sudo sed -i 's/#$nrconf{restart} = '"'"'i'"'"';/$nrconf{restart} = '"'"'a'"'"';/g' $FILE
fi

# --------------------------------------------------------------------
banner1 "Updating and Upgrading..."
sudo $APT update
sudo $APT upgrade

# --------------------------------------------------------------------
banner1 "Adding additional programs..."
sudo $APT install nmap net-tools

# --------------------------------------------------------------------
banner1 "Installing, enabling and starting Apache..."
sudo $APT install apache2	
sudo systemctl start apache2
sudo systemctl enable apache2

# --------------------------------------------------------------------
# Install Proxmox agent if needed
if [ "$PROXMOX" -eq "1" ]; then
  banner1 "Adding the Proxmox agent..."
  sudo $APT install qemu-guest-agent
  sudo systemctl enable qemu-guest-agent
  sudo systemctl start qemu-guest-agent
fi

# --------------------------------------------------------------------
banner1 "Adding the Zabbix repo and updating..."
wget https://repo.zabbix.com/zabbix/6.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.2-4%2Bubuntu22.04_all.deb
sudo dpkg -i zabbix-release_6.2-4+ubuntu22.04_all.deb
rm zabbix-release_6.2-4+ubuntu22.04_all.deb
sudo $APT update

# --------------------------------------------------------------------
banner1 "Installing Zabbix..."
sudo $APT install zabbix-server-mysql zabbix-frontend-php zabbix-apache-conf zabbix-sql-scripts zabbix-agent

# --------------------------------------------------------------------
banner1 "Installing MySQL..."
sudo $APT install mysql-server

# --------------------------------------------------------------------
banner1 "Creating the Zabbix database and it's user..."
cat <<EOF > sql.sql
create database $Z_DB_NAME character set utf8mb4 collate utf8mb4_bin;
create user $Z_DB_USERNAME@localhost identified by "$Z_DB_PASSWORD";
grant all privileges on $Z_DB_NAME.* to $Z_DB_USERNAME@localhost;
set global log_bin_trust_function_creators = 1;
EOF

sudo mysql < sql.sql
rm sql.sql

# --------------------------------------------------------------------
banner1 ">>>>> Loading the Zabbix database. Please be patient... <<<<<"
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -u$Z_DB_USERNAME -p$Z_DB_PASSWORD $Z_DB_NAME
echo

# --------------------------------------------------------------------
cat <<EOF > sql.sql
set global log_bin_trust_function_creators = 0;
EOF

sudo mysql < sql.sql
rm sql.sql

# --------------------------------------------------------------------
banner1 "Configuring Zabbix..."
sudo sed -i "s/^DBName=zabbix$/DBName=$Z_DB_NAME/g"          /etc/zabbix/zabbix_server.conf
sudo sed -i "s/^DBUser=zabbix$/DBUser=$Z_DB_USERNAME/g"      /etc/zabbix/zabbix_server.conf
sudo sed -i "s/^\# DBPassword=$/DBPassword=$Z_DB_PASSWORD/g" /etc/zabbix/zabbix_server.conf


# --------------------------------------------------------------------
banner1 "Configuring the Zabbix user in sudoers file..."
#sudo echo "$Z_NAME  ALL=(root) NOPASSWD:ALL" >> /etc/sudoers
#sudo echo "Defaults:$Z_NAME    !requiretty" >> /etc/sudoers
echo "$Z_NAME  ALL=(root) NOPASSWD:ALL" | sudo tee -a /etc/sudoers
echo "Defaults:$Z_NAME    !requiretty" | sudo tee -a /etc/sudoers

sudo systemctl restart zabbix-server zabbix-agent apache2
sudo systemctl enable zabbix-server zabbix-agent apache2
# --------------------------------------------------------------------

banner1 "All done."
HOST_IP=$(hostname -I | cut -d " " -f 1)
echo -e "Access the site here: ${GREEN}http://$HOST_IP/zabbix${NC}"
echo -e "Database user:        ${GREEN}$Z_DB_USERNAME{NC}"
echo -e "Database password:    ${GREEN}$Z_DB_PASSWORD{NC}"
echo -e "Zabbix Username:      ${GREEN}Admin{NC}"
echo -e "Zabbix Password:      ${GREEN}zabbix{NC}"
echo
echo -e "You will need to answer a few questions. The above information will help."
This post is licensed under CC BY 4.0 by the author.