LoginSignup
3
2

More than 5 years have passed since last update.

Fedora21 に nginx 導入

Posted at

Fedora21 Workstationにyumからnginxを導入する

yumからインストール


# yum install nginx

※2015/2/7時点でインストールされたパッケージは
nginx.x86_64 1:1.6.2-4.fc21

インストール後の設定

インストール直後は、サービスは停止状態

[root@fedora ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: inactive (dead)
[root@fedora ~]# 

設定ファイルは/etc/nginx/nginx.conf

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

とのことなので、/etc/nginx/conf.d 配下にconfファイルを作成する

/etc/nginx/server.conf
server {
    listen       81;  #←nginx.confに書かれた設定が80のため、別ポートを指定
    server_name  localhost;

    location / {
        root   /home/aznprpr/eclipsework;  #←ルートディレクトリの指定
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ /\.ht {
        deny  all;
    }
}

作成後、正しい記述になっているか確認

[root@fedora conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@fedora conf.d]# 

サービスの起動…が失敗

サービスを起動しようとしたら失敗した


root@fedora conf.d]# systemctl start nginx
Job for nginx.service failed. See "systemctl status nginx.service" and "journalctl -xe" for details.

メッセージ通り、詳細を確認する


[root@fedora conf.d]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: failed (Result: exit-code) since 土 2015-02-07 21:30:13 JST; 16s ago
  Process: 5086 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE)

 2月 07 21:30:13 fedora.local nginx[5086]: nginx: the configuration file /e...k
 2月 07 21:30:13 fedora.local systemd[1]: nginx.service: control process ex...1
 2月 07 21:30:13 fedora.local systemd[1]: Failed to start The nginx HTTP an....
 2月 07 21:30:13 fedora.local systemd[1]: Unit nginx.service entered failed....
 2月 07 21:30:13 fedora.local systemd[1]: nginx.service failed.
 2月 07 21:30:13 fedora.local nginx[5086]: nginx: [emerg] open() "/run/ngin...)
 2月 07 21:30:13 fedora.local nginx[5086]: nginx: configuration file /etc/n...d
Hint: Some lines were ellipsized, use -l to show in full.
[root@fedora conf.d]# 
[root@fedora conf.d]# 
[root@fedora conf.d]# journalctl -xe
-- 
-- The result is failed.
 2月 07 21:30:13 fedora.local systemd[1]: Unit nginx.service entered failed state
 2月 07 21:30:13 fedora.local systemd[1]: nginx.service failed.
 2月 07 21:30:13 fedora.local nginx[5086]: nginx: [emerg] open() "/run/nginx.pid"
 2月 07 21:30:13 fedora.local nginx[5086]: nginx: configuration file /etc/nginx/n
 2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:30:13 fedora.local nginx[5086]: nginx: the configuration file /etc/ngi2月 07 21:30:13 fedora.local dbus[744]: [system] Activating service name='org.fe2月 07 21:30:13 fedora.local systemd[1]: nginx.service: control process exited, 2月 07 21:30:13 fedora.local systemd[1]: Failed to start The nginx HTTP and reve- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has failed.
 2月 07 21:30:14 fedora.local dbus[744]: [system] Successfully activated service 
 2月 07 21:30:14 fedora.local setroubleshoot[5092]: SELinux is preventing nginx  2月 07 21:30:13 fedora.local systemd[1]: nginx.service failed.
 2月 07 21:30:13 fedora.local nginx[5086]: nginx: [emerg] open() "/run/nginx.pid"
 2月 07 21:30:13 fedora.local nginx[5086]: nginx: configuration file /etc/nginx/n
 2月 07 21:30:14 fedora.local dbus[744]: [system] Successfully activated service 
 2月 07 21:30:14 fedora.local setroubleshoot[5092]: SELinux is preventing nginx f
 2月 07 21:30:14 fedora.local python[5092]: SELinux is preventing nginx from 'rea

                                             *****  Plugin catchall (100. confid

                                             If you believe that nginx should be
                                             Then you should report this as a bu
                                             You can generate a local policy mod
                                             Do
                                             allow this access for now by execut
                                             # grep nginx /var/log/audit/audit.l
                                             # semodule -i mypol.pp

 2月 07 21:31:35 fedora.local google-chrome.desktop[2524]: Fontconfig error: Cann
 2月 07 21:31:35 fedora.local google-chrome.desktop[2524]: [2524:2563:0207/213135
 2月 07 21:31:51 fedora.local google-chrome.desktop[2524]: [2524:2563:0207/213151
[root@fedora conf.d]# 

なんとなーくSELinuxが怪しい気がする
起動した時にポップアップ出たし…

SELinux通知ブラウザーを起動してみると
トラブルシューティングとして2つほどコマンドがあったので打ってみる

root@fedora conf.d]# grep nginx /var/log/audit/audit.log | audit2allow -M mypol
******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i mypol.pp

[root@fedora conf.d]# 
[root@fedora conf.d]# semodule -i mypol.pp
[root@fedora conf.d]# 

サービスの起動

再度起動してみる

[root@fedora conf.d]# systemctl start nginx
[root@fedora conf.d]# 
[root@fedora conf.d]# 
[root@fedora conf.d]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: active (running) since 土 2015-02-07 21:54:20 JST; 17s ago
  Process: 8475 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 8472 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
 Main PID: 8478 (nginx)
   CGroup: /system.slice/nginx.service
           ├─8478 nginx: master process /usr/sbin/nginx
           └─8479 nginx: worker process

 2月 07 21:54:20 fedora.local nginx[8472]: nginx: the configuration file /e...k
 2月 07 21:54:20 fedora.local nginx[8472]: nginx: configuration file /etc/n...l
Hint: Some lines were ellipsized, use -l to show in full.
[root@fedora conf.d]# 

無事起動できた

ハマった

ブラウザからlocalhost:81でアクセスすると、403 Forbiddenに
今回は、ユーザディレクトリ配下のEclipseワークスペースを
ドキュメントルートに設定しているため、
ディレクトリごと755に変更していおいた。

が、ログを見るとPermissionDenyっぽい…

[root@fedora conf.d]# cat /var/log/nginx/error.log 
(略)
2015/02/07 22:07:10 [error] 8479#0: *9 open() "/home/aznprpr/eclipsework/adminpage/about.html" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /adminpage/about.html HTTP/1.1", host: "localhost:81"
2015/02/07 22:07:11 [error] 8479#0: *9 open() "/home/aznprpr/eclipsework/favicon.ico" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:81"
[root@fedora conf.d]# 

faviconはないので良いとして、なぜ…

ググってみると、ユーザディレクトリそのものの権限も必要だという情報が
確かに盲点だった…

ユーザディレクトリそのものの権限も755に設定し、
再度アクセスするも、状況は変わらず
(開発機でセキュリティ無視してるのでこんな設定ができます)

またしてもお前か

SELinux通知センターを確認すると、再度nginxの通知が…
そこにあらたなコマンドが書いてあったため実行した


[root@fedora conf.d]# setsebool -P httpd_read_user_content 1

結果、ページの表示を確認

長かった…

やっぱりSELinux無効化したほうが開発は楽に進められる気がする

参考ページ

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2