ZabbixのHTTPを httpd -> Nginxに変更する

一通り CentOS7 + MySQL 5.7 + Zabbix 3.4 + PHP 7.x で構築できたので
今更な Apache(httpd)じゃなくて、他のサーバーでも使っている Nginx に変更

1. Zabbix Server と httpd の停止
# systemctl stop zabbix-server
# systemctl stop httpd
2. Nginxのインストール

Nginxのサイトにしたがい https://nginx.org/en/linux_packages.html#mainline
mainlineのレポジトリを追加

# cat > /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
<ここで Ctrl+D で出力終了>
# chmod 644 /etc/yum.repos.d/nginx.repo
#
# yum install nginx

nginxを起動してWelcomページが表示されるか確認

# systemctl start nginx

ブラウザから、サーバーのIPを叩いて表示されること
https://i.imgur.com/tD0jR4Y.png

3. PHPのモジュールをインストール

既に入っているものもあるが、次回構築時に判るようにするために

# yum install php-mbstring php-pdo php-xml php-zip php-fpm php-mysqlnd
4. PHP-fpmの設定変更

apach向けになっているので、Nginxに変更

# vi /etc/php-fpm.d/www.conf
user = nginx
group = nginx

セッションファイルのディレクトリのパーミッション変更

# chown -R nginx:nginx /var/lib/php/session/

PHP-fpmの起動

# systemctl start php-fpm
5. Nginxの設定変更

/etc/nginx/conf.d 以下のdefault.conf をコピーして修正する
default.conf については、conf.bak などにリネームしておかないと /zabbix/ では失敗する

http://www.hogehoge.com/ で表示させる場合と
httpd の時と同様に http://www.hogehoge.com/zabbix/ で表示させる方法があるのだが
どちらが良いのかイマイチわからない

取り敢えず今回は後者の config

# cp -p /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/zabbix.conf
# vi /etc/nginx/conf.d/zabbix.conf
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /zabbix {
        alias   /usr/share/zabbix;
        index  index.php;
        if (!-e $request_filename) {
            rewrite ^/zabbix(.+)$ /zabbix/index.php?q=$1 last;
            break;
        }
    }

    location ~ ^/zabbix/.+\.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^/zabbix(.+\.php)(.*)$;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/zabbix$fastcgi_script_name;
        include        fastcgi_params;
    }

パーミッションを変更

# chown nginx:nginx /etc/zabbix/web
# chown nginx:nginx /etc/zabbix/web/zabbix.conf.php
6. Nginx再起動とZabbix Server起動
# systemctl reload nginx
# systemctl start zabbix-server
7. 自動起動設定
# systemctl disable httpd
# systemctl enable nginx
# systemctl enable php-fpm
8. http://www.hogehoge.com/ で起動する nginx conf

こちらで運用する場合もあるので記述

# vi /etc/nginx/conf.d/zabbix.conf
    location / {
        root   /usr/share/zabbix;
        index  index.php;
    }

    location ~ \.php$ {
        root           /usr/share/zabbix;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

こっちのほうがお手軽なので、これでもいいかなあと