0

I'm aware this has been asked a few others times, but none of those solutions seem to be working for me. This was my first time deploying a Django project, so for reference, I followed this tutorial: https://linuxhint.com/create_django_app_ubuntu/

For that reason, it is entirely possible that something flew over my head completely.

nginx config (I've tried alias instead of root, with and without trailing slash, made sure the static file aren't owned by root, etc.):

upstream django {
    server 127.0.0.1:8000;
}

server {
    listen 80;

    location /static/ {
        root /home/bryan/Projects/portfolio;
        try_files $uri =404;
    }

    location / {
        try_files $uri @send_to_django;
    }

    location @send_to_django {
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://django;
    }
}

settings.py:

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

Note:

  • django.contrib.staticfiles is installed
  • python manage.py collectstatic appears to be working
  • I do believe I am referencing the static files correctly in my templates:
{% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'home/css/index.css' %}">

My file structure:

  • portfolio (django project root)
    • home (django app)
    • static (project static files)
      • home (static files for home app)
        • css

Other than the static files, the site works flawlessly with this setup.

Bryan
  • 43
  • 8

1 Answers1

1
  1. Make sure your static files are in /home/bryan/Projects/portfolio/static
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )

Settings above may cause static files being collected into /home/bryan/Projects/portfolio/static/static dir

  1. Check static dir permissions

  2. Use "alias" instead of "root" in your nginx conf

Alexey Popov
  • 726
  • 4
  • 8
  • Thanks for the reply. 1. It doesn't seem like that is the case. All the files exist as intended under `/home/bryan/Projects/portfolio/static`. For example, even the one I reference in my HTML snippet can be found in the file system under `home/bryan/Projects/portfolio/static/home/css/index.css` 2. I just checked and the perms for the static dir are given to my user and not to root. 3. I am trying this now: `location /static/ { alias /home/bryan/Projects/portfolio/static/; try_files $uri =404; }` But, it still just gives me a 404 – Bryan Sep 25 '20 at 19:11
  • @Bryan location /static/ { alias /home/bryan/Projects/portfolio/static/; remove trailing slashes – Alexey Popov Sep 25 '20 at 19:15
  • I've now removed them and it still gives the 404, unfortunately – Bryan Sep 25 '20 at 19:26
  • @Bryan what does nginx log say? – Alexey Popov Sep 25 '20 at 19:28
  • Oh, it seems to be throwing a 403 for static files. But, why is this happening only with static files? The actual HTML template itself reports 200 and works just fine; everything in the /portfolio/ directory has the exact same permissions. – Bryan Sep 25 '20 at 19:52
  • @Bryan static files dir (and files as well) should be readable for nginx user or group (www-data in ubuntu/debian), so you can add www-data to your home user group ("bryan" I suppose), set explicit read permission for all like 777, or store your static files in a folder like /var/www which already has necessary permissions for nginx user (in this case you will need to add your user to www-data group and add read-write permissions for this group) – Alexey Popov Sep 25 '20 at 20:02
  • So I managed to fix the static files issue by moving the entire project to a separate user entirely. This user also runs nginx and gunicorn. Now, I've been stuck on an entirely different problem. The website works if I manually execute gunicorn, but if I try to setup a systemd service to start gunicorn, it doesn't work (gives me status=127 and nothing else). – Bryan Sep 25 '20 at 22:54
  • @Bryan check this answer https://stackoverflow.com/questions/1763156/127-return-code-from – Alexey Popov Sep 25 '20 at 23:20
  • Okay so I solved everything by reinstalling the venv. I think I never installed it properly in the first place (gunicorn wasn't in the `/env/bin/` for some reason). Thanks again for the replies! – Bryan Sep 26 '20 at 01:29