Adam. R

How to Deploy Django Static Files in Nginx!

Created November 2, 2021

This article shows how to deploy Django static files in Linux. If you want to deploy in Google Cloud, create your account by using my referral link here. You can get free $350 credit and also help me!


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>/;
        autoindex off;
    }
   
    ...
}

If you found this usful then please share this and follow me! Also check out my website where I also post everything from here