CentOS下LNMP环境搭建Zabbix监控系统

摘要:#软件环境: CentOS-6.7-x64:Server端和Linux-Agent端系统环境; nginx-1.8.1.tar.gz:Nginx源码包; mysql-5.5.49-linux2.6-x86_64.tar.gz:Mysql二进制包; php-5.4.45.tar.bz2:PHP源码包; xcache-3.2.0.tar.bz2:PHP缓存模块源码包; nginx.vim:Nginx配

#软件环境:

CentOS-6.7-x64:Server端和Linux-Agent端系统环境; nginx-1.8.1.tar.gz:Nginx源码包; mysql-5.5.49-linux2.6-x86_64.tar.gz:Mysql二进制包; php-5.4.45.tar.bz2:PHP源码包; xcache-3.2.0.tar.bz2:PHP缓存模块源码包; nginx.vim:Nginx配置文件插件; zabbix-2.2.12.tar.gz:Zabbix源码包;

1 (31).jpg

#部署过程: ##安装开发环境,并解决软件依赖关系: 注意:事先配置好base和epel源。

[root@node2 ~]# yum groupinstall "Server Platform Development" "Development tools" "Desktop Platform Development" [root@node2 ~]# yum install bzip2-devel pcre-devel libvpx-devel libXpm-devel libaio-devel libcurl-devel libssh2-devel net-snmp net-snmp-devel libmcrypt-devel mhash-devel [root@node0 ~]# ln -sv /usr/lib64/libXpm.so /usr/lib/libXpm.so `/usr/lib/libXpm.so\' -> `/usr/lib64/libXpm.so\' [root@node0 ~]# ln -sv /usr/lib64/libXpm.so.4 /usr/lib/libXpm.so.4 `/usr/lib/libXpm.so.4\' -> `/usr/lib64/libXpm.so.4\' [root@node0 ~]# ln -sv /usr/lib64/libXpm.so.4.11.0 /usr/lib/libXpm.so.4.11.0 `/usr/lib/libXpm.so.4.11.0\' -> `/usr/lib64/libXpm.so.4.11.0\'

##编译安装Nginx:

源码来自于搜狐开源镜像:http://mirrors.sohu.com/nginx/

创建nginx用户:

[root@node0 ~]# useradd -r nginx

编译安装:

[root@node0 ~]# tar xf nginx-1.8.1.tar.gz [root@node0 ~]# cd nginx-1.8.1 [root@node0 nginx-1.8.1]# ./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre [root@node0 nginx-1.8.1]# make [root@node0 nginx-1.8.1]# make install

导出二进制命令:

[root@node0 ~]# ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx `/usr/sbin/nginx\' -> `/usr/local/nginx/sbin/nginx\'

提供服务脚本:

注意:此脚本为Nginx官方提供,根据自身环境修改以下变量; 脚本位置:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/ [root@node0 ~]# vim /etc/rc.d/init.d/nginx # config: /usr/local/nginx/conf/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /usr/local/nginx/logs/nginx.pid NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" lockfile=/usr/local/nginx/logs/nginx.lock

启动服务:

[root@node0 ~]# chmod +x /etc/rc.d/init.d/nginx [root@node0 ~]# chkconfig nginx --add [root@node0 ~]# chkconfig nginx on [root@node0 ~]# service nginx start

nginx配置文件支持vim高亮:

插件位置:http://www.vim.org/scripts/script.php?script_id=1886 [root@node0 ~]# cp nginx.vim /usr/share/vim/vim74/syntax/ [root@node0 ~]# vim /usr/share/vim/vim74/filetype.vim \\:配置文件加入以下行; au BufRead,BufNewFile /usr/local/nginx/conf/* if &ft == \'\' | setfiletype nginx | endif ##注意:/usr/local/nginx/conf/*,表示配置文件目录,根据自身环境进行修改;

##编译安装PHP:

源码来自于搜狐开源镜像:http://mirrors.sohu.com/php/

创建php用户:

[root@node0 ~]# useradd -r php

编译安装:

[root@node0 ~]# tar xf php-5.4.45.tar.bz2 [root@node0 ~]# cd php-5.4.45 [root@node0 php-5.4.45]# ./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=php --with-fpm-group=php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --enable-mbstring --with-libxml-dir --with-zlib --with-pcre-dir --with-vpx-dir --with-jpeg-dir --with-png-dir --with-xpm-dir --with-freetype-dir --enable-xml --enable-sockets --enable-session --with-mcrypt --with-bz2 --enable-bcmath --with-gd --with-gettext --enable-xmlwriter --enable-xmlreader --enable-ctype [root@node0 php-5.4.45]# make [root@node0 php-5.4.45]# make install

添加并重读环境变量:

[root@node0 ~]# vim /etc/profile.d/php.sh export PATH=/usr/local/php/sbin:/usr/local/php/bin:$PATH [root@node0 ~]# source /etc/profile.d/php.sh [root@node0 ~]# php -v PHP 5.4.45 (cli)

导出man手册:

[root@node0 ~]# vim /etc/man.config MANPATH /usr/local/php/php/man

编译安装xcache:

源码来自于官网:http://xcache.lighttpd.net/ [root@node0 ~]# tar xf xcache-3.2.0.tar.bz2 [root@node0 ~]# cd xcache-3.2.0/ [root@node0 xcache-3.2.0]# phpize [root@node0 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@node0 xcache-3.2.0]# make [root@node0 xcache-3.2.0]# make install

为php和php-fpm提供配置文件:

[root@node0 ~]# cp ./php-5.4.45/php.ini-production /usr/local/php/lib/php.ini [root@node0 ~]# vim /usr/local/php/lib/php.ini date.timezone = Asia/Shanghai always_populate_raw_post_data = 1 mbstring.func_overload = 0 memory_limit = 128M post_max_size = 16M upload_max_filesize = 2M max_execution_time = 300 max_input_time = 300 [root@node0 ~]# cd /usr/local/php/etc/ [root@node0 etc]# cp php-fpm.conf.default php-fpm.conf [root@node0 etc]# vim php-fpm.conf listen = 172.16.0.4:9000 \\:本机IP,9000端口;

配置xcache:

[root@node0 ~]# mkdir -p /usr/local/php/etc/php.d [root@node0 ~]# cp ./xcache-3.2.0/xcache.ini /usr/local/php/etc/php.d/ [root@node0 ~]# vim /usr/local/php/etc/php.d/xcache.ini extension = /usr/local/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

为php-fpm提供服务启动脚本,并启动服务:

[root@node0 ~]# cp ./php-5.4.45/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@node0 ~]# chmod +x /etc/rc.d/init.d/php-fpm [root@node0 ~]# chkconfig php-fpm --add [root@node0 ~]# chkconfig php-fpm on [root@node0 ~]# service php-fpm start

##安装Mysql二进制包:

二进制包来自于搜狐开源镜像:http://mirrors.sohu.com/mysql/MySQL-5.5/

创建mysql用户和数据目录:

[root@node0 ~]# useradd -r mysql [root@node0 ~]# mkdir -p /zabbix/data [root@node0 ~]# chown -R mysql:mysql /zabbix/data/ [root@node0 ~]# chmod -R o-rx /zabbix/data/

二进制安装设置:

[root@node0 ~]# tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz -C /usr/local/ [root@node0 ~]# ln -sv /usr/local/mysql-5.5.49-linux2.6-x86_64 /usr/local/mysql `/usr/local/mysql\' -> `/usr/local/mysql-5.5.49-linux2.6-x86_64\' [root@node0 ~]# cd /usr/local/mysql/ [root@node0 mysql]# chown -R root:mysql ./* [root@node0 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/zabbix/data

导出头文件、库文件、man手册:

[root@node0 ~]# ln -sv /usr/local/mysql/include /usr/local/include/mysql `/usr/local/include/mysql\' -> `/usr/local/mysql/include\' [root@node0 ~]# vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [root@node0 ~]# ldconfig [root@node0 ~]# vim /etc/man.config MANPATH /usr/local/mysql/man

添加并重读环境变量:

[root@node0 ~]# vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH [root@node0 ~]# source /etc/profile.d/mysql.sh [root@node0 ~]# mysql -V mysql Ver 14.14 Distrib 5.5.49, for linux2.6 (x86_64) using readline 5.1

提供配置文件和服务启动脚本:

[root@node0 ~]# cd /usr/local/mysql [root@node0 mysql]# mkdir etc [root@node0 mysql]# cp support-files/my-large.cnf etc/my.cnf [root@node0 mysql]# vim etc/my.cnf [mysqld] datadir = /zabbix/data thread_concurrency = 2 \\:数值为服务器核心数的二倍; [root@node0 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@node0 mysql]# chmod +x /etc/rc.d/init.d/mysqld

加入服务列表,启动并初始化数据库:

[root@node0 ~]# chkconfig mysqld --add [root@node0 ~]# chkconfig mysqld on [root@node0 ~]# service mysqld start

##编译安装Zabbix-Server:

源码来自于SF.net:https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/2.2.12/

创建zabbix用户:

[root@node0 ~]# useradd -r zabbix

编译安装server端和agent端:

[root@node0 ~]# tar xf zabbix-2.2.12 [root@node0 ~]# cd zabbix-2.2.12 [root@node0 zabbix-2.2.12]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-ssh2 --with-libxml2 --with-libcurl [root@node0 zabbix-2.2.12]# make [root@node0 zabbix-2.2.12]# make install

添加并重读环境变量:

[root@node0 ~]# vim /etc/profile.d/zabbix.sh export PATH=/usr/local/zabbix/sbin:/usr/local/zabbix/bin:$PATH [root@node0 ~]# source /etc/profile.d/zabbix.sh [root@node0 ~]# zabbix_server -V Zabbix server v2.2.12 (revision 59538) (20 April 2016)

导出man手册:

[root@node0 ~]# vim /etc/man.config MANPATH /usr/local/zabbix/share/man

提供Zabbix前端:

[root@node0 ~]# mkdir -p /zabbix/web [root@node0 ~]# cp -a ./zabbix-2.2.12/frontends/php /zabbix/web/zabbix

在Nginx中添加虚拟主机:

[root@node0 ~]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name 172.16.0.4; location / { root /zabbix/web; index index.php index.html index.hml; } location ~ .php$ { root /zabbix/web; fastcgi_pass 172.16.0.4:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

创建Zabbix库和并授权用户:

[root@node0 ~]# mysql mysql> create database zabbix; Query OK, 1 row affected (0.00 sec) mysql> grant all on zabbix.* to "zabbixuser"@"172.16.0.4" identified by "zabbixpass"; Query OK, 0 rows affected (0.05 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)

导入数据库:

[root@node0 ~]# cd ./zabbix-2.2.12/database/mysql/ [root@node0 mysql]# mysql zabbix < schema.sql [root@node0 mysql]# mysql zabbix < images.sql [root@node0 mysql]# mysql zabbix < data.sql

编辑Server端配置文件:

[root@node0 ~]# vim /usr/local/zabbix/etc/zabbix_server.conf DBHost=172.16.0.4 \\:数据库IP; DBName=zabbix \\:存储监控数据的库 DBUser=zabbixuser \\:用于连接数据的用户; DBPassword=zabbixpass \\:连接数据库的密码;

配置本身agent用于监控自己:

[root@node0 ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf Server=172.16.0.4 ServerActive=172.16.0.4 Hostname=server.zabbix.org

为Server端:提供服务脚本:

[root@node0 ~]# cp ./zabbix-2.2.12/misc/init.d/fedora/core5/zabbix_server /etc/rc.d/init.d/ [root@node0 ~]# vim /etc/rc.d/init.d/zabbix_server ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_server" ##注意:根据自身环境修改此变量。 [root@node0 ~]# chmod +x /etc/rc.d/init.d/zabbix_server

为Agent端:提供服务脚本:

[root@node0 ~]# cp ./zabbix-2.2.12/misc/init.d/fedora/core5/zabbix_agentd /etc/rc.d/init.d/ [root@node0 ~]# vim /etc/rc.d/init.d/zabbix_agentd ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd" ##注意:根据自身环境修改此变量。 [root@node0 ~]# chmod +x /etc/rc.d/init.d/zabbix_agentd

启动Server和Agent端:

[root@node0 ~]# chkconfig zabbix_server --add [root@node0 ~]# chkconfig zabbix_agentd --add [root@node0 ~]# chkconfig zabbix_server on [root@node0 ~]# chkconfig zabbix_agentd on [root@node0 ~]# service zabbix_server start [root@node0 ~]# service zabbix_agentd start

##在另一台Centos服务器上编译安装Zabbix-Agent: 安装开发环境:

[root@node1 ~]# yum groupinstall "Development tools"

创建zabbix用户:

[root@node1 ~]# useradd -r zabbix

编译安装:

[root@node1 ~]# tar xf zabbix-2.2.12.tar.gz [root@node1 ~]# cd zabbix-2.2.12 [root@node1 zabbix-2.2.12]# ./configure --prefix=/usr/local/zabbix --enable-agent [root@node1 zabbix-2.2.12]# make [root@node1 zabbix-2.2.12]# make install

添加并重读环境变量:

[root@node1 ~]# vim /etc/profile.d/zabbix.sh export PATH=/usr/local/zabbix/sbin:/usr/local/zabbix/bin:$PATH [root@node1 ~]# source /etc/profile.d/zabbix.sh [root@node1 ~]# zabbix_agentd -V Zabbix Agent (daemon) v2.2.12 (revision 59538) (20 April 2016)

导出man手册:

[root@node1 ~]# vim /etc/man.config MANPATH /usr/local/zabbix/share/man

编辑agent配置文件:

[root@node1 ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf Server=172.16.0.5 \\:本机IP; ServerActive=172.16.0.4 \\:Server端IP; Hostname=linux.zabbix.org

提供服务脚本:

[root@node1 ~]# cp ./zabbix-2.2.12/misc/init.d/fedora/core5/zabbix_agentd /etc/rc.d/init.d/ [root@node1 ~]# vim /etc/rc.d/init.d/zabbix_agentd ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd" ##注意:根据自身环境修改此变量。 [root@node1 ~]# chmod +x /etc/rc.d/init.d/zabbix_agentd

启动Agent端:

[root@node1 ~]# chkconfig zabbix_agentd --add [root@node1 ~]# chkconfig zabbix_agentd on [root@node1 ~]# service zabbix_agentd start