阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Zabbix替换默认Web服务器httpd为Nginx

224次阅读
没有评论

共计 4039 个字符,预计需要花费 11 分钟才能阅读完成。

本身环境 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

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

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

Zabbix 替换默认 Web 服务器 httpd 为 Nginx

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

Zabbix 替换默认 Web 服务器 httpd 为 Nginx

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

到这里完成结束了。

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4039字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中