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

环境介绍:

先前环境:apache+php+nagios 除了nagios 其余的都使用yum自动安装
目标环境:nginx+php5.2.10(php-fpm patch)+nagios 均为源码编译安装

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

安装配置

yum install fcgi fcgi-devel -y

php-fpm 的配置与运行这里略过,下面主要讲解Spawn-fcgi+fcgiwrap 的安装与配置

spawn-fcgi

# wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz
# tar -xvzf spawn-fcgi-1.6.2.tar.gz
# cd spawn-fcgi-1.6.2/
# ./configure
# make
# cp src/spawn-fcgi /usr/local/bin/

fcgiwrap

# wget http://github.com/gnosek/fcgiwrap/tarball/master
# tar -xvzf gnosek-fcgiwrap-28ac6f9aa5e7dadf9aaf2062ab003a0fb4c82ad8.tar.gz
# cd gnosek-fcgiwrap-28ac6f9aa5e7dadf9aaf2062ab003a0fb4c82ad8/
# make
# mv fcgiwrap /usr/local/bin/

创建一个shell脚本来用spawn-fcgi 启动fcgiwrap实例
# vi /usr/local/bin/c-fcgi.sh

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

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

创建一个系统启动进程,方便使用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

www      20039     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwrap
www      20040     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwrap
www      20041     1  0 15:20 ?        00:00:00 /usr/local/bin/fcgiwrap

# ps -ef | grep php-cgi | grep -v grep

root     32629     1  0 13:37 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
.
.
.
www      32642 32629  0 13:37 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf

我php-fpm配置启动32个php-cgi 实例

配置nginx

user                www www;
worker_cpu_affinity 0001 0010 0100 1000;
worker_processes        4;

# 可以在下方直接使用 [ debug | info | notice | warn | error | crit ]  参数
error_log   /usr/local/nginx/logs/nginx_error.log   crit;

pid     /var/run/nginx.pid;

#进程所能打开的最大文件描述符个数
worker_rlimit_nofile    51200;

events
{
# linux 2.6+ 版本以上的内核推荐使用 epoll 事件模式
use             epoll;

# 最大连接数=worker_processes*worker_connections/4
worker_connections  4096;
}

http
{
include     ./conf.d/mime.types;
default_type    application/octet-stream;

charset utf-8;

server_names_hash_bucket_size   128;
client_header_buffer_size       32k;
large_client_header_buffers     4   32k;
client_max_body_size            8m;

sendfile        on;
tcp_nopush  on;

keepalive_timeout   60;

tcp_nodelay     on;

# global fastcgi configure
include         ./conf.d/fastcgi_custom.conf;

# include fastcgi_params.conf
include               ./conf.d/fastcgi_params.conf;

# gzip configure
include         ./conf.d/gzip.conf;

#limit_zone  crawler  $binary_remote_addr  10m;

server
{
listen          80;
server_name     cacti.opt.jobkoo.com;
index           index.html index.htm index.php;
root            /data/www/cacti;

#limit_conn   crawler  20;

location ~ .*\.(php|php5)?$
{
fastcgi_pass        127.0.0.1:9000;
fastcgi_index       index.php;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires     30d;
}

location ~ .*\.(js|css)?$
{
expires     1h;
}

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log  /usr/local/nginx/logs/access.log  access;
}

server
{
listen          80;
server_name     nagios.opt.jobkoo.com;
index           index.html index.htm index.php;
root            /usr/local/nagios/share/;

auth_basic      "Welcome to Jobkoo Nagios Monitor System!";
auth_basic_user_file    ./conf.d/htpasswd;

# 这个rewrite针对的是pnp插件的鼠标移动到图标后显示的浮动窗
rewrite     ^/nagios/pnp/(.*)\.php /pnp/$1.php break;

# 以 cgi 结尾的文件
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;

}

# 以 php/php5 结尾的文件
location ~ \.(php|php5)+$
{
fastcgi_pass        127.0.0.1:9000;
fastcgi_index       index.php;
}

log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log      /usr/local/nginx/logs/nagios.access.log  wwwlogs;
}

server
{
listen          80;
server_name     status.apt.jobkoo.com;

location / {
stub_status     on;
access_log      off;
}
}
}

/etc/nginx/conf.d 目录下的cgi 参数配置文件
#vi /etc/nginx/conf.d/fastcgi_custom.conf

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

# vi /etc/nginx/conf.d/fastcgi_params.conf

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

设置nagios页面身份验证
# htpasswd -c /etc/nginx/conf.d/htpasswd admin
# cat /etc/nginx/conf.d/htpasswd
admin:QqbxsY3jdkOpQ

配置nagios

#vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1

# service nagios start
# service nginx start

访问nagios页面
nagios_nginx.png

原创文章,转载请注明: 转自 http://salogs.com