微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

zabbix替换默认web服务器httpd为nginx

本身环境zabbix之前是采用的lamp环境rpm包去安装zabbix的。现在要换成Nginx做为web服务。

替换思路 : zabbix的web服务是用PHP写的,httpd 只是一个web服务器。有了替换思路我们就进行下一步,我们首先找到PHP程序存放的目录。


找到zabbix.conf并打开文件 /etc/httpd/conf.d/zabbix.conf,根据路径来看不难判断这个文件应该就是httpd配置文件,打开文件根据Directory可以判     断/usr/share/zabbix为程序所在目录。

找到zabbix程序所在目录后,我们就着手配置Nginx就好了,进入Nginx的配置目录并打开 /etc/Nginx/conf.d/default.conf文件(或者另外创建一个zabbix.conf 的文件

安装好lnmp环境,Nginx是基于PHP-fpm,rhel7.4只有PHP相关rpm包,但没有PHP-fpm的rpm包,所以需要自己下载相应版本的PHP-fpm的rpm包并安装,

zabbix不想放在网站根目录下,这样不容易和网站应用混在一起,这样zabbix的目录就放在别处,在Apache里,有alias,比较方便,在Nginx下没有虚拟目录概念的,是用location配合alias使用,但使用alias标签的目录块中不能使用rewrite的break。我先试了简单的配置方式:

编辑default.conf为下面的内容

一、采用别名配置方法一:

# vi /etc/Nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/Nginx/host.access.log  main;
    
采用别名zabbix方式:http://IP/zabbix,这样去访问,就不用Nginx认/目录了
    location /zabbix {
        alias   /usr/share/zabbix;  #是zabbix前端的PHP文件所在目录
        index  index.html index.htm index.PHP;
    }
    
    
    #设置下面几个目录 不允许外部访问
    location ^~ /app {
        deny all;
    }
    
    location ^~ /conf {
        deny all;
    }
    
    location ^~ /local {
        deny all;
    }
    
    location ^~ /include {
        deny all;
    }
    
    
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/Nginx/html;
    }
    
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.PHP$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    
    # 配置NginxPHP-fpm通信
    # 我们使用端口的形式来进行通讯
    # 前面注释去掉并修改成你需要的
    location ~ ^/zabbix/.+\.PHP$ {
        #fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.PHP;
        fastcgi_param  SCRIPT_FILENAME  /usr/share$fastcgi_script_name;
        include        fastcgi_params;
    }
    
    # deny access to .htaccess files, if Apache's document root
    # concurs with Nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

配置好之后保存文件

启动服务:

systemctl start PHP-fpm
systemctl restart Nginx
systemctl restart zabbix-server zabbix-agent

开机启动:

systemctl enable PHP-fpm
systemctl enable zabbix-server zabbix-agent Nginx

二、采用别名配置方法二:

# vi /etc/Nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/Nginx/host.access.log  main;
    
    
    location /zabbix {
        alias   /usr/share/zabbix;
        index  index.html index.htm index.PHP;
    }
    
    #设置下面几个目录 不允许外部访问
    location ^~ /app {
        deny all;
    }
    
    location ^~ /conf {
        deny all;
    }
    
    location ^~ /local {
        deny all;
    }
    
    location ^~ /include {
        deny all;
    }
    
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/Nginx/html;
    }
    
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.PHP$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    # 配置NginxPHP-fpm通信
    # 我们使用端口的形式来进行通讯
    #此方法二原理应该是采用rewrite的方法,对于/zabbix/下PHP类型的的请求交给后端的FastCGI处理,
      #并且指定了PHP脚本的位置,这样我们就可以配置zabbix了,配置如下:
    location ~ ^/zabbix/.+\.PHP$ {
        root /usr/share;
        rewrite /zabbix/(.*\.PHP?) /$1 break;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.PHP;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/zabbix$fastcgi_script_name;
        include        fastcgi_params;
    }
    
    location ~ .*\.(PHP|PHP5)?$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.PHP;
    }
    
    # deny access to .htaccess files, if Apache's document root
    # concurs with Nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
    
}


要注意的是:
location ~ .*\.(PHP|PHP5)?$ {
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.PHP;
include fcgi.conf;
}

这段,要放在location ~ ^/zabbix/.+\.PHP$的后面,放在前面就有问题,这是和Nginx的location规则有关,具体看Nginx的文档,
另外,zabbix里要配置一下URI的绝对路径,就可以了。

三、访问zabbix服务:http:/IP/zabbix

image.png

到上面为止,我们就替换zabbix认web服务器httpd为Nginx。但是我们还没有结束,是的,还没有结束!!!

我们登录后可能会出现如下报错,这个是需要设置PHP.ini参数date.timezone设置PHP认时区,设置好后点重试,即可打开首页

zabbixn.jpg

跳转首页,右下角dashboard模块下 Status of Zabbix 有几个红色的异常

zabbixn1.jpg

1、date.timezone => 没有设置PHP认时区

2、max_input_time 60 

3、max_execution_time 30

4、post_max_size    8M    

这四个是PHP配置问题,我们只需要编辑PHP.ini就好了

#vi /etc/PHP.ini
post_max_size = 16M
max_input_time = 300
max_execution_time = 300
date.timezone = Asia/Shanghai

到些完成结束了!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐