编译安装Zabbix4.0

1、LNMP环境搭建

1.1 Nginx环境搭建

# 1.安装Nginx依赖
yum install -y vim bash-completion lrzsz wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 2.下载Nginx源码包
cd /usr/local/src && wget 'http://nginx.org/download/nginx-1.14.2.tar.gz'
# 3.解压
tar xf nginx-1.14.2.tar.gz && cd nginx-1.14.2
# 4.编译安装
./configure --prefix=/usr/local/nginx
make && make install
# 5.配置环境变量
echo "export PATH=$PATH:/usr/local/nginx/sbin/" >> /etc/profile
source /etc/profile
# 6.检查nginx配置
nginx -t
# 7.启动nginx
nginx
# 8.关闭nginx
nginx -s stop

浏览器访问:http://ip

配置Nginx使用systemctl管理:

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
[Install]
WantedBy=multi-user.target

# 启动nginx
systemctl start nginx.service
# 关闭nginx
systemctl stop nginx.service

1.2 PHP源码编译安装

# 1.安装PHP依赖
yum -y install epel-release
yum -y install  gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap openldap-devel libmcrypt libmcrypt-devel
# 2.下载PHP源码包
cd /usr/local/src/ && wget 'http://hk1.php.net/distributions/php-5.6.40.tar.gz'
# 3.解压
tar -zxf php-5.6.40.tar.gz && cd php-5.6.40
# 4.编译安装
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-ctype --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-fpm
make && make install
# 5.生成php.ini配置文件
cp php.ini-production /usr/local/php/etc/php.ini
# 6.配置环境变量
echo "export PATH=$PATH:/usr/local/php/sbin/:/usr/local/php/bin/" >> /etc/profile
source /etc/profile
# 生成php-fpm.conf配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  • 编译参数说明:
--prefix指定php的安装目录
--with-config-file-path指定php的配置文件位置
--with-mysql、--with-mysqli让php可以操作mysql
--enable-fpm主要是nginx要来调用php语言得使用php-fpm

配置PHP使用systemctl管理:

vim /usr/lib/systemd/system/php.servic
[Unit]
Description=php
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
[Install]
WantedBy=multi-user.target

# 启动php
systemctl start php.service
# 关闭php
systemctl stop php.service

Nginx配置PHP代理:

  • 编写一个php文件
<?php
phpinfo();
?>
  • 修改nginx配置文件
location / {
  root   html;
  index  index.html index.htm index.php;
}

location ~ \.php$ {
	root           html;
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME  $document_root/scripts$fastcgi_script_name;
	include        fastcgi_params;
}
  • 修改配置文件之后要重启nginx:
systemctl restart nginx.service

浏览器访问:http://ip/test.php

1.3 MySQL源码编译安装

# 1.安装MySQL依赖
yum install -y gcc gcc-c++ make tar openssl openssl-devel cmake ncurses ncurses-devel
# 2.添加mysql用户
useradd -s /sbin/nologin mysql
# 3.下载源码包
cd /usr/local/src && wget 'https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39.tar.gz'
# 4.解压
tar -zxvf mysql-5.6.39.tar.gz
cd mysql-5.6.39
# 5.编译安装
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1
make && make install
# 6.生成MySQL启动脚本
cp support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
# 7.配置环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin/" >> /etc/profile
source /etc/profile
# 8.更新配置文件
vim /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
user=mysql
skip-name-resolve
long_query_time=2
slow_query_log_file=/data/mysql/mysql-slow.log
expire_logs_days=2
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
max_allowed_packet      = 512M
connect_timeout = 60
net_read_timeout = 120

[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
# 9.数据库初始化
mkdir -pv /data/mysql
chown -R mysql.mysql /data/mysql/
chown -R mysql.mysql /usr/local/mysql/
yum install -y perl-Module-Install
# 初始化过程出现两个'OK'则初始化成功
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql  --datadir=/data/mysql/
# 10.systemctl管理MySQL
vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
[Install]
WantedBy=multi-user.target
# 11.启动MySQL
systemctl start mysqld
  • 编译参数说明:
CMACK_INSTALL_PREFIX指定安装的目录
MYSQL_DATADIR指定Mysql的数据目录
  • mysql的安全方式
mysql能限制ip访问、访问需要用户名密码、mysql权限还能再细分
默认让127.0.0.1访问,用户名root,不需要密码
mysql -uroot -h 127.0.0.1 -A # 登陆
  • 给MySQL设置密码
mysqladmin -h 127.0.0.1 -u root password 'Vu39hbnx'
mysql -h 127.0.0.1 -uroot -pVu39hbnx -A
  • MySQL授权某ip登录
GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.0.0.%' IDENTIFIED BY 'Vu39hbnx' WITH GRANT OPTION;
FLUSH PRIVILEGES;
  • 网络登录MySQL
mysql -h 10.0.0.15 -uroot -pVu39hbnx -A

1.4 LNMP环境验证

用户 --> php文件
用户 --> nginx --> 127.0.0.1:9000 --> php-fpm --> mysql
  • 编写一个PHP文件/usr/local/nginx/html/test_mysql.php
<?php
  $link=mysql_connect("127.0.0.1","root","Vu39hbnx"); 
  if(!$link){
    echo "mysql_connect fail";
  }else{
    echo "mysql_connect success";
  }
?>

浏览器访问:http://ip/test_mysql.php

<?php
header("Content-type:text/html;charset=utf-8");
$server="127.0.0.1";
$username="root";
$password="Vu39hbnx";
echo '开始mysql数据库连接.<br><br><br>';
$mysqli=new mysqli($server,$username,$password);
if($mysqli)
{
    echo '连接mysql数据库成功.<br>';
    echo 'You can use \'new mysqli()\' to connect.<br><br>';
    $mysqli->close();
}
if($con1= mysqli_connect($server,$username,$password))
{
    echo '连接mysql数据库成功.<br>';
    echo 'You can use mysqli_connect() to connect.<br><br>';
    mysqli_close($con1);
}
if($con2=mysql_connect($server,$username,$password)) 
{
    echo '连接mysql数据库成功.<br>';
     echo 'You can use mysql_connect() to connect.<br><br>';    
     mysql_close($con2);
}
else
{
    die('连接mysql数据库失败:' . mysql_error());
}
echo '<br><br><br>';
echo '关闭mysql数据库连接.';
?>

2、zabbix4.0版本的编译安装

  • zabbix的部署原理
zabbix server需要把监控数据入sql数据库,所以得Mysql环境
zabbix的web是基于php开发的,所以得LNMP环境
部署zabbix server和zabbix web

2.1 zabbix4.0编译安装

# 1.安装依赖
yum install -y libevent-devel wget tar gcc gcc-c++ make net-snmp-devel libxml2-devel libcurl-devel
# 2.添加zabbix用户
useradd -s /sbin/nologin zabbix
# 3.下载源码包
cd /usr/local/src/ && wget 'https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz'
# 4.解压
tar -zxvf zabbix-4.0.3.tar.gz && cd zabbix-4.0.3
# 5.编译安装
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql=/usr/local/mysql/bin/mysql_config --with-net-snmp --with-libcurl --with-libxml2
make  && make install
# 6.配置环境变量
echo "export PATH=$PATH:/usr/local/zabbix/sbin/:/usr/local/zabbix/bin/" >> /etc/profile
source /etc/profile
# 7.初始化数据库
mysql -h 127.0.0.1 -uroot -pVu39hbnx -A
create database zabbix character set utf8 collate utf8_bin;
grant all privileges on zabbix.* to zabbix@'127.0.0.1' identified by 'Vu39hbnx';
flush privileges;
set names utf8;
use zabbix;
source /usr/local/src/zabbix-4.0.3/database/mysql/schema.sql;
source /usr/local/src/zabbix-4.0.3/database/mysql/data.sql;
source /usr/local/src/zabbix-4.0.3/database/mysql/images.sql;
# 8.配置zabbix server,最主要是配置连接mysql的用户名和密码
vim /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/usr/local/zabbix/zabbix_server.log
DBHost=127.0.0.1
DBName=zabbix
DBUser=zabbix
DBPassword=zabbixpwd
DBPort=3306
Timeout=30
AlertScriptsPath=/usr/local/zabbix/alertscripts
ExternalScripts=/usr/local/zabbix/externalscripts
LogSlowQueries=3000
# 9.启动zabbix-server
chown -R zabbix.zabbix /usr/local/zabbix/
zabbix_server
  • 编译参数说明
--prefix指定安装目录
--enable-server安装zabbix server
--enable-agent安装zabbix agent
--with-mysql用mysql来存储

2.2 zabbix-web安装

# 创建zabbix-web目录
mkdir -p /usr/local/nginx/html/zabbix
# 将zabbix-web文件复制到目录中
cp -a /usr/local/src/zabbix-4.0.3/frontends/php/* /usr/local/nginx/html/zabbix/

浏览器访问:http://ip/zabbix

2.3 配置web界面

根据检查需求Fail的地方进行修改

vim /usr/local/php/etc/php.ini
post_max_size = 32M
max_execution_time = 300
max_input_time = 300
date.timezone = Asia/Shanghai
always_populate_raw_post_data = -1

修改完php.ini文件之后要重启php:

systemctl restart php.service

然后刷新网页:

配置zabbix-web数据库连接:

下一步,配置Zabbix server details:

详细配置信息:

zabbix-web无法创建配置文件:

  • 根据提示操作
Alternatively, you can install it manually:

1. Download the configuration file  # 下载zabbix-web生成的配置文件
2. Save it as "/usr/local/nginx/html/zabbix/conf/zabbix.conf.php"  # 将配置文件放在服务器`/usr/local/nginx/html/zabbix/conf/`下

scp zabbix.conf.php root@10.0.0.15:/usr/local/nginx/html/zabbix/conf/

上传配置文件到指定位置之后,刷新网页:

完成安装:

![image-20190604083949002](/Users/mac/Library/Application Support/typora-user-images/image-20190604083949002.png)

2.4登陆Zabbix

  • zabbix-web默认用户名为:Admin
  • zabbix-web默认密码为:zabbix

  • 禁用本身监控

zabbix默认监控zabbix-server本身,如果不需要的话可以在设置中将zabbix-server本身的监控禁用掉即可:

  • 禁用guset用户

为了安全起见,我们要将不需要的用户禁用或者删除掉,在zabbix中,每一个用户都必须有一个用户组,所有的权限之类的操作都是在用户组中操作的,无法对单个用户操作:

  • 修改Admin密码

zabbix默认用户名密码大家都知道,为了安全起见也需要修改Admin的密码:

2.5网页汉化

zabbix-web默认使用英文,支持切换成中文,但是默认直接切换到中文的话在监控的图形中会有乱码的现象,我们需要修改zabbix使用的字体。

  • 切换zabbix-web默认语言为中文

更新之后,网页自动刷新并切换为中文显示:

  • 在[监测]-->[图形]中的图形中会出现中文乱码

  • 解决乱码问题
  1. 下载微软雅黑字体
wget https://raw.githubusercontent.com/chenqing/ng-mini/master/font/msyh.ttf
  1. 上传微软雅黑字体到zabbix字体存放路径
scp msyh.ttf root@10.0.0.15:/usr/local/nginx/html/zabbix/fonts/
  1. 修改zabbix-web网页字体配置为msyh.ttf
vim /usr/local/nginx/html/zabbix/include/defines.inc.php
define('ZBX_GRAPH_FONT_NAME',           'msyh'); // font file name
define('ZBX_FONT_NAME', 'msyh');
  1. 验证是否解决乱码问题

posted @ 2020-08-06 13:46  StaryJie  阅读(602)  评论(0编辑  收藏  举报