skip to Main Content

I’m trying to deploy my django site to ubuntu server nginx by following this tutorial (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu).

Deployment works, but css not working. I figured it’s something with collectstatic but not sure. Funny enough css worked properly for 0.0.0.0:8000 port.

This is from settings.py

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

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

I noticed in tutorial that they don’t have STATICFILES_DIRS… and… STATIC_URL and STATIC_ROOT are same 'static'. Could this be an issue…?

also, tried editing path to static with sudo nano /etc/nginx/sites-available/django_project

server {
    listen 80;
    server_name xxx.xx.xx.xxx; #my ip, just hide it for purpose of question

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        alias /home/muser/django_project/static/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Any idea what I did wrong? Thanks in advance!

2

Answers


  1. you use wrong setting.
    As far as I know, static dirs are only used in debug mode.
    When not in debug mode, static root must be used.
    And the static root you set is different from the static root of nginx.

    and don’t forget python manage.py collectstatic.

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

    If you know how to read Korean, this link may be helpful.

    https://django.seolpyo.com/entry/31/

    https://django.seolpyo.com/entry/29/

    Login or Signup to reply.
  2. i saw the problem in you nginx configuration on static location block there you have provided the path for the static folder

    location /static/ {
            alias /home/muser/django_project/static/;
    }
    

    in the location you give path to static folder which you should change the path to staticfiles

    location /static/ {
            alias /home/muser/django_project/staticfiles/;
    }
    

    because you had given the name for the static_root folder is staticfiles

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

    and don’t forget to run this command to create a staticfiles folder.

    python manage.py collectstatic
    

    I hope this will solve your problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search