通过Django上载用户图像时出现权限错误。服务器以root用户身份运行,目录有775个权限

2024-09-28 22:02:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在管理界面中创建新记录时,尝试上载图像时出现以下错误。

Permission denied: '/home/jeffy/django_files/django_test/static/uploaded_files/1407616465_2016587_61DjIaZQI8L.jpg'

相关settings.py变量:

STATIC_ROOT = "/home/jeffy/django_files/django_test/static/"
MEDIA_ROOT=STATIC_ROOT

的输出

^{pr2}$

drwxrwxr-x 2 jeffy jeffy 4096 Aug  9 16:32 uploaded_files

服务器以用户jeffy的身份启动:

jeffy@originaldjangster:/etc/nginx/sites-available$ sudo service nginx start
jeffy@originaldjangster:/etc/nginx/sites-available$ sudo /home/jeffy/django_files/django_test_venv/bin/gunicorn -c /home/jeffy/django_files/django_test_venv/gunicorn_config.py django_test.wsgi
root

我把这段代码放在settings.py的末尾

import getpass
print(getpass.getuser())

这就是为什么“root”会在gunicorn服务器启动后打印出来。


为什么这个文件不能上传?


Tags: djangopytest服务器homesettingsnginxstatic
1条回答
网友
1楼 · 发布于 2024-09-28 22:02:54

这是古尼科恩的原版_配置.py文件:

command = '/home/jeffy/django_files/django_test_venv/bin/gunicorn'
pythonpath = '/home/jeffy/django_files/django_test'
bind = '127.0.0.1:8001'
workers = 1
user = 'nobody'

nobody更改为jeffy可以使其正常工作(尽管它在静态和媒体目录相等的情况下犹豫不决…我在static旁边创建了一个media文件夹)。在

这个设置只是为了让我炫耀我的Django血统。如果这是一个生产环境,我认为以root用户身份运行服务器是个坏主意。在

不知道这个设置到底有多“糟糕”,但它至少到目前为止是有效的。。。在

相关问题 更多 >