How to Deploy Django Static Files in Nginx

1 min read
How to Deploy Django Static Files in Nginx

This article shows how to deploy Django static files in Linux. If you want to deploy in DigitalOcean, create your account by using my referral link below. You can get free $100 credit and you can help this site. Follow my previous article here on how you can setup your DigitalOcean droplet.

DigitalOcean Referral Badge

settings.py

In your project_name/settings.py, specify the your app static file directories in STATICFILES_DIRS list.

In STATIC_ROOT variable, specify the target directory for your static files. You can choose any directory that is not linked to a user. It should be visible to

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "<app_name>/static"),
]

STATIC_URL = "/static/"
STATIC_ROOT = "/var/www/<project_name>/static/"

Collect static files

Django does not automatically copy all your static files to STATIC_ROOT. You can do this manually by running:

python manage.py collectstatic

Nginx configuration

In your nginx configuration add a location section.

server {
    ...

    location /static/ {
        root /var/www/<project_name>/;
    }
   
    ...
}

Read more articles like this in the future by buying me a coffee!

Buy me a coffeeBuy me a coffee