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
    #
   
    # 配置nginx和php-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
    #
    # 配置nginx和php-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

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

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

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

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

到这里完成结束了。

linuxboy的RSS地址:https://www.linuxboy.net/rssFeed.aspx

本文永久更新链接地址:https://www.linuxboy.net/Linux/2019-07/159396.htm

相关内容