禁止访问django apache2媒体文件

时间:2017-02-02 09:59:27

标签: python django apache media

我无法从django管理站点访问我的文件。我保存文件没有任何问题,但当我尝试打开它时,我收到错误:

Forbidden
You don't have permission to access /media/file.pdf on this server.

在django项目设置中:

STATIC_URL = '/static/'

STATIC_ROOT = '/full/path/to/static/'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

在项目urls.py中:

if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

在我的虚拟主机中,我添加了以下设置:

 Alias /static/ "/static/folder/"
 Alias /media/ "/meida/folder/" 
 <Directory "/static/folder">
    Require all granted
 </Directory>
 <Directory "/media/folder">
    Require all granted
 </Directory>

但仍然会收到此错误。哪里可能是错误/错误? 编辑1:apache错误日志给出:

client denied by server configuration: /etc/apache2/home

1 个答案:

答案 0 :(得分:-1)

更改/etc/apache2/conf-available/php5-fpm.conf。替换每个

Order Deny,Allow
Deny from all

Require all granted

配置必须由a2enconf php5-fpm启用。

修改

按照基本配置:https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/

编辑2

进行更改并将其添加到虚拟主机设置中:

<Directory /usr/local/django_apps/myapp/static>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

请注意,路径来自根!