环境说明
这里使用为 CentOS 7.9版本进行测试验证,zabbix Server 采用源码包部署,数据库采用 MySQL8.0版本,zabbix-web使用 ,nginx+php来实现。
具体信息如下:
软件名 | 版本 |
---|---|
zabbix-server | 6.0.12LTS |
zabbix-agent | 6.0.12LTS |
mysql | 8.0.31 |
nginx | 1.20.1 |
php | 7.4.30 |
1,安装nginx,
1.1,关闭selinux
$ sed -i 's/enforcing/disabled/' /etc/selinux/config #永久
$ setenforce 0 # 临时
1.2,创建nginx用户和用户组,
$ groupadd -f nginx
$ useradd -g nginx nginx
1.3, 配置好系统yum源,安装如下依赖包:
$ yum install gcc pcre-devel openssl-devel
1.4,下载上传 nginx-1.20.2.tar.gz至服务器,解压文件包命令如下:
$ tar -xvf nginx-1.20.2.tar.gz
$ cd nginx-1.20.2
1.5,安装编译nginx命令如下:
./configure --prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/etc/nginx/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--http-client-body-temp-path=/usr/local/nginx/tmp/nginx/client_body \
--http-proxy-temp-path=/usr/local/nginx/tmp/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/tmp/nginx/fastcgi \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/lock/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre
$ make && make install
1.6, 配置nginx.conf,修改如下内容
**************************************************************************
location / {
root /usr/local/nginx/html/zabbix;
try_files $uri $uri/ /index.php?query_string;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/local/nginx/html/zabbix;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
**************************************************************************
1.7,配置nginx开机自启动服务
1.7.1,创建/usr/lib/systemd/system/nginx.service文件,输入如下内容:
$ vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
1.7.2,重载系统服务:
$ systemctl daemon-reload
1.7.3,设置开机启动:
$ mkdir -p /usr/local/nginx/tmp/nginx/client_body
systemctl enable nginx.service
systemctl start nginx.service
2,安装php
2.1, 下载安装如下依赖包
$ yum install libxml2-devel sqlite-devel libcurl-devel libpng-devel libjpeg-turbo-devel libXpm-devel freetype-devel libicu-devel gcc-c++ libxslt-devel oniguruma-devel libsodium-devel openldap-devel net-snmp-devel libwebp-devel
2.2,如果yum源没有oniguruma oniguruma-devel libsodium libsodium-devel libwebp-devel安装包,则需要从网上下载安装。
$ rpm -ivh oniguruma-6.7.0-1.el7.x86_64.rpm oniguruma-devel-6.7.0-1.el7.x86_64.rpm
$ rpm -ivh libsodium-1.0.18-2.el7.x86_64.rpm libsodium-devel-1.0.18-2.el7.x86_64.rpm
$ rpm -ivh libwebp-0.3.0-10.el7_9.x86_64.rpm rpm -ivh libwebp-devel-0.3.0-10.el7_9.x86_64.rpm
$ rpm -ivh oniguruma-6.7.0-1.el7.x86_64.rpm oniguruma-devel-6.7.0-1.el7.x86_64.rpm
$ rpm -ivh libsodium-1.0.18-2.el7.x86_64.rpm libsodium-devel-1.0.18-2.el7.x86_64.rpm
$ rpm -ivh libwebp-0.3.0-10.el7_9.x86_64.rpm rpm -ivh libwebp-devel-0.3.0-10.el7_9.x86_64.rpm
$ cp -frp /usr/lib64/libldap* /usr/lib/
2.3,centos7.9系统自带libzip-0.10.1-8版本太低,PHP不支持,所以源码安装libzip-1.2.0
$ tar zxf libzip-1.2.0.tar.g
$ cd libzip-1.2.0
$ ./configure
$ make && make install
$ export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
2.4,源码安装php,需要在root环境下编译,否则会提示libzip错误
$ tar -xvf php-7.4.30.tar.gz
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/usr/local/php \
--with-freetype \
--with-jpeg \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-webp \
--with-xpm \
--with-snmp \
--with-ldap
$ vim Makefile # 开头是‘EXTRA_LIBS’,结尾加上‘-llber’
$ make && make install
2.5,配置php-fpm
$ cp /usr/local/php-7.4.30/php.ini-production /usr/local/php/etc/php.ini
2.6,修改php.ini文件,将对应参数做如下修改。
$ vi /usr/local/php/etc/php.ini
max_execution_time = 300
max_input_time = 300
post_max_size = 16M
date.timezone =Asia/Shanghai
$ cd /usr/local/php-7.4.30/sapi/fpm && cp php-fpm.conf /usr/local/php/etc/php-fpm.conf
$ cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
2.7,测试php-fpm配置
$ /usr/local/php/sbin/php-fpm -t -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf
$ chown -R nginx:nginx /usr/local/php/var
2.8,php启动命令
$ /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf
2.9,配置php-fpm开机自启动
$ vi /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=PHP-FPM
After=network.target syslog.target2
[Install]
WantedBy=default.target
Alias=php-fpm.service
[Service]
User=nginx
Group=nginx
#User and Group can be set in the php-fpm configure file
Type=forking
#PIDFile=/usr/local/php/var/run/php-fpm.pid
#不需要在这里指定pid文件位置,需要到php-fpm.conf文件中指定
ExecStart=/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf
ExecStop=/bin/kill -INT $MAINPID
ExecReload=/bin/kill -USR2 $MAINPID
2.10,启动php
$ cp /usr/local/php-7.4.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
systemctl daemon-reload
systemctl enable php-fpm.service
systemctl start php-fpm.service
3,安装MySQL
3.1,创建mysql组与用户
# 创建一个用户组:mysql
groupadd mysql
# 创建一个系统用户:mysql,指定用户组为mysql
useradd -r -g mysql mysql
3.2,下载上传并解压安装包
tar xvf /usr/local/mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
3.3,创建软链接,并授权
cd /usr/local
mv mysql-8.0.31-linux-glibc2.12-x86_64 mysql-8.0.31 #改名
ln -s mysql-8.0.31 mysql #创建软链接
#授权
chown -R mysql.mysql /usr/local/mysql
chown -R mysql.mysql /data/3306/dbdata
3.4,修改配置文件
cat >/etc/my.cnf<<\EOF
[client]
port=3306
socket=/data/3306/dbdata/mysql.sock
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
#prompt='(zabbix)\u@\h [\d]> '
no-auto-rehash
max_allowed_packet = 128M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
[mysqld_safe]
log-error=/data/3306/dbdata/logs/error.log
pid-file=/data/3306/dbdata/my.pid
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin=/usr/local/mysql/bin/mysqladmin
log=/data/3306/logs/mysqld_multi.log
[mysqld]
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/3306/dbdata/data
tmpdir=/data/3306/dbdata/tmp
socket=/data/3306/dbdata/mysql.sock
skip_name_resolve = 1
#general_log = 1
#general_log_file=/data/3306/dbdata/logs/mysql.log
log-error=/data/3306/dbdata/logs/error.log
pid-file=/data/3306/dbdata/my.pid
plugin-dir=/data/3306/dbdata/lib/plugin
innodb_data_home_dir=/data/3306/dbdata/data
innodb_log_group_home_dir=/data/3306/dbdata/data
default-storage-engine = InnoDB
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
lower_case_table_names = 0
transaction_isolation = READ-COMMITTED
default_time_zone = "+8:00"
explicit_defaults_for_timestamp = true
log_bin_trust_function_creators = 1
event_scheduler=1
#sql_mode = STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
back_log = 1024
max_connections = 3000
max_connect_errors = 6000
max_user_connections=1000
external-locking = FALSE
connect_timeout = 20
open_files_limit = 65535
table_open_cache = 1024
interactive_timeout = 1800
wait_timeout = 28800
max_allowed_packet = 128M
max_heap_table_size = 1024M
tmp_table_size = 128M
sort_buffer_size = 16M
join_buffer_size = 16M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
thread_cache_size = 64
key_buffer_size = 256M
performance_schema = 1
performance_schema_instrument = '%=on'
table_definition_cache = 800
table_open_cache_instances = 64
thread_stack = 512K
#write_buffer = 4M
bulk_insert_buffer_size = 64M
#expire_logs_days = 30
lock_wait_timeout = 3600
slow_query_log = 1
log_timestamps = SYSTEM
slow_query_log_file = /data/3306/logs/mysql-slow.log
long_query_time = 5
log_queries_not_using_indexes = 0
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 5000
#############master and slave############
server-id = 55
log-bin=/data/3306/dbdata/binlog/mysql-bin
binlog_format = ROW
binlog_cache_size = 256M
max_binlog_cache_size = 2G
max_binlog_size = 1G
#binlog_row_image = FULL
binlog_expire_logs_seconds = 1209600
master_info_repository = TABLE
relay_log_info_repository = TABLE
log_slave_updates
relay_log_recovery = 1
slave_skip_errors = ddl_exist_errors
sync_binlog = 1
log_timestamps = system
log-output="FILE"
#log-error = /data/3306/dbdata/log/logs/error.log
log_error_verbosity = 2
log-slow-slave-statements = 1
log-slow-admin-statements = 1
gtid_mode = on
enforce_gtid_consistency = 1
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
# default value is CRC32
binlog_checksum = 1
# default value is ON
relay-log-purge = 1
#========>[innodb]
innodb_data_file_path = ibdata1:1G:autoextend
innodb_buffer_pool_size = 30G
innodb_spin_wait_delay = 30
innodb_flush_log_at_trx_commit = 1
innodb_thread_concurrency = 0
innodb_log_buffer_size = 64M
#innodb_log_file_size = 256M
#innodb_log_files_in_group = 3
innodb_redo_log_capacity = 256M
innodb_max_dirty_pages_pct = 50
innodb_lock_wait_timeout = 20
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_status_file = 1
innodb_write_io_threads = 16
innodb_read_io_threads = 16
innodb_io_capacity = 4000
innodb_flush_neighbors = 1
innodb_adaptive_flushing = 1
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_buffer_pool_instances = 4
innodb_sync_spin_loops = 100
innodb_thread_concurrency = 0
innodb_thread_sleep_delay = 500
innodb_concurrency_tickets = 1000
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_online_alter_log_max_size = 4G
#internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0
innodb_max_undo_log_size=4G
innodb_status_output = 0
innodb_status_output_locks = 0
innodb_undo_log_truncate = 1
innodb_undo_directory = /data/3306/dbdata/undolog
#innodb_undo_tablespaces = 95
innodb_io_capacity_max = 8000
innodb_flush_sync = 0
innodb_sort_buffer_size = 67108864
innodb_autoinc_lock_mode = 1
innodb_file_per_table = 1
[mysqldump]
quick
max_allowed_packet = 128M
EOF
3.5,创建所需目录
mkdir -p /data/3306/dbdata/{logs,binlog,data,tmp,lib,undolog,relaylog}
3.6,初始化mysql
cd /usr/local/mysql-8.0.31
bin/mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/3306/dbdata/data --user=mysql
3.7,添加环境变量
export PATH=$PATH:/usr/local/mysql/bin
#执行命令生效
source ~/.bash_profile
3.8,添加启动项,并启动
cp /opt/mysql-8.0.31/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
service mysql start
3.9,获取初始密码
grep 'root@localhost:' /data/3306/logs/error.log | awk -F 'root@localhost: ' '{print $2}'
3.10,修改密码
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY "Admin@zbx";
quit
4,安装Zabbix
下载软件包: https://www.zabbix.com/cn/download_sources
4.1,解压软件包
tar -zxvf zabbix-6.0.12.tar.gz
4.2,创建 zabbix 用户
groupadd --system zabbix
useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix
4.3,初始化数据(进入解压目录下的 database目录下)
cd zabbix-6.0.12/database/mysql
mysql -u root -p
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'@'%' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'@'%';
flush privileges;
use zabbix;
source schema.sql;
source images.sql;
source data.sql;
quit
4.4,安装编译所需的依赖,这里根据启用的模块不一样,所需要的依赖也不一样(内网环境需要在公网上下载 go >“golang”的rpm包)
yum install gcc mysql-devel libevent-devel libcurl-devel libxml2-devel libssh2-devel OpenIPMI-devel net-snmp-devel go java-devel -y
4.5,进行编译安装,去到软件包解压目录zabbix-6.0.12中
cd zabbix-6.0.12
./configure --prefix=/usr/local/zabbix \
--enable-server \
--enable-agent \
--with-mysql \
--enable-ipv6 \
--with-net-snmp \
--with-libcurl \
--with-libxml2 \
--with-ssh2 \
--enable-java
make install
4.6,修改配置文件
cp /usr/local/zabbix/etc/zabbix_server.conf{,.bak}
grep -v -E "^#|^$" /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/usr/local/zabbix/log/zabbix_server.log
LogFileSize=0
PidFile=/usr/local/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=127.0.0.1
DBPort=3306
DBName=zabbix
DBUser=zabbix
DBPassword=Zabbix
StartPollers=12
StartTrappers=30
StartDiscoverers=50
StartHTTPPollers=100
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
CacheSize=4G
CacheUpdateFrequency=1200
ValueCacheSize=12G
Timeout=4
AlertScriptsPath=/usr/local/zabbix/alertscripts
ExternalScripts=/usr/local/zabbix/externalscripts
FpingLocation=/sbin/fping
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
4.6.1,修改如下配置
cd /usr/local/nginx/html/zabbix/conf/
cp zabbix.conf.php.example zabbix.conf.php
cat zabbix.conf.php
<?php
// Zabbix GUI configuration file.
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = '127.0.0.1';
$DB['PORT'] = '3306';
$DB['DATABASE'] = 'zabbix';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = 'passwordxxxxx';
4.7,创建相关配置目录及权限设置
mkdir -p /usr/local/zabbix/{alertscripts,externalscripts,logs}
mkdir /var/run/zabbix
chown zabbix.zabbix /var/run/zabbix/
chown zabbix.zabbix /usr/local/zabbix/ -R
4.8,配置systemd启动
cat <<EOF > /etc/systemd/system/zabbix-server.service
[Unit]
Description=Zabbix Server
After=syslog.target network.target network-online.target
Wants=network.target network-online.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/zabbix/sbin/zabbix_server -c /usr/local/zabbix/etc/zabbix_server.conf
RemainAfterExit=yes
PIDFile=/usr/local/zabbix/zabbix_server.pid
[Install]
WantedBy=multi-user.target
EOF
4.9,加入开机启动并启动
systemctl daemon-reload
systemctl enable zabbix-server --now
systemctl start zabbix-server
4.10,前端访问zabbix,并配置安装
4.11,启用zabbix-agent
启用 zabbix-server 服务器本身的 agnet,这里我们上面安装的时候已经把模块安装上了,这里只需要配置下启动即可。
这里由于在一台机器,不用修改什么,直接启动即可
4.11.1,配置systemd
cat <<EOF > /etc/systemd/system/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=syslog.target network.target network-online.target
Wants=network.target network-online.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
4.11.2,加入开机启动并启动
systemctl daemon-reload
systemctl enable zabbix-agent --now
systemctl start zabbix-agent.service