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

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

到些完成結束了!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章