82
I have solved this myself in the end.
When running on the development machines, I am in fact running using my current userβs privileges. However, when running on the deployment server, I am in fact running through wsgi
, which means itβs running using www-data
βs privileges.
www-data
is neither the owner nor in the group of users that own /var/www
. This means that www-data
is treated as other
and has the permissions set to others.
The BAD solution to this would be to do:
sudo chmod -R 777 /var/www/
This would give everyone full access to everything in /var/www/
, which is a very bad idea.
Another BAD solution would be to do:
sudo chown -R www-data /var/www/
This would change the owner to www-data
, which opens security vulnerabilities.
The GOOD solution would be:
sudo groupadd varwwwusers
sudo adduser www-data varwwwusers
sudo chgrp -R varwwwusers /var/www/
sudo chmod -R 770 /var/www/
This adds www-data
to the varwwwusers
group, which is then set as the group for /var/www/
and all of its subfolders. You could set it to 750
to make it more secure but then you wonβt be able to use Django's
collectstatic
functionality so stick to 770
unless youβre very confident about what youβre doing.
13
Create a βMEDIAβ directory in the root of your project.
Then set:
MEDIA_ROOT = os.path.join(BASE_DIR,'MEDIA')
- [Django]-How to reload modules in django shell?
- [Django]-Update all models at once in Django
- [Django]-Django CSRF check failing with an Ajax POST request
11
To know which user you are logged on to:
$ whoami
ubuntu
And adding to your solution, if you are using an AWS Instance, you should add your user to the group to be able to access that folder:
Making a group for webservices users (varwwwusers)
$ sudo groupadd varwwwusers
Change the www folder and make it belong to varwwwusers
$ sudo chgrp -R varwwwusers /var/www/
www-data is the server making django requests, add that to the group
$ sudo adduser www-data varwwwusers
Change folder policy
$ sudo chmod -R 770 /var/www/
Add ubuntu to the group of varwwwusers
$ usermod -a -G varwwwusers ubuntu
Hope this helps!
- [Django]-Django β makemigrations β No changes detected
- [Django]-Good ways to sort a queryset? β Django
- [Django]-Django rest-auth allauth registration with email, first and last name, and without username
1
The resolution for this problem when dealing with the production server would be to make use of collectstatic as already mentioned that it used and resolved or give permissions to the folders. However if your solution is in a local environment, the solution can be acquired by configuring the local MEDIA
directory in the settings.py
file that acts on the local server.
So, would be adding these two lines in the local configuration file as @Nic Scozzaro mentioned:
MEDIA_ROOT = os.path.join (BASE_DIR, 'media')
STATIC_ROOT = os.path.join (BASE_DIR, 'static')
After the configuration restart the services to apply the fixes.
- [Django]-CSRF with Django, React+Redux using Axios
- [Django]-Having trouble with user.is_authenticated in django template
- [Django]-Django App Improperly Configured β The app module has multiple filesystem locations
1
instead of :
MEDIA_ROOT = os.path.join(BASE_DIR, '\media\')
add the following:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
- [Django]-How do you convert a PIL `Image` to a Django `File`?
- [Django]-How can I access environment variables directly in a Django template?
- [Django]-Django ChoiceField
0
Unlike Others it was the answer for me (ubantu with gunicorn nginx)
instead of : MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
add the following: MEDIA_ROOT = os.path.join(BASE_DIR, '/media/')
- [Django]-Unable to find a locale path to store translations for file __init__.py
- [Django]-Getting a count of objects in a queryset in Django
- [Django]-NameError: name '_mysql' is not defined after setting change to mysql