本文基于《LNMP最新源码安装脚本》,Nagios依赖PHP环境和perl环境,由于Nginx不支持Perl的CGI,需先来搭建Perl环境,Nagios原理介绍略。
1、下载最新稳定源码包和Perl脚本
wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.74.tar.gz
wget http://www.cpan.org/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz
wget http://blog.linuxeye.com/wp-content/uploads/2013/04/perl-fcgi.pl
wget http://jaist.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.5.0/nagios-3.5.0.tar.gz
wget http://nchc.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/nagios-plugins-1.4.16.tar.gz
wget http://nchc.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz
2、Nginx对Perl的CGI支持
Nginx支持Perl的CGI方法有好几种,基本原理都是通过Perl的FCGI模块实现,下面的方法就是其中一种:
安装FCGI模块
tar xzf FCGI-0.74.tar.gz
cd FCGI-0.74
perl Makefile.PL
make && make install
cd ../
安装FCGI-ProcManager模块
tar xzf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
perl Makefile.PL
make && make install
cd ../
安装IO模块
tar xvf IO-1.25.tar.gz
perl Makefile.PL
make && make install
cd ..
安装IO-All模块
tar xvf IO-All-0.39.tar.gz
cd IO-All-0.39
perl Makefile.PL
make && make install
启动方法一:
【
Perl脚本
cp perl-fcgi.pl /usr/local/nginx
chmod +x /usr/local/nginx/perl-fcgi.pl
/usr/local/nginx/perl-fcgi.pl > /usr/local/nginx/logs/perl-fcgi.log 2>&1 & #启动Perl
chmod 777 /usr/local/nginx/logs/perl-fcgi.sock #Nginx Log中提示Permision Denied方法
】
启动方法二:
下载nginx-fcgi脚本
Wget http://www.nginx.eu/nginx-fcgi/nginx-fcgi.txt
#mv nginx-fcgi.txt /usr/sbin/nginx-fcgi
#chmod +x /usr/sbin/nginx-fcgi
mv nginx-fcgi.txt /usr/local/nginx/sbin/nginx-fcgi (我的nginx安装位置)
chmod +x /usr/local/nginx/sbin/nginx-fcgi
如果不用 sudo 方式运行 nginx-fcgi,请注释掉 nginx-fcgi 脚本中的:
if ( $> == "0" ) {
print "\n\tERROR\tRunning as a root!\n";
print "\tSuggested not to do so !!!\n\n";
exit 1;
}
启动 nginx-fcgi:
/usr/local/nginx/sbin/nginx-fcgi -l /usr/local/nginx/logs/nginx-fcgi.log -pid /usr/local/nginx/logs/nginx-fcgi.pid -S /usr/local/nginx/logs/nginx-fcgi.sock
chown nginx:nginx /usr/local/nginx/logs/nginx-fcgi.sock
注意一定要为 socket 添加 nginx 帐户的权限,否则 cgi 会执行失败。
运行前使用cpan 安装 FCGI; Getopt::Long; Socket;
设置开机启动(添加到/etc/rc.loacl文件)
/bin/rm -rf /usr/local/nginx/logs/nginx-fcgi.pid
/usr/local/nginx/sbin/nginx-fcgi -l /usr/local/nginx/logs/nginx-fcgi.log -pid /usr/local/nginx/logs/nginx-fcgi.pid -S /usr/local/nginx/logs/nginx-fcgi.sock
chmod 777 /usr/local/nginx/logs/nginx-fcgi.sock
新建 nginx-fcgi 脚本指令配置,直接从 fastcgi_params 复制模板:
cp /etc/nginx/fastcgi_params /etc/nginx/nginx_fcgi_params
去除尾部的:
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
新建示例 cgi 站点配置:
server {
listen 80;
server_name test.local;
location ~ ^/cgi-bin/.*\.cgi$
{
root /work/www/test;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include nginx_fcgi_params;
fastcgi_read_timeout 5m;
fastcgi_pass unix:/var/run/nginx-fcgi.sock;
}
}
重启 nginx,cgi配置完成。
3、Nagios安装(服务端)
useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd www
tar xzf nagios-3.5.0.tar.gz
cd nagios
yum -y install gd gd-devel
./configure --prefix=/usr/local/nagios --with-command-group=nagcmd --with-htmurl=/ –with-cgiurl=/cgi-bin
make all
make install #用于安装主要的程序、CGI及HTML文件
make install-init #用于生成init启动脚本
make install-config #用于安装示例配置文件
make install-commandmode #用于设置相应的目录权限
chkconfig --add nagios
chkconfig nagios on
cd ../
Nagios主程序只是提供一个运行框架,其具体监控是靠运行在其下的插件完成的,Nagios插件必须安装
tar xzf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make && make install
cd ../
配置Nagios Web界面登陆帐号及密码
借用Apache的htpasswd命令工具
yum install httpd
service httpd stop && chkconfig httpd off #不启用Apache
/usr/local/apache/bin/htpasswd -nb nagiosadmin 123456 > /usr/local/nagios/etc/htpasswd.users
生成加密文件
cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:wqx7mC26z5vKs
* 修改配置文件
打开 /usr/local/nagios/etc/cgi.cfg,修改 use_authentication 为:
use_authentication=0
即去除用户验证,然后重启 nagios。
打开 /usr/local/nagios/share/config.inc.php,把:
$cfg[‘cgi_base_url‘]=‘/nagios/cgi-bin‘;
改为(根据自己的实际情况修改):
$cfg[‘cgi_base_url‘]=‘/cgi-bin‘;
如果报错:Error: No such CGI app - /usr/local/nagios/sbin/cgi-bin/status.cgi may not exist or is not executable by this process.
则一般都是这里的错误,此处修改的位置根据上面configure编译nagios时定义–with-cgiurl=/cgi-bin修改,本人的这次安装不知道哪里出问题,编译nagios带参数–with-cgiurl=/cgi-bin,此处却必须为 $cfg[‘cgi_base_url‘]=‘/nagios/cgi-bin‘;不能修改,可能带的参数编译不成功
启动前先检查下配置文件是否正确
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果没有报错,可以启动Nagios服务
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
【或者/etc/init.d/nagios start】
查看nagios监控状态
/usr/local/nagios/bin/nagiostats
nginx配置文件:
vim /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes 10;
error_log /data/logs/nginx_error.log crit;
#pid logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gbk;
server_names_hash_bucket_size 128;
client_header_buffer_size 16k;
large_client_header_buffers 4 32k;
#client_max_body_size 8m;
server_tokens off;
expires 1h;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
error_page 404 /;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 8 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /dev/shm;
gzip on;
gzip_min_length 2048;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/xml application/x-javascript ;
log_format access ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" $http_x_forwarded_for‘;
################# include ###################
# server
# {
# listen 81;
# server_name empty;
# root /data/web/webclose;
# }
server {
listen 80;
server_name test.local;
location ~ ^/cgi-bin/.*\.cgi$
{
root /work/www/test;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include nginx_fcgi_params;
fastcgi_read_timeout 5m;
fastcgi_pass unix:/var/run/nginx-fcgi.sock;
}
}
server
{
listen 81;
server_name 71.69.ming.cn ;
index index.html index.htm index.php;
root /data/web/awstats/www;
location /status/ {
stub_status on;
access_log off;
expires off;
}
location ~ .*\.php$
{
include fcgi.conf;
fastcgi_pass 127.0.0.1:10080;
fastcgi_index index.php;
expires off;
}
access_log /data/logs/71.69.ming.cn.log access;
}
include block_ips.conf ;
include vhost/nagios.conf ;
}
vim /usr/local/nginx/conf/vhost/nagios.conf
server
{
listen 80;
server_name nagios.frsy.com;
access_log /usr/local/nginx/logs/access.log;
root /usr/local/nagios/share;
index index.php index.html index.htm;
location ~ .*\\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
auth_basic "Nagios Login pain";
auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
}
location ~ .*\\.cgi$ {
root /usr/local/nagios/sbin;
rewrite ^/nagios/cgi-bin/(.*)\\.cgi /$1.cgi break;
fastcgi_pass unix:/usr/local/nginx/logs/nginx-fcgi.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin/$fastcgi_script_name;
fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;
include fcgi.conf;
auth_basic "Nagios Login pain";
auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
}
location /nagios/ {
alias /usr/local/nagios/share/;
}
}
vim /usr/local/nginx/conf/fcgi.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;
或者
(nginx + php-fpm页面显示空白的问题http://blog.sina.com.cn/s/blog_67d781d20101nhy0.html)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
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 REMOTE_USER $remote_user;
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;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;