1
votes

I've deployed my web on Digital Ocean VPS - Ubuntu 14.04. I have a ImageField in one of my models which I choose in Django admin (browse pc and upload the image). The problem is that this image can't be saved into /media/categories/. It's raising:

[Errno 13] Permission denied: '/home/django/project/media/categories/television_d3q3EM0.png'

I thought that it is, because Nginx hasn't write privileges to /media/ directory so I run these commands:

sudo chown -R www-data /media/

which didn't help

sudo chown -R django /media/

which didn't help neither

I'm a newbie in Linux rights so I can't figure out what am I doing wrong.

Media folder is in /project/ directory (where the manage.py is).

Here are permissions of the /media/ folder.

Owner has rights so I don't know why sudo chown -R www-data /media/ didn't work.

enter image description here

GUNICORN OWNER

 ps aux | grep gunicorn
django   16636  0.3  2.7  66040 13616 ?        Ss   08:16   0:00 /usr/bin/python /usr/bin/gunicorn --name=project --pythonpath=project --bind=127.0.0.1:9000 --config /etc/gunicorn.d/gunicorn.py project.wsgi:application
django   16642  1.1  7.8 174800 39164 ?        S    08:16   0:00 /usr/bin/python /usr/bin/gunicorn --name=project --pythonpath=project --bind=127.0.0.1:9000 --config /etc/gunicorn.d/gunicorn.py project.wsgi:application
django   16643  1.9 10.5 214244 52808 ?        S    08:16   0:01 /usr/bin/python /usr/bin/gunicorn --name=project --pythonpath=project --bind=127.0.0.1:9000 --config /etc/gunicorn.d/gunicorn.py project.wsgi:application
django   16644  2.3 10.4 209232 52224 ?        S    08:16   0:01 /usr/bin/python /usr/bin/gunicorn --name=project --pythonpath=project --bind=127.0.0.1:9000 --config /etc/gunicorn.d/gunicorn.py project.wsgi:application
root     16667  0.0  0.1  11744   932 pts/0    S+   08:17   0:00 grep --color=auto gunicorn
2
Your folder permissions should be 755 not 311.themanatuf
@themanatuf It didn't help. I tried to change it.Milano
maybe sudo chown -R yourusername:www-data /home/django/project/mediaAaron

2 Answers

2
votes

Assuming you are using gunicorn, make sure the user which is running gunicorn have permissions to write to the specified path. Check out gunicorn deployment docs to see how to control which user is running gunicorn.

2
votes

The problem was with chown command.

sudo chown -R django /media/

worked because there is /media/ directory - it is an absolute path to the media folder at the bottom of the tree.

I should instead do:

sudo chown -R django media/ which is a relative path (I was in project directory).

The django user is handling gunicorn so django has to be media/ owner (or has to have appropriate rights).