zabbix搭建

一、环境准备

    本人用的是RedHat 6.6 做的试验。参照运维生存时间PDF版本或者网页链接谢谢运维生存时间的技术人员的整理。

    环境是 nginx + php + mysql + zabbix(所有软件都是源码安装)
    各个版本号,nginx-1.10.2、PHP-7.0.12、mysql-5.6.14、zabbix-2.2.2 
  • 首先关闭 selinux

查看selinux的状态

#getenforce
#或者
#sestaus -v
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

零时关闭

#setenforce 0
  • 1
  • 1

重启生效,修改配置文档/tec/sysconfig/selinux

#vim /tec/sysconfig/selinux
SELINUX=disabled
  • 1
  • 2
  • 1
  • 2
  • 关闭iptables 
    及时生效
开启
#chkconfig iptables on
关闭
#chkconfig iptables off
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 同步时间以及创建本地yum源
#crontab -l
5 18 * * * /usr/sbin/ntpdate *.*.*.* > /var/log/ntpdate.log
  • 1
  • 2
  • 1
  • 2

(不会的自行百度吧)

  • 卸载自带的mysql
#rpm -qa | grep mysql   ###查看自带的mysql 
#rpm -e (安装包)       ###卸载
  • 1
  • 2
  • 1
  • 2

二、安装nginx

  • 必要软件准备

为了支持rewrite功能,我们需要安装pcre

# yum install pcre*     ###如过你已经装了,请跳过这一步
  • 1
  • 1

需要ssl的支持,如果不需要ssl支持,请跳过这一步

# yum install openssl*
  • 1
  • 1
  • 安装依赖包
yum install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel


  • 下载nginx (本文所有网上下载的安装包,都放在/usr/local/src/ 下)
#cd /usr/local/src/
#wget -c http://nginx.org/download/nginx-1.10.2.tar.gz
  • 1
  • 2
  • 1
  • 2
  • 解压安装
#tar -zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2
#./configure --prefix=/usr/local/nginx-1.10.2 --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
#make
#make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 启动和关闭
#/usr/local/nginx-1.10.2/sbin/nginx             ###启动
#/usr/local/nginx-1.10.2/sbin/nginx -s  stop    ###关闭
#/usr/local/nginx-1.10.2/sbin/nginx -s reload   ###重置
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

三、PHP安装+nginx配置

  • 安装所需的依赖包 
    确保安装之前有安装gd,png,curl,xml等等lib开发库。如果不确定,执行以下命令:
#yum install -y gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel freetype-devel 
  • 1
  • 1
  • 下载php
#cd /usr/local/src/
#wget -c http://cn2.php.net/distributions/php-7.0.12.tar.gz
  • 1
  • 2
  • 1
  • 2
  • 解压并安装
#tar -zxvf php-7.0.12.tar.gz
#cd php-7.0.12
#./configure  --prefix=/usr/local/php-7.0.12 --with-config-file-path=/usr/local/php-7.0.12/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib  --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --enable-bcmath
#make
#make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 配置php
#cp php.ini-production /usr/local/php-7.0.12/etc/php.ini
#cp www.conf.default www.conf
#cp /usr/local/php-7.0.12/etc/php-fpm.conf.default /usr/local/php-7.0.12/etc/php-fpm.conf
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  • 运行php
#/usr/local/php7.0.12/sbin/php-fpm      ##启动php
#killall php-fpm        ###停止php
  • 1
  • 2
  • 1
  • 2

如果执行完启动PHP命令之后没有报错,一般情况表示启动正常,如果不放心,也可以通过端口判断php是否启动

#netstat -lnt | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
  • 1
  • 2
  • 1
  • 2
  • 配置nginx 
    配置测试站点test.com
#mkdir -p /data/logs/nginx/     ###用于存放nginx日志
#mkdir -p /data/site/test.com/  ###站点根目录
#vim /data/site/test.com/info.php
<?php
phpinfo();
?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 修改nginx配置文档 
    在nginx.conf的http段中加入如下内容:
#vim /usr/local/nginx-1.10.2/conf/nginx.conf
server {
listen 80;
server_name localhost;
access_log /data/logs/nginx/zabbix.com.access.log main;

index index.php index.html index.html;
root /data/site/test.com;

location /
{
try_files $uri $uri/ /index.php?$args;
}

location ~ .*\.(php)?$
{
expires -1s;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;

}
}
  • 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
  • 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
  • 启动nginx
#/usr/local/nginx-1.10.2/sbin/nginx/
  • 1
  • 1
  • 访问测试 
    使用浏览器访问ip地址,出现nginx的版本号说明安装成功了

  • 启动nginx报错及解决办法(本人所遇到的问题,通过百度解决之)

报错1

bashunknown log format "main" in /usr/local/nginx-1.10.2/conf/nginx.conf:38
  • 1
  • 1

打开nginx.conf,”main”错误是因为丢失了log_format选项,之前把他屏蔽掉了,修改之后问题解决。

报错2

nginx: [error] open() "/usr/local/nginx-1.10.2/logs/nginx.pid" failed (2: No such file or directory)
  • 1
  • 1

使用nginx -c的参数指定nginx.conf文件的位置

#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  • 1
  • 1

四、安装mysql

  • 安装必要的组件
#yum install -y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio libaio-devel bzr bison libtool ncurses5-devel 
  • 1
  • 1
  • 下载安装mysql
#cd /usr/local/src/
#http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz/from/http://cdn.mysql.com/ -O mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz
#tar -zxvf mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz -C ../
#cd /usr/lcoal/
#ln -s mysql-5.6.14-linux-glibc2.5-x86_64 mysql ###创建一个软链接
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 创建mysql用户和用户组,及数据库存放目录
#mkdir -p /data/mysql_data_3306
#mkdir -p /data/mysql_log
$mkdir -p /data/log-bin
#groupadd mysql
#useradd mysql -g mysql -M -s /sbin/nologin
#chown -R mysql.mysql /data/mysql_data_3306 /data/mysql_log /data/log-bin
#chown -R mysql.mysql /usr/local/mysql-5.6.14-linux-glibc2.5-x86_64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 修改配置文件
#vim /etc/my.cnf
[mysqld]
# GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /data/mysql_data_3306/mysql.sock
pid-file = /data/mysql_data_3306/mysql.pid
port = 3306
# MyISAM #
key_buffer_size = 1344M
myisam_recover = FORCE,BACKUP
# SAFETY #
max_allowed_packet = 16M
max_connect_errors = 1000000
skip_name_resolve
# DATA STORAGE #
datadir = /data/mysql_data_3306/
long_query_time = 1
# BINARY LOGGING #
log-bin = /data/log-bin/mysql-bin-3306
expire-logs-days = 14
sync-binlog = 1
server-id = 1
max_binlog_size = 500M
# REPLICATION #
relay-log = /data/log-bin/relay-bin-3306
slave-net-timeout = 60
# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size = 32M
max_connections = 500
thread_cache_size = 50
open_files_limit = 65535
table_definition_cache = 4096
table_open_cache = 4096
# INNODB #
innodb_data_file_path = ibdata1:128M;ibdata2:10M:autoextend
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 2
innodb_lock_wait_timeout = 50
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_thread_concurrency = 8
innodb_buffer_pool_size = 8G
# LOGGING #
log-error = /data/mysql_log/mysql-error-3306.log
log-queries-not-using-indexes = 1
slow-query-log = 1
long_query_time = 1
slow-query-log-file = /data/mysql_log/mysql-slow-3306.log
# FOR SLAVE #
#binlog-format = ROW
#log-slave-updates = true
#gtid-mode = on
#enforce-gtid-consistency = true
#master-info-repository = TABLE
#relay-log-info-repository = TABLE
#sync-master-info = 1
#slave-parallel-workers = 2
#binlog-checksum = CRC32
#master-verify-checksum = 1
#slave-sql-verify-checksum = 1
#binlog-rows-query-log_events = 1
#report-port = 3306
#report-host = 10.1.1.10
  • 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
  • 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
  • 系统服务
# cp -af /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld_3306
# vi /etc/init.d/mysqld_3306    ###修改两处位置:
basedir=/usr/local/mysql
datadir=/data/mysql_data_3306

执行如下命令
# chmod 755 /etc/init.d/mysqld_3306     ###修改权限
# chkconfig --add mysqld_3306           ###添加启动项
# chkconfig --level 345 mysqld_3306 on
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 初始化数据库
#cd /usr/local/mysql
#./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf
  • 1
  • 2
  • 1
  • 2
  • 启动数据库
#server mysqld_3306 start
  • 1
  • 1
  • 修改root密码
# /usr/local/mysql/bin/mysql -p -uroot -S /data/mysql_data_3306/mysql.sock #这里直接回车就能进入数据库系统
Mysql>delete from mysql.user where user='';
Mysql>update mysql.user set password=PASSWORD(‘xxxxxxxx’) where user='root';
Mysql> create database zabbix default charset utf8; ###防止乱码
Mysql>flush privileges;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

五、安装zabbix

  • 下载安装zabbix
#cd /usr/local/src/
#wget -c http://iweb.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/2.2.2/zabbix-2.2.2.tar.gz
#tar -zxvf zabbix-2.2.2.tar.gz      ###解压
#cd zabbix-2.2.2
#./configure --prefix=/usr/local/zabbix-2.2.2/ --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2
#make
#make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7


  • 报错信息 

报错1
configure: error: MySQL library not found
the problem is not installed mysql-devel
  • 1
  • 2
  • 1
  • 2

安装MySQL-devel

#yum install mysql-devel  
  • 1
  • 1

报错2

configure: error : Not found NET-SNMP library
  • 1
  • 1

安装snmp支持

#yum install net-snmp-devel
  • 1
  • 1

创建用户

#groupadd zabbix
#useradd -g zabbix zabbix
  • 1
  • 2
  • 1
  • 2
  • 初始化数据库
#mysql -uroot -p123456 -S /data/mysql_data_3306/mysql.sock zabbix < /usr/local/src/zabbix-2.2.2/database/mysql/schema.sql 
如果你仅仅是初始化proxy的数据库,那么就够了。如果初始化server,就接着导入下面两个sql。
#mysql -uroot -p123456 -S /data/mysql_data_3306/mysql.sock zabbix < /usr/local/src/zabbix-2.2.2/database/mysql/images.sql 
#mysql -uroot -p123456 -S /data/mysql_data_3306/mysql.sock zabbix < /usr/local/src/zabbix-2.2.2/database/mysql/data.sql 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 配置zabbix
#cd /usr/local/src/zabbix-2.2.2
#mkdir /etc/zabbix
#cp conf/zabbix_server.conf /etc/zabbix/
#vim /etc/zabbix/zabbix_server.conf
DBName=zabbix
DBUser=root
DBPassword=ttlsapwd
DBPort=3306
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 启动zabbix server
#/usr/loca/zabbix-2.2.2/sbin/zabbix_server
  • 1
  • 1

六、zabbix客户端安装配置

  • 下载安装
#cd /usr/local/src
#wget -c http://iweb.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/2.2.2/zabbix-2.2.2.tar.gz       ##和zabbix server端一样
解压安装
#cd zabbix-2.2.2
#./configure --prefix=/usr/lcoal/zabbix-2.2.2/ --enable-agent
#make
#make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • zabbix客户端配置 
    修改zabbix_agentd配置文件,在zabbix源码目录下
#vim /usr/local/zabbix-2.2.2/etc/zabbix_agentd.conf
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

其中Server和ServerActive都指定zabbixserver的IP地址,不同的是,前者是被动后者是主动。也就是说Server这个配置是用来允许127.0.0.1这个ip来我这取数据。而serverActive的127.0.0.1的意思是,客户端主动提交数据给他。

  • zabbix客户端启动
#/usr/local/zabbix-2.2.2/sbin/zabbix_agentd
  • 1
  • 1

七、zabbix管理网站配置

  • 拷贝前端文件
# mkdir /data/logs/nginx
# mkdir /data/site/test.com/zabbix
# cp -rp frontends/php/* /data/site/test.com/zabbix
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  • 配置虚拟主机 
    查看并修改配置文件路径(安装php时候已修改,此处可忽略)
# vim /usr/local/nginx-1.5.8/conf/vhost/test.com.conf
server {
        listen       80;
        server_name localhost;
        access_log  /data/logs/nginx/test.com.access.log  main;

        index index.html index.php index.html;
        root /data/site/test.com;

        location /
        {
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ ^(.+.php)(.*)$ {
                fastcgi_split_path_info ^(.+.php)(.*)$;
                include fastcgi.conf;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param  PATH_INFO          $fastcgi_path_info;
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 在线配置zabbix 
    浏览器打开本地ip地址 
    然后到欢迎界面,配置没有问题(图片可参考运维生存时间)上面的。 
    1. 看到Welcome to Zabbix 2.2几个大字,然后点击 Next>>
    2. php需求检查,右边框里所有OK就没有问题,接着点击Next>>
    3. MySQL配置,填入相应的数据库信息 
      Database trpe MySQL
      Database host 127.0.0.1
      Database port 3306
      Database name zabbix
      User root
      Password ***

      点击 Test connection 出现OK,说明没有问题,接着点击Next>>
    4. zabbix服务端详细信息(这里要注意三个都得填写,后期出错,各种找原因,就是因为Name这项没有填写导致) 
      Host localhost
      Port 10051
      Name zabbix
       
      接着点击Next>>
    5. zabbix安装钱信息列表,点击Next>>
    6. 安装完成 
      按照说明,需要把配置文件下载到本地之后,导入到相应的目录,操作完成之后,点击Retry,显示绿色的OK,说明已经成功。
    7. 点击Finish 进入登陆界面,输入账号密码 
      默认账号:admin 
      密码:zabbix
    8. 进入首页

八、结束

到此安装结束,之间碰到各种报错,然后就各种百度,最后还是把问题解决了。安装比较简单,但是比较耗时,安装完之后才是真正的开始,希望大家看到这里之后zabbix环境已经搭建完成。如有问题,首先自己动手解决,实在解决不了的,上运维生存时间的留言区查找答案,或者留言说出问题。谢谢运维生存时间的技术们,你们辛苦了。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章