Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

Zabbix4.2のフロントエンドをApache(httpd)からnginxに変更する

こんにちは、LinuCエバンジェリストこと、鯨井貴博@opensourcetechです。

igore sysoevさんと一緒Alexei Vladishevさんと一緒 

 

今回は、Zabbix4.2のフロントエンドとして使われているApache(httpd)をnginxに変更してみます。

 

 

 0.準備

なお、①の記事のZabbix環境をベースにしている、②の記事で使ったnginx/php-fpm環境がインストール・設定済みという前提で進めます。

PostgreSQL11を使ってZabbix 4.2を構築する - Opensourcetechブログ

nginx 1.15.12 でPHP(php-fpm)を実行する - Opensourcetechブログ

 

 

1.Apache(httpd)の停止

既存のフロントエンドであるApache(httpd)を停止します。

※停止しておかないと、nginxを立ち上げた際「80/tcp」が使われているとエラーになり立ち上がらないので注意。

[root@localhost html]# systemctl stop httpd

 

 

2.nginxの設定

 /etc/nginx/conf.d/default.confを編集します。

編集箇所は、

4行目のドキュメントルート、33〜40行目にかけてのPHP関連の設定です。

[root@localhost ~]# vi /etc/nginx/conf.d/default.conf
[root@localhost ~]# cat -n /etc/nginx/conf.d/default.conf
1 server {
2 listen 80;
3 server_name localhost;
4 root /usr/share/nginx/html/zabbix/zabbix;
5 #root /usr/share/nginx/html;
6
7 #charset koi8-r;
8 #access_log /var/log/nginx/host.access.log main;
9
10 #location / {
11 # root /usr/share/nginx/html;
12 # root /usr/share/nginx/html/zabbix;
13 # index index.html index.htm;
14 #}
15
16 #error_page 404 /404.html;
17
18 # redirect server error pages to the static page /50x.html
19 #
20 error_page 500 502 503 504 /50x.html;
21 location = /50x.html {
22 root /usr/share/nginx/html;
23 }
24
25 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
26 #
27 #location ~ \.php$ {
28 # proxy_pass http://127.0.0.1;
29 #}
30
31 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
32 #
33 location ~ \.php$ {
34 #root html;
35 fastcgi_pass 127.0.0.1:9000;
36 fastcgi_index index.php;
37 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
38 fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
39 include fastcgi_params;
40 }
41
42 # deny access to .htaccess files, if Apache's document root
43 # concurs with nginx's one
44 #
45 #location ~ /\.ht {
46 # deny all;
47 #}
48 }
49

 

 

3.ドキュメントルートの設定 

ドキュメントルートとして指定した「/usr/share/nginx/html/zabbix/zabbix」を用意します。

 

まず、/usr/share/nginx/html/の直下に「zabbixディレクトリ」を作成します。

[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# mkdir zabbix
[root@localhost html]# ls -l
合計 8
-rw-r--r--. 1 root root 494 4月 17 00:14 50x.html
-rw-r--r--. 1 root root 612 4月 17 00:14 index.html
drwxr-xr-x. 2 root root 6 5月 6 17:04 zabbix <---これ

 

 続いて、zabbixのフロントエンドとなるPHPファイルが置かれている「/usr/share/zabbix」のシンボリックリンクとして「/usr/share/nginx/html/zabbix/zabbix」を作成します。

[root@localhost zabbix]# ls /usr/share/zabbix/
actionconf.php chart4.php httpdetails.php report4.php
adm.gui.php chart5.php image.php robots.txt
adm.housekeeper.php chart6.php images screen.import.php
adm.iconmapping.php chart7.php img screenconf.php
adm.images.php charts.php imgstore.php screenedit.php
adm.macros.php conf include screens.php
adm.other.php conf.import.php index.php services.php
adm.regexps.php correlation.php index_http.php setup.php
adm.triggerdisplayoptions.php disc_prototypes.php items.php slideconf.php
adm.triggerseverities.php discoveryconf.php js slides.php
adm.valuemapping.php favicon.ico jsLoader.php srv_status.php
adm.workingtime.php fonts jsrpc.php styles
api_jsonrpc.php graphs.php latest.php sysmap.php
app history.php local sysmaps.php
applications.php host_discovery.php locale templates.php
audio host_prototypes.php maintenance.php toptriggers.php
auditacts.php host_screen.php map.import.php tr_events.php
auditlogs.php hostgroups.php map.php trigger_prototypes.php
browserwarning.php hostinventories.php overview.php triggers.php
chart.php hostinventoriesoverview.php profile.php usergrps.php
chart2.php hosts.php queue.php users.php
chart3.php httpconf.php report2.php zabbix.php
[root@localhost zabbix]# pwd
/usr/share/nginx/html/zabbix
[root@localhost html]# ln -s /usr/share/zabbix /usr/share/nginx/html/zabbix/
[root@localhost html]# ls -l zabbix/
合計 0
lrwxrwxrwx. 1 root root 17 5月 6 17:05 zabbix -> /usr/share/zabbix

 

 

4.パーミッションの編集

Apache(httpd)をフロントエンドとしたZabbix環境では、

使用するPHPファイルを格納するディレクトリの所有者や所有グループが「apache」となっており、nginxには権限が与えられていません。 

[root@localhost ]# ls -l /etc/zabbix/
合計 32
drwxr-x---. 2 apache apache 56 5月 6 16:07 web <----ここ
-rw-r--r--. 1 root root 10735 4月 18 18:32 zabbix_agentd.conf
drwxr-xr-x. 2 root root 38 5月 6 13:20 zabbix_agentd.d
-rw-r-----. 1 root zabbix 17384 5月 6 14:50 zabbix_server.conf
[root@localhost ~]# cd /var/lib/php/classes
[root@localhost php]# ls -l
合計 0
drwxrwx---. 2 root apache 45 5月 6 15:09 session <----ここ

 

 なので、それぞれ「apache」から「nginx」へ変更します。

[root@localhost ~]# chown nginx.nginx /etc/zabbix/web
[root@localhost ~]# ls -l /etc/zabbix/
合計 32
drwxr-x---. 2 nginx nginx 56 5月 6 16:07 web <----ここ
-rw-r--r--. 1 root root 10735 4月 18 18:32 zabbix_agentd.conf
drwxr-xr-x. 2 root root 38 5月 9 13:20 zabbix_agentd.d
-rw-r-----. 1 root zabbix 17384 5月 9 14:50 zabbix_server.conf
[root@localhost ~]# cd /var/lib/php/classes
[root@localhost php]# chgrp nginx session/
[root@localhost php]# ls -l
合計 0
drwxrwx---. 2 root nginx 84 5月 6 16:08 session <----ここ

 

 

5.nginxの起動

nginxを起動します。

※必要に応じて、自動起動設定(systemctl enable nginx)を行なってください。

[root@localhost html]# systemctl start nginx
[root@localhost html]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since 月 2019-05-06 17:35:01 JST; 5s ago
Docs: http://nginx.org/en/docs/
Process: 31721 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 31722 (nginx)
CGroup: /system.slice/nginx.service
├─31722 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─31723 nginx: worker process

5月 06 17:35:01 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
5月 06 17:35:01 localhost.localdomain systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
5月 06 17:35:01 localhost.localdomain systemd[1]: Started nginx - high performance web server.

 

 

6.ブラウザからのアクセス

ブラウザから「http://nginxIP/index.php」へアクセスすると、

ログインしダッシュボードが表示されます。

zabbixのログイン画面

zabbixのダッシュボード

 

 

ハマりどころ・トラブルシュート

・PHP設定関連

設定が間違っていないか確認

間違っている場合、「403 forbidden」などとなる可能性があります。

 

・nginxのドキュメントルート

配置が間違っていないか確認

間違っている場合、「404 not found」などとなる可能性があります。

 

・PHPディレクトリのパーミッション

apacheからnginxへ変更したか確認

※/var/lib/php/classes/sessionの権限がapacheのままの場合、以下のようになります。

PHPのパーミッションエラー画面

 

 

 

 

 

 

www.slideshare.net

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

 

Opensourcetech by Takahiro Kujirai