首页 > 代码库 > centos7下搭建zabbix监控
centos7下搭建zabbix监控
安装zabbix前,需要先安装lnmb环境:
一、安装mysql
1、卸载了mariadb(mysql分支,会占用3306端口等,也可以用其替代官方mysql)
rpm -qa|grep mariadb #查看拥有的软件包
rpm -e --nodeps mariadbXXX #强制卸载
2、创建mysql用户
useradd -d /var/empty/mysql -s /sbin/nologin mysql #因为用户目录有隐藏文件,所以为了眼不见为净,挪动了下
3、安装mysql,我这里使用mysql-5.6.30源包,需要软件包的可以去官网下载
cd /usr/local/src #我的源包放在该路径 tar -zxvf mysql-5.6.30.tar.gz #解压压缩包 cd /usr/local/src/mysql-5.6.30
mkdir -p /home/mysql/etc #创建配置文件目录,此处纯粹看个人喜好,只是/home我挂载机械硬盘的,较大所以存这里 mkdir -p /home/mysql/tmp #创建日志文件目录 mkdir -p /home/mysql/data #创建数据文件目录 chown -R mysql:mysql /home/mysql #修改文件拥有者
yum install cmake bison #mysql新版本弃用./configure了,所以需要先安装cmake,bios yum install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* #安装依赖包
编译及安装 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql #此处是软件安装路径 -DMYSQL_DATADIR=/home/mysql/data #此处是data库路径 -DSYSCONFDIR=/home/mysql/etc/my.cnf #此处是配置文件路径 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/home/mysql/tmp/mysqld.sock #此处是日志文件路径 -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 #用utf8文字 -DDEFAULT_COLLATION=utf8_general_ci #用utf8文字 -DMYSQL_TCP_PORT=3306 #使用3306端口 make make install
初始化数据库 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/home/mysql/data --user=mysql 添加server执行权限 chmod +x /usr/local/mysql/support-files/mysql.server 启动mysql.server /usr/local/mysql/support-files/mysql.server start (Starting MySQL. SUCCESS!) 添加mysql.server到开机启动 cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig --add /etc/init.d/mysqld chkconfig mysqld --level 345 on ln -s /usr/local/mysql/bin/mysql /bin/mysql
二、安装PHP
1、安装依赖环境及安装
yum install gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel libxml2-devel libjpeg-turbo-devel libmcrypt-devel
cd /usr/local/src/ #我的源包存放路径 tar -zxvf php-5.5.38.tar.gz cd php-5.5.38 如是提示缺少某些依赖,则使用yum源进行安装(阿里yum挺全的,不过偶尔也会缺少,再缺就用源包自己安装吧,可以手动加路径) ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/home/mysql/tmp/mysqld.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --enable-bcmath --enable-mbstring --enable-sockets --with-gd --with-libxml-dir --with-gettext make make test make install
2、PHP配置文件
cp php.ini-production /usr/local/php/etc/php.ini #copy下模板,源包内有 ln -s /usr/local/php/etc/php.ini /etc/php.ini #制作个软链接,在此之前先检查下etc中有没有php.ini,有就先删掉 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf #复制下php-fpm的配置文件 ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf #制作软连接 (完全看个人喜好存放)
groupadd www #先创建个www用户吧,后面用也可以给nginx使用 useradd -s /sbin/nologin -d /var/empty/www -g www www
vi /usr/local/php/etc/php-fpm.conf #编辑php-fpm配置文件 user = www #设置php-fpm运行账号为www group = www #设置php-fpm运行组为www pid = run/php-fpm.pid #取消前面的分号
cp /usr/local/src/php-5.5.38/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #复制执行文件 chmod +x /etc/rc.d/init.d/php-fpm #添加执行权限 chkconfig php-fpm on #加入开机自启
vim /usr/local/php/etc/php.ini #修改php.ini配置文件 disable_functions=passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname date.timezone = Asia/Shanghai # 修改时区 expose_php = Off #禁止显示php版本的信息 short_open_tag = ON #支持php短标签 opcache.enable=1 #php支持opcode缓存 opcache.enable_cli=0 #去掉注释 php支持opcode缓存 在最后一行添加:zend_extension=opcache.so #开启opcode缓存功能
三、安装nginx
1、yum下依赖,安装编译nginx
yum -y install zlib-devel pcre-devel openssl-devel tar -zxvf nginx-1.8.1.tar.gz cd nginx-1.8.1/ ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module make make install
2、测试nginx是否成功
/usr/local/nginx/sbin/nginx 本地浏览器输入:http://ServerIP/ 出现Welcome to nginx!
3、制作启动快捷执行文件vim /etc/rc.d/init.d/nginx
将下面文本复制进去变形,这样执行nginx的时候就可以使用start/restart等快捷命令
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep ‘configure arguments:‘` for opt in $options; do if [ `echo $opt | grep ‘.*-temp-path‘` ]; then value=http://www.mamicode.com/`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { #configtest || return $? stop sleep 1 start } reload() { #configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
4、加入开机启动项:
chkconfig nginx on
5、配置nginx支持php vim /usr/local/nginx/conf/nginx.conf
#user nobody;修改为user www www; index index.html index.htm index.php; #添加index.php location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
6、测试PHP、nginx是否正常:
cd /usr/local/nginx/html/
vim test.php (添加测试页面)
<?php phpinfo(); ?>
chown -R www:www /usr/localnginx/html/ chmod 700 /usr/local/nginx/html/test.php 访问http://IP/test.php
四、安装zabbix
1、在mysql中创建zabbix库和账号
groupadd zabbix useradd -s /sbin/nologin -d /var/empty/zabbix -g zabbix zabbix mysql -uroot -p mysql> create database zabbix character set utf8; mysql> grant all privileges on zabbix.* to zabbix@ip identified by ‘passwd‘; mysql> FLUSH PRIVILEGES;
2、安装zabbix(本人使用zabbix-3.0.4.tar.gz),下载和解压就不写了
yum install mysql-devel net-snmp-devle #其实我们只用到agent,snmp可以不装 ./configure --prefix=/usr/local/zabbix --with-mysql --with-net-snmp --with-libcurl --enable-server --enable-agent --enable-proxy make make install
3、开放端口zabbix-agent(客户端使用到10050端口)
开放端口zabbix-server(服务端使用到10051端口)
vim /etc/services zabbix-agent 10050/tcp # Zabbix Agent zabbix-agent 10050/udp # Zabbix Agent zabbix-trapper 10051/tcp # Zabbix Trapper zabbix-trapper 10051/udp # Zabbix Trapper
4、修改zabbix配置
mkdir -p /etc/zabbix #新建配置文件目录 cp -R /usr/local/zabbix/etc/* /etc/zabbix/ #从软件处复制配置文件 chown -R zabbix:zabbix /etc/zabbix
5、修改zabbix-server配置vim /etc/zabbix/zabbix_server.conf
LogFile=/home/zabbix/zabbix_server.log PidFile=/home/zabbix/zabbix_server.pid DBName=zabbix DBUser=zabbix DBPassword=XXXX #指定zabbix数据库密码 ListenIP=XXXXX #服务器IP地址
添加到开机启动vim /etc/rc.d/rc.local
/etc/init.d/zabbix_server -c /etc/zabbix/zabbix_server.conf
6、修改zabbix-agent配置vim /etc/zabbix/zabbix_agentconf
LogFile=/home/zabbix/zabbix_agent.log PidFile=/home/zabbix/zabbix_agent.pid EnableRemoteCommands=1 #允许执行远程命令 Hostname=biyi-kvm #必须与zabbix创建的host name相同 server=XX #服务器IP地址
添加到开机启动vim /etc/rc.d/rc.local
/etc/init.d/zabbix_agent -c /etc/zabbix/zabbix_agent.conf
7、复制zabbix安装文件到nginx中
cd /usr/local/src/zabbix-3.0.4/frontends/ cp -a php /usr/local/nginx/html/zabbix cd /usr/local/nginx/html chown -R zabbix:zabbix zabbix
8、安装zabbix-web前要修改下php的参数vim /etc/php.ini
max_execution_time = 300 #可能有纰漏,后面安装的时候回提醒的 max_input_time = 300 memory_limit = 128M post_max_size = 32M date.timezone = Asia/Shanghai mbstring.func_overload=Off
9、安装zabbix-web
登陆 #根据提示一步步走就行了,缺什么就补什么
10、客户机安装zabbix-agent(我是在官网直接下载3.0.4的agent)
yum install zabbix-agent-3.0.4-1.el7.x86_64.rpm
添加端口 vim /etc/services
zabbix-agent 10050/tcp # Zabbix Agent zabbix-agent 10050/udp # Zabbix Agent
配置参数 vim /etc/zabbix/zabbix_agentd.conf
PidFile=/home/zabbix/zabbix_agentd.pid LogFile=/home/zabbix/zabbix_agentd.log LogFileSize=1 Server=服务器的IP Hostname=名字,在zabbix-web添加主机上进行配对
加入开机启动:
vim /etc/rc.d/rc.local /etc/init.d/zabbix-agent start
11、配置zabbix-web(zabbix-web的使用就不详细讲了,自己捣鼓捣鼓就懂了)
默认账号密码是:admin zabbix 在配置-主机处可以添加客户机 需要给其添加模板(看你是使用zabbix-agent还是snmp等等)
本文出自 “欢迎吐槽” 博客,请务必保留此出处http://rickzhw.blog.51cto.com/12004841/1843856
centos7下搭建zabbix监控