免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 15904 | 回复: 1
打印 上一主题 下一主题

nginx+nagios3.1.2支持perl cgi ,php cgi 简要配置 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-19 22:11 |只看该作者 |倒序浏览
基于Nginx的nagios-3.1.2简要配置。此文主要关注于Nginx的perl-cgi和php-cgi的安装及设置。
本文基于 http://www.lazysa.com/2009/05/392.htmlhttp://www.thismail.org/bbs/thread-3002-1-1.html 改进。
我的主机环境
操作系统:debian 5.0
web服务器:Nginx 6.32
CGI-perl环境:nginx-fcgi
CGI-php环境:FastCGI
nagios版本:nagios 3.1.2
nagios 的安装在这里就不谈了,你可以看看这个:
http://nagios-cn.sourceforge.net/nagios-cn/
先说说我对nginx解析cgi脚本流程的理解:
      ㈡          ㈢         ㈣
nginx---->fastcgi---->result---->nginx
↑㈠    (perl,php)                        ↓㈤
client                                        client
在fastcgi中,有两种处理cgi的模式:sock 和tcp协议。

下面安装并配置Nginx
在debian下,很方便
apt-get install nginx
现在开始配置PHP CGI支持。
apt-get install lighttpd  ,安装lighttpd,我们需要它的spawn-fcgi
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi #这里就是建立PHP CGI 的监听后台进程,响应目的端口为9000的请求。spawn-fcig 参数解释: -a 监听地址,-p 端口,-C 启动N个监听进程,-u 启动进程的用户名, -f 处理php解析的程序 。
在 /etc/nginx/sites-available/default 中加入:
        location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;       #这里是监听PHP解析请求的设置。9000为端口号,127.0.0.1 就不用说了吧? 当然也可以填其他的主机IP,但得有相应服务可以访问才行。
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;  
在nginx中建立虚拟站点(主机)。
在 /etc/nginx/conf.d 中
touch nagios.conf 并加入以下内容:

server
       {
               listen       80;
               server_name  www.nagios-t.com;          #绑定的虚拟主机站定域名。当访问www.nagios-t.com时,自动在下面 root 定义的目录中寻找index文件。
               index index.php index.html index.htm;
               root  /usr/local/nagios/share;
               location ~ .*\.php?$
               {
                        gzip off;
                       include /etc/nginx/conf.d/enable_php5.conf;
                        fastcgi_pass  127.0.0.1:9000;    #这里是解析php cgi的脚本接口。
                        fastcgi_index index.php;
               }
                location /nagios/
               {
                gzip off;
                alias /usr/local/nagios/share/;            #nginx中的别名设置,相当于apache中的虚拟目录
                index index.html index.htm index.php;
             }
enable_php5.conf 跟 /etc/nginx/fastcgi_params 的内容一样。可以去掉,但我没测试。
在 /etc/hosts 中加入 www.nagios-t.com 127.0.0.1
现在你可以重启nagios 和nginx ,应该可以看到左边的列表了。


CGI-perl环境:
获取perl fastcgi脚本。
wget http://www.nginx.eu/nginx-fcgi/nginx-fcgi.txt
首先安装Perl的FCGI模块
   1.wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.67.tar.gz
   2.tar -zxvf FCGI-0.67.tar.gz
   3.cd FCGI-0.67
   4. perl Makefile.PL
   5. make && make install
还可以使用如下方法安装:
perl -MCPAN -e 'install FCGI'
安装FCGI-ProcManager
   1. wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.18.tar.gz
   2. tar -xzxf FCGI-ProcManager-0.18.tar.gz
   3. cd FCGI-ProcManager-0.18
   4. perl Makefile.PL
   5. make
   6. make install
安装 perl IO ALL 这个模块。
apt-get install libio-all-perl
cp nginx-fcgi.txt /usr/sbin/nginx-fcgi
chmod +x /usr/sbin/nginx-fcgi
注意:要使用启动Nginx的用户来运行下面这个指令
我的Nginx运行用户是www-data ,若你不确定,可以 cat /etc/nginx/nginx.conf |grep user ,可以看到结果(apt安装)。
su www-data  /usr/sbin/nginx-fcgi -l /var/log/nginx/nginx-fcgi.log -pid /var/run/nginx-fcgi.pid -S /var/run/nginx-fcgi.sock
且保证 nginx-fcgi.sock 的权限为nginx和nagios可读、写、执行的,我当初就卡在这里,瞎折腾了17个小时。
如果执行上面的指令提示权限不足的问题,就把数据放在/tmp 这个目录。这样做:
     使用 root 用户:chown www-data:www-data /tmp
                  chmod -R 777 /tmp        ,把这个目录的权限设成777是因为 nagios 和nginx都要读取 nginx-fcgi.sock,或者你可以把运行  nagios和nginx的用户加在一个组,并chmod -R 770 /tmp 也可以 。
     再次 su www-data  /usr/sbin/nginx-fcgi -l /tmp/nginx-fcgi.log -pid /tmp/nginx-fcgi.pid -S /tmp/nginx-fcgi.sock
      
我们需要的nginx 的配置文件有
/etc/nginx/sites-available/nginx.conf  
/etc/nginx/sites-available/default
/etc/nginx/conf.d/nagios.conf
nginx.conf内容:
user www-data;
worker_processes  3;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
events {
    worker_connections  3;
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
     access_log /var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;
    tcp_nodelay        on;
    gzip  on;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
##参数很少,但我只跑一个nagios的web。


/etc/nginx/conf.d/nagios.conf 内容
server
       {
               listen       80;
               server_name  www.nagios-t.com;          #虚拟主机
               index index.php index.html index.htm;
               root  /usr/local/nagios/share;
               location ~ .*\.php?$
               {
                        gzip off;
                       include /etc/nginx/conf.d/enable_php5.conf;
                        fastcgi_pass  127.0.0.1:9000;    #这里是解析php cgi的脚本接口。
                        fastcgi_index index.php;
               }
                location /nagios/
               {
                gzip off;
                alias /usr/local/nagios/share/;            #nginx中的别名设置,相当于apache中的虚拟目录
                index index.html index.htm index.php;

location ~ \.cgi$ {
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;   #这个我解释不了 :(
fastcgi_pass unix:/tmp/nginx-fcgi;                  #解析perl cgi 所用的sock接口。
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name; #要解析的脚本位置
fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;
include /etc/nginx/fastcgi_params;
}
###############################################绿色字体部分为解析perl cgi脚本的设置。
location ~ \.pl$ {
               fastcgi_pass  unix:/tmp/nginx-fcgi.sock;
               fastcgi_index index.pl;
               fastcgi_param SCRIPT_FILENAME  /usr/local/nagios/sbin$fastcgi_script_name;
               include /etc/nginx/fastcgi_params;
           }

/etc/nginx/conf.d/enable_php5.conf; 内容
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_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  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
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;
fastcgi_param  REDIRECT_STATUS    200;

最后,把 /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www -f /usr/bin/php-cgi
        su www-data  /usr/sbin/nginx-fcgi -l /tmp/nginx-fcgi.log -pid /tmp/nginx-fcgi.pid -S /tmp/nginx-fcgi.sock
      放入/etc/rc.local
重启nagios,nginx 。
###################################################################
如果要求nginx下所有的站点都可以支持 perl 、php 的cgi ,那么可以把下面的代码加入 /etc/nginx/sites-available/default 中 。

               location ~ .*\.php?$
               {
                        gzip off;
                       include /etc/nginx/conf.d/enable_php5.conf;
                        fastcgi_pass  127.0.0.1:9000;    #这里是解析php cgi的脚本接口。
                        fastcgi_index index.php;
               }

location ~ \.cgi$ {
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;   
fastcgi_pass unix:/tmp/nginx-fcgi;                 
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #要解析的脚本位置
fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;
include /etc/nginx/fastcgi_params;
location ~ \.pl$ {
               fastcgi_pass  unix:/tmp/nginx-fcgi.sock;
               fastcgi_index index.pl;
               fastcgi_param SCRIPT_FILENAME  /usr/local/nagios/sbin$fastcgi_script_name;
               include /etc/nginx/fastcgi_params;
               
           }

并把 /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www -f /usr/bin/php-cgi
        su www-data  /usr/sbin/nginx-fcgi -l /tmp/nginx-fcgi.log -pid /tmp/nginx-fcgi.pid -S /tmp/nginx-fcgi.sock
      放入/etc/rc.local
如果排版有问题,请看附件

有问题亦可联系 nagios.free@gmail.com

[ 本帖最后由 357202171 于 2009-7-19 22:17 编辑 ]

Nginx+perl+php+Nagios.rar

7.03 KB, 下载次数: 257

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-07-05 06:20:00
2 [报告]
发表于 2013-09-13 14:10 |只看该作者
好文,比较有用。我来支持
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP