I am about to publish my first Django project on a Digital Ocean droplet. I was following the official Digital Ocean guide to install Django on a droplet (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04#creating-systemd-socket-and-service-files-for-gunicorn) and everything worked fine but the serving of static files.
Here are key parts of my files:
settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
DEBUG = False
COMPRESS_ROOT = os.path.join(BASE_DIR,"static")
COMPRESS_ENABLED = True
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
]
urls.py
urlpatterns = [
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Here is my folder structure on digital ocean:
root/
snap/
gymtracker/
gymenv/
gymtracker/
gymtrackerapp/
static/
I see on in the terminal on digital ocean that all static files are in this location:
/root/gymtracker/gymtracker/static/
but when I load the page I get 403 for the static files ( the page loads fine otherwise):
Request Method: GET
Status Code: 403 Forbidden
Thanks so much for your help!
2
Answers
Found the solution.
The Digital Ocean tutorial (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04#creating-systemd-socket-and-service-files-for-gunicorn) missed one important Nginx config change at the end to make your static resources show up.
After you follow the tutorial you need to edit /etc/nginx/nginx.conf and change the user from www-data to your username (e.g sammy) by running
The other solution works, while there are few alternatives. I think the safest way is to move the /static/ folder out of /home/{user}/ directory
Creating the directories and setting permissions:
Now changing the NGINX config:
replace the "
location /static/
" part withWill need to change the DJANGO settings as well, replace the "
STATIC_URL =
" lines with:Now run collectstatic and it should work! Based on https://realpython.com/django-nginx-gunicorn/#serving-static-files-directly-with-nginx