skip to Main Content

When I run, from the command line,

python manage.py runserver mywebsite.com:8099

the css works fine. But when I use the systemd service with Gunicorn socket, the app works but without the css. Even if I use the following command: gunicorn demo.wsgi:application, the app works fine but without the css.

Excerpt from my settings.py:

STATIC_URL = "static/"

STATICFILES_DIRS = [
    BASE_DIR / "mystaticfiles"
]

Excerpt from my nginx config file:

    location /static/ {
        root /root/djdeploy/demo/static;
    }

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

My Django project working directory is: /root/djdeploy/demo/,

I have put my css files in the following folder: /root/djdeploy/demo/mystaticfiles

2

Answers


  1. Chosen as BEST ANSWER
    1. There was a hidden problem of permission. I put the static folder in /var/www/ instead of the root folder. Then I changed ownership to allow nginx to get access to it:
    chown -R root:www-data /var/www/static/
    
    1. in nginx configuration the word static shouldn't be repeated. That is to do this:
        location /static/ {
            root /var/www/;
        }
    
    

    instead of this:

        location /static/ {
            root /var/www/static;
        }
    
    
    1. I changed the STATIC_ROOT VARIABLE in setting.py as follows:
    STATIC_ROOT = "/var/www/static"
    
    Then I restarted nginx and gunicorn service and the problem was solved.
    

  2. The CSS works when you do python manage.py runserver because it knows where to find static files in your code. On the other hand, gunicorn doesn’t know where to look for them.

    You need to set STATIC_ROOT and run python manage.py collectstatic on your server. You already have nginx set up to serve the files once you collect them to the location that nginx will look for them.

    See the official documentation for more details about deploying. Also see the section on configuring static files.

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