首页 > 代码库 > nagios平台从apache迁移到nginx

nagios平台从apache迁移到nginx

nginx的性能远远优于apache,但由于nagios的web界面中包含php和c-cgi程序,因此需要两套fcgi管理工具(并非必须)和两套解释器(必须)。php用php-cgi跑就可以,c-cgi我选用fcgiwrap。下面介绍安装/配置步骤。


php-fpm:是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi
Spawn-fcgi:是lighttpd的一个分支项目,是一个cgi进程的管理器

● php 打php-fpm补丁,编译时启用--enable-fastcgi --enable-fpm 参数,使用php-fpm管理php-cgi。php安装详细步骤参见 张宴文章:http://blog.s135.com/nginx_php_v5/
● c-cgi 使用 Spawn-fcgi 管理 ,利用fcgiwrap驱动。fcgiwrap 介绍参见http://nginx.localdomain.pl/wiki/FcgiWrap

php-cgi 监听 127.0.0.1:9000
fcgiwrap 监听 127.0.0.1:10000

nagios 安装配置不是本文重点,略过。web 目录如下:
/usr/local/nagios/share

安装spawn-fcgi

wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.3.tar.gz
tar xf spawn-fcgi-1.6.3.tar.gz
cd  /usr/local/src/spawn-fcgi-1.6.3 
./configure  --prefix=/usr/local/spawn-fcgi
make && make install

安装fcgi库

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/fcgi-2.4.0-10.el6.x86_64.rpm
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/fcgi-devel-2.4.0-10.el6.x86_64.rpm
rpm -ivh fcgi-2.4.0-10.el6.x86_64.rpm
rpm -ivh fcgi-devel-2.4.0-10.el6.x86_64.rpm

以上fcgi软件的rpm为RHEL6对应版本的,如果是5系列请安装RHEL5对应版本的fcgi库,RHEL5软件下载地址如下:

fcgi: http://flexbox.sourceforge.net/centos/5/x86_64/fcgi-2.4.0-10.el5.x86_64.rpm

fcgi-devel:http://flexbox.sourceforge.net/centos/5/x86_64/fcgi-devel-2.4.0-10.el5.x86_64.rpm

安装fcgiwrap

fcgiwrap wiki --> http://nginx.localdomain.pl/wiki/FcgiWrap

最新版本为gnosek-fcgiwrap-1.1.0-0-g333ff99.tar.gz

cd gnosek-fcgiwrap-333ff99/
autoreconf -i
./configure  --prefix=/usr/local/fcgiwrap
make && make install

创建一个shell脚本来用spawn-fcgi 启动fcgiwrap实例

vi /usr/local/bin/c-fcgi.sh

#!/bin/sh
/usr/local/spawn-fcgi/bin/spawn-fcgi -f /usr/local/fcgiwrap/bin/fcgiwrap -a 127.0.0.1 -p 10000-F 3 -P /var/run/fastcgi-c.pid -u daemon -g daemon

chmod +x /usr/local/bin/c-fcgi.sh

参数含义如下:

-f <fcgiapp> 指定调用FastCGI的进程的执行程序位置

-a <addr> 绑定到地址addr

-p <port> 绑定到端口port

-s <path> 绑定到unixsocket的路径path

-C < children> 指定产生的FastCGI的进程数(仅用于PHP)

-F <children> 指定产生的FastCGI的进程数

-P <path> 指定产生的进程的PID文件路径

-u和-g FastCGI使用什么身份(-u用户-g用户组)运行,这里使用nginx的用户和组daemon运行


创建一个系统启动进程,方便使用service 和chkconfig 命令管

vi /etc/init.d/c-fcgi

#!/bin/bash
# c-fcgi - this script starts and stops the fcgiwrap instance
#
# chkconfig:   - 96 28
# description:  c-fcgi
# processname: c-fcgi

C_SCRIPT=/usr/local/bin/c-fcgi.sh

RETVAL=0

case "$1" in
start)
echo "Starting fastcgi"
$C_SCRIPT
RETVAL=$?
;;
stop)
echo "Stopping fastcgi"
killall -9 fcgiwrap
RETVAL=$?
;;
restart)
echo "Restarting fastcgi"
killall -9 fcgiwrap
$C_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: c-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
# <span style="color: #0000ff;">chkconfig --add c-fcgi</span>
# <span style="color: #0000ff;">chkconfig c-fcgi on</span>
# <span style="color: #0000ff;">service c-fcgi start</span>

验证启动,是否提供了相应的端口

#netstat -tulnp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      32629/php-cgi
tcp        0      0 127.0.0.1:10000             0.0.0.0:*                   LISTEN      20039/fcgiwrap
#ps -ef | grep fcgiwrap | grep -v grep

daemon      20039     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwrap
daemon      20040     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwrap
daemon      20041     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwr
#ps -ef | grep php-cgi | grep -v grep

nginx    22046     1  0 Jul10 ?        00:00:00 /usr/bin/php-cgi
nginx    22047 22046  0 Jul10 ?        00:00:00 /usr/bin/php-cgi
nginx    22048 22046  0 Jul10 ?        00:00:00 /usr/bin/php-cgi
nginx    22049 22046  0 Jul10 ?        00:00:00 /usr/bin/php-cgi
nginx    22050 22046  0 Jul10 ?        00:00:00 /usr/bin/php-cgi
nginx    22051 22046  0 Jul10 ?        00:00:00 /usr/bin/php-cgi

配置nginx相关nagios设置

server {
        listen 80;
        server_name nagios.icoffer.cn;
        root /usr/local/nagios/share;
        index index.php;
        auth_basic      "Welcome to Jobkoo Nagios Monitor System!";
        auth_basic_user_file    ./htpasswd;
        
        location /nagios{
        alias /usr/local/nagios/share/;
        }

        location ~ .*\.cgi?$ {
        root /usr/local/nagios/sbin;
        rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
        fastcgi_pass  127.0.0.1:10000;
        fastcgi_index index.cgi;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include         fastcgi_params;
        }

        location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 500;
        include        fastcgi_params;
        fastcgi_buffers 8 128k;
        }
}

配置Nagios Nginxweb认证

htpasswd -c /etc/nginx/conf.d/htpasswd admin
cat /etc/nginx/conf.d/htpasswd
admin:QqbxsY3jdkOpQ

若没有htpasswd命令,请安装httpd-tools包

nagios平台从apache迁移到nginx