0

i'm running django mod_wsgi with apache2 on GCP VM instance and when i run it on DEBUG=False the static files work fine but media files is geting 404 when i check them while clearly the files are present on the server . my conf is based on the following django doc https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/modwsgi/#serving-files

#settings.py
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "media")
MEDIA_URL = os.environ.get("MEDIA_URL", "/media/")

STATIC_ROOT = os.path.join(PROJECT_ROOT, "static")
STATIC_URL = os.environ.get("STATIC_URL", "/static/")

#apache2
Alias /media/ /home/ubuntu/myshop/media/
Alias /static/ /home/ubuntu/myshop/static/

<Directory /home/ubuntu/myshop/media>
Require all granted
</Directory>

<Directory /home/ubuntu/myshop/static>
Require all granted
</Directory>

tried restarting apache2 and even the server but didnt work either

LeLouch
  • 601
  • 8
  • 21

2 Answers2

1

fixed it was an https problem i needed to add the config to both virtualhost file port 80 and 443

<VirtualHost *:80>
...
Alias /media/ /home/ubuntu/myshop/media/
Alias /static/ /home/ubuntu/myshop/static/

<Directory /home/ubuntu/myshop/media>
Require all granted
</Directory>

<Directory /home/ubuntu/myshop/static>
Require all granted
</Directory>
...
</VirtualHost>
<VirtualHost *:443>
...
Alias /media/ /home/ubuntu/myshop/media/
Alias /static/ /home/ubuntu/myshop/static/

<Directory /home/ubuntu/myshop/media>
Require all granted
</Directory>

<Directory /home/ubuntu/myshop/static>
Require all granted
</Directory>
...
</VirtualHost>
LeLouch
  • 601
  • 8
  • 21
0

Looks like your alias is wrong...

#apache2
Alias /media/ /home/ubuntu/myshop/media/
Alias /static/ /home/ubuntu/myshop/static/

<Directory /home/ubuntu/myshop/media>
Require all granted
</Directory>

the alias has a backslash and your directory statement does not.

Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
  • 1
    removing the backslash from the alias or adding one on the directory file doesnt solve the problem so i kept it similar to the docs – LeLouch Jan 15 '20 at 17:09
  • may be a permission issue then. What happens when you navigate with debug turned on, what is the error message? – Chris Hawkes Jan 15 '20 at 17:16
  • 1
    works fine with debug = True when i remove + static("/media/", document_root=settings.MEDIA_ROOT) i get 404 not found in the console – LeLouch Jan 15 '20 at 17:22
  • 1
    the permision looks like that drwxrwxrwx 5 www-data www-data 4096 Dec 21 13:18 media – LeLouch Jan 15 '20 at 17:30