LoginSignup
13
13

More than 5 years have passed since last update.

Djangoをホームディレクトリに配置した時、静的ファイルがpermissionエラーになってハマった

Posted at

タイトルの通り
AWSで/home/ec2-user/配下にDjangoアプリケーションを配置した時の話
WEBサーバはNginxを使用

Djangoアプリケーションを公開する時の静的ファイルの扱いについて

Djangoアプリケーションを公開する時、静的ファイルは

$ python manage.py collectstatic

でどこかに一つにまとめる必要がある

settings.pyはこんな感じ

settings.py
STATIC_URL = '/static/'

STATICFILES_DIRS=(
    os.path.join(BASE_DIR, "app/static/"),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

こうしておくと諸々のcssやjsやらは/home/ec2-user/django_project_root/staticに集められてhttp://domain/static以下でアクセスできるようになる

nginx.confは以下のようにし、静的ファイルにアクセスできるようにしておく

nginx.conf
server {
    ...
    location /static/ {
            autoindex   on;
            alias  /home/ec2-user/django_project_root/static/;
    }

しかしパーミッションエラー

これでいけるっしょ、ということでアプリケーションの様子を見てみたら、cssが適用されていない(403 forbidden)
/var/log/nginx/error.logを見てみると

2017/08/29 02:17:27 [error] 11629#0: *22 open() "/home/ec2-user/django_project_root/static/apps/css/base.css" failed (13: Permission denied), client: xx.xxx.xxx.xxx, server: xx.xxx.xxx.xxx, request: "GET /static/apps/css/base.css HTTP/1.1", host: "xx.xxx.xxx.xxx", referrer: "http://xx.xxx.xxx.xxx/"

このようにパーミッションエラーが出てた

いろいろググってみるとSElinuxが悪さしてんじゃね?みたいな記事があったが

$ getenforce
Disabled

無効化されてた

静的ファイルのパーミッションを弄ったり、ディレクトリ/home/ec2-user/django_project_root/static/のパーミッションを弄ってみたりしたが、状態変わらず

/home/ec2-user/に実行権限を与える必要があった

初期状態としては700(drwx------)の状態なのだけれど、otherに実行権限を与える必要があった

$ sudo chmod o+x /home/ec2-user/

これで

xx.xxx.xxx.xxx - - [29/Aug/2017:02:54:10 +0000] "GET /static/apps/css/login.css HTTP/1.1" 200 1203 "http://xx.xxx.xxx.xxx" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36" "-"

はい
無事、静的ファイルにアクセスできるようになりました

子のディレクトリへの実行権限があっても、親のディレクトリへの実行権限が無いと「そのディレクトリ自体への移動」というのができない、って初歩的な話でした...

13
13
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
13
13