I have Ubuntu 20.04 server with Django applications, Nginx and Gunicorn. I was wondering what is the best and most secure practice for directory permissions for my website and apps? I have researched and read 755 for directories and 644 for files. Would this be the best practice for all directories and files for my Django apps and Nginx?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi @mpasquali,
Usually, yes folders generally do tend to have 755 and files 644 permissions. Those are the default standards. Having said that some files and folders have different perms, it really does depend on the case. Usually, config files might be with perms 400 and some folders might have 600 permissions. Having said that, thread carefully as some permissions break the functionality of the App.
When setting up permissions for a Django application with Nginx and Gunicorn, it’s crucial to strike a balance between usability and security. The general permissions you’ve mentioned (755 for directories and 644 for files) are a common starting point, but let’s get into more detail.
Here’s a breakdown:
User & Group:
www-data
ordjango
.www-data
user on most systems, but this can vary.Django Project Directory & Files:
drwxr-xr-x
). This allows the owner to write to the directory while others can only read/traverse it.-rw-r--r--
). This allows the owner to write to the file, while others can only read it.manage.py
: Should be given execute permission (i.e., 755) if you want to run it directly without usingpython manage.py ...
.SECRET_KEY
& Sensitive Information:Static & Media Directories:
static/
: This directory will be read by Nginx to serve static files directly. It should be readable by thewww-data
user or whatever user Nginx runs as.media/
: If users upload files, this directory should be writable by the Gunicorn/Django user.Database Files (if using SQLite):
-rw-r-----
). This is to ensure that the web server (like Nginx) cannot write to the database.Virtual Environment & Dependencies:
Nginx Configuration Files:
nginx.conf
and others in/etc/nginx/
): Should generally be owned by root and not writable by other users. Permissions like 644 are common.Logs:
Socket Files:
Other Tips:
These are general guidelines, and the specific needs can vary based on the application, plugins, and additional services you might be using. Always refer to the documentation of each component and consider the principle of least privilege when setting permissions.