环境:CentOS 6.5(最小化安装)

Nginx:1.6.2

Nagios:3.3.1

 

安装基本环境:

[[email protected] software]# yum -y install gcc gcc-c++
[[email protected] software]# yum -y install perl-devel perl

源码安装PHP:

[[email protected] software]# yum -y install mysql mysql-devel
[[email protected] software]# yum -y install gd-2.0.35-11.el6.x86_64.rpm gd-devel-2.0.35-11.el6.x86_64.rpm
[[email protected] software]# yum -y install libxml2 libxml2-devel
[[email protected] software]# tar -zxvf zlib-1.2.7.tar.gz &>/dev/null
[[email protected] software]# cd zlib-1.2.7
[[email protected] zlib-1.2.7]# ./configure -prefix=/usr/local/zlib  &&make &&make install
[[email protected] software]# tar -zxvf libmcrypt-2.5.7.tar.gz &>/dev/null
[[email protected] software]# cd libmcrypt-2.5.7
[[email protected] libmcrypt-2.5.7]# ./configure &&make &&make install
[[email protected] software]# useradd -s /sbin/nologin www
[[email protected] software]# tar -jxvf php-5.3.8.tar.bz2 &>/dev/null
[[email protected] software]# cd php-5.3.8
[[email protected] php-5.3.8]# ./configure --prefix=/usr/local/php --enable-mbstring --enable-soap --enable-wddx=shared --enable-xml --with-gd --with-gettext --with-regex=system --with-zlib-dir=/usr/local/zlib --enable-fpm --with-jpeg-dir --with-fpm-user=www --with-fpm-group=www --enable-shmop --with-mcrypt --with-mysqli=mysqlnd
[[email protected] php-5.3.8]# make &&make install
[[email protected] ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[[email protected] ~]# sed -i '/run\/php-fpm.pid/s/^;//g' /usr/local/php/etc/php-fpm.conf
[[email protected] ~]# sed -i '/^;pm.min_spare_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
[[email protected] ~]# sed -i '/^;pm.max_spare_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
[[email protected] ~]# sed -i '/^;pm.start_servers/s/^;//g' /usr/local/php/etc/php-fpm.conf
[[email protected] ~]# /usr/local/php/sbin/php-fpm
[[email protected] ~]# echo '/usr/local/php/sbin/php-fpm'>>/etc/rc.local

[[email protected] ~]# netstat -tunlp |grep php

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      1592/php-fpm        

[[email protected] ~]# ps -ef|grep php|grep -v grep

root       1592      1  0 12:36 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)

www        1593   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1594   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1595   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1596   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1597   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1598   1592  0 12:36 ?        00:00:00 php-fpm: pool www

www        1599   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1600   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1601   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1602   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1603   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1604   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1605   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1606   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1607   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1608   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1609   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1610   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1611   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

www        1612   1592  0 12:36 ?        00:00:00 php-fpm: pool www          

[[email protected] ~]#

php启动错误解决方法:http://yfshare.blog.51cto.com/8611708/1635679

安装Nginx:

[[email protected] software]# tar -jxvf pcre-8.12.tar.bz2 &>/dev/null
[[email protected] software]# cd pcre-8.12
[[email protected] pcre-8.12]# ./configure &&make &&make install
zlib上面安装php时已安装
[[email protected] software]# tar -zxvf openssl-1.0.1c.tar.gz &>/dev/null
[[email protected] software]# cd openssl-1.0.1c
[[email protected] openssl-1.0.1c]# ./config &&make &&make install
[[email protected] software]# tar -zxvf nginx-1.6.2.tar.gz &>/dev/null
[[email protected] software]# cd nginx-1.6.2
[[email protected] nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-pcre=/software/pcre-8.12 --with-zlib=/software/zlib-1.2.7 --with-openssl=/software/openssl-1.0.1c --with-http_stub_status_module
[[email protected] nginx-1.6.2]# make &&make install

编写/etc/init.d/nginx文件

[[email protected] nginx-1.6.2]# /etc/init.d/nginx start

Starting nginx:                                            [  OK  ]

[[email protected] nginx-1.6.2]# 

[[email protected] nginx-1.6.2]# chkconfig --add nginx
[[email protected] nginx-1.6.2]# chkconfig nginx on
[[email protected] conf]# ln -s /usr/local/nagios/share /usr/local/nginx/html/nagios

[[email protected] conf]# ll /usr/local/nginx/html/

total 8

-rw-r--r-- 1 root root 537 Apr 18 10:59 50x.html

-rw-r--r-- 1 root root 612 Apr 18 10:59 index.html

lrwxrwxrwx 1 root root  23 Apr 18 12:43 nagios -> /usr/local/nagios/share

还需要自己编写nginx.conf、fastcgi.confcgi.confstart_perl_cgi.shperl-fcgi.pl

[[email protected] conf]#

给Nginx添加对Perl的CGI支持(默认不支持):

安装FCGI模块:

[[email protected] software]# tar -zxvf FCGI-0.73.tar.gz &>/dev/null
[[email protected] software]# cd FCGI-0.73
[[email protected] FCGI-0.73]# perl Makefile.PL &&make &&make install
[[email protected] software]# tar -zxvf FCGI-ProcManager-0.25.tar.gz &>/dev/null
[[email protected] software]# cd FCGI-ProcManager-0.25
[[email protected] FCGI-ProcManager-0.25]# perl Makefile.PL &&make &&make install
[[email protected] software]# tar -zxvf IO-1.25.tar.gz &>/dev/null
[[email protected] software]# cd IO-1.25
[[email protected] IO-1.25]# perl Makefile.PL &&make &&make install
[[email protected] software]# tar -zxvf IO-All-0.39.tar.gz &>/dev/null
[[email protected] software]# cd IO-All-0.39
[[email protected] IO-All-0.39]# perl Makefile.PL &&make &&make install
[[email protected] software]# tar -zxvf spawn-fcgi-1.6.3.tar.gz &>/dev/null
[[email protected] software]# cd spawn-fcgi-1.6.3
[[email protected] spawn-fcgi-1.6.3]# ./configure &&make &&make install
[[email protected] software]# unzip perl-fcgi.zip
[[email protected] software]# cp perl-fcgi.pl /usr/local/nginx/
[[email protected] software]# chmod 755 /usr/local/nginx/perl-fcgi.pl
[[email protected] software]# cd /usr/local/nginx/
[[email protected] nginx]# chmod 755 start_perl_cgi.sh

编写start_perl_cgi.sh脚本

[[email protected] nginx]# ./start_perl_cgi.sh start

start perl-fcgi done

[[email protected] nginx]# ll /usr/local/nginx/logs/perl-fcgi.sock

srwxr-xr-x 1 www www 0 Apr 18 11:02 /usr/local/nginx/logs/perl-fcgi.sock

[[email protected] nginx]# echo "/bin/sh /usr/local/nginx/start_perl_cgi.sh start" >>/etc/rc.local

安装Nagios:

[[email protected] software]# yum -y install libgd2-noxpm libgd2-noxpm-dev
[[email protected] software]# yum -y install php-gd
[[email protected] software]# groupadd nagcmd
[[email protected] software]# useradd -G nagcmd nagios
[[email protected] software]# usermod -G nagcmd www
[[email protected] software]# tar -zxvf nagios-3.3.1.tar.gz &>/dev/null
[[email protected] software]# cd nagios
[[email protected] nagios]# ./configure --with-command-group=nagcmd --prefix=/usr/local/nagios --with-gd-lib=/usr/lib --with-gd-nc=/usr/include
[[email protected] nagios]# make all &&make install &&make install-init &&make install-config &&make install-commandmode
[[email protected] nagios]# yum -y install httpd-tools
[[email protected] nagios]# cd /usr/local/nagios/etc/
[[email protected] etc]# htpasswd -cm htpasswd.users yfshare
[[email protected] etc]# sed -i '/authorized\_for\_/s/nagiosadmin/&\,yfshare/g' cgi.cfg
[[email protected] etc]# cd /usr/local/nginx/conf/
[[email protected] etc]# mkdir /usr/local/nagios/logs

[[email protected] etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg |grep ^Total

Total Warnings: 0

Total Errors:   0

[[email protected] etc]# /etc/init.d/nagios start

Starting nagios: done.

[[email protected] etc]# chkconfig --add nagios
[[email protected] etc]# chkconfig nagios on

也可通过访问http://www.4webhelp.net/us/password.php生成htpasswd.users

基于Nginx平台部署Nagios监控系统

[[email protected] etc]# /etc/init.d/nagios restart

Running configuration check...done.

Stopping nagios: done.

Starting nagios: done.

[[email protected] etc]#

[[email protected] conf]# /etc/init.d/nginx restart

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Stopping nginx:                                            [  OK  ]

Starting nginx:                                            [  OK  ]

[[email protected] conf]#

安装Nagios-plugin:

[[email protected] software]# tar -zxvf nagios-plugins-1.4.15.tar.gz &>/dev/null
[[email protected] software]# cd nagios-plugins-1.4.15
[[email protected] nagios-plugins-1.4.15]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
[[email protected] nagios-plugins-1.4.15]# make &&make install
[[email protected] software]# ls /usr/local/nagios/libexec/|wc -l
58
[[email protected] software]#

安装NRPE:

[[email protected] software]# yum -y install openssl openssl-devel
[[email protected] software]# tar -zxvf nrpe-2.13.tar.gz &>/dev/null
[[email protected] software]# cd nrpe-2.13
[[email protected] nrpe-2.13]# ./configure
[[email protected] nrpe-2.13]# make all &&make install-plugin &&make install-daemon &&make install-daemon-config
[[email protected] nrpe-2.13]# cd /usr/local/nagios/etc/
[[email protected] etc]# sed -i '/allowed_host/s/127.0.0.1/&,192.168.10.99/g' nrpe.cfg
[[email protected] etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[[email protected] etc]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local

[[email protected] etc]# /usr/local/nagios/libexec/check_nrpe -H localhost

NRPE v2.13

[[email protected] etc]#

访问:http://192.168.10.99/nagios

基于Nginx平台部署Nagios监控系统

但是,打开页面时,其他选项卡都是显示的502 Bad Gateway

[[email protected] ~]# chown nagios:nagcmd /usr/local/nagios/ -R

[[email protected] ~]# chown www:nagcmd /usr/local/nginx/ -R

[[email protected] ~]# cd /usr/local/nginx/


[[email protected] nginx]# ./start_perl_cgi.sh start

 ERROR PID file /usr/local/nginx/logs/perl-fcgi.pid already exists

start perl-fcgi done

[[email protected] nginx]#

因为perl-fcgi进程意外中断,但是进程未退出变成了孤儿进程,所以就变成这样了

修改下脚本start_perl_cgi.sh,把这个脚本写成服务或者加入开机脚本(关闭sudo的tty)

[[email protected] nginx]# ./start_perl_cgi.sh start

start perl-fcgi done

[[email protected] nginx]#

[[email protected] nginx]# /etc/init.d/nagios restart

Running configuration check...done.

Stopping nagios: done.

Starting nagios: done.

[[email protected] nginx]# /etc/init.d/nginx restart

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Stopping nginx:                                            [  OK  ]

Starting nginx:                                            [  OK  ]

[[email protected] nginx]#

基于Nginx平台部署Nagios监控系统

OK,在Nginx上安装Nagios成功

如果出现上面的问题,解决方法:

[[email protected] ~]# sed -i '/use_authentication/s/1/0/g' /usr/local/nagios/etc/cgi.cfg

[[email protected] ~]# /etc/init.d/nagios restart

Running configuration check...done.

Stopping nagios: done.

Starting nagios: done.

[[email protected] ~]# /etc/init.d/nginx restart

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Stopping nginx:                                            [  OK  ]

Starting nginx:                                            [  OK  ]

[[email protected] ~]#

访问http://192.168.10.99/nagios/

基于Nginx平台部署Nagios监控系统

OK,Successful