【问题标题】:Django/mod_wsgi OSError: [Errno 13] Permission denied: 'static' when DEBUG = OFFDjango/mod_wsgi OSError: [Errno 13] Permission denied: 'static' when DEBUG = OFF
【发布时间】:2012-07-08 16:33:04
【问题描述】:

我在 Centos 6.2 服务器(运行 apache、mysql、php)上有一个 Django 1.4 应用程序,使用 mod_wsgi 并将我的项目部署在虚拟环境中。该应用程序本身是我多年来一直在托管服务上使用的应用程序,现在我正在自己的机架空间云服务器上进行部署。该应用程序的确切版本在其他地方运行良好,所以这个问题是关于我如何部署它的。这是我的第一次 python/django 部署——我之前已经建立了几十个 LAMP 站点,所以我对 Django 部署的不熟悉显然阻碍了我。

该应用程序在我的服务器上运行良好,在项目的 settings.py 文件中使用 DEBUG = TRUE,但是当我将其更改为 FALSE 时,网站的前端会产生 [500] 内部服务器错误。

我知道在 DEBUG 设置为 OFF 的情况下,apache 现在通过 mod_wsgi 提供我的静态文件(mod_wsgi 工作正常),这让我相信我的配置中的“某些东西”正在阻止这种情况。我已经运行了./manage.py collectstatic 命令,该命令填充了/myproject 文件夹中的/static 目录。

我已经为此工作了数周,阅读了尽可能多的部署指南,但到目前为止还没有任何乐趣。非常感谢您的帮助。

以下是我项目的 settings.py 文件中的相关声明:

############ settings.py #############

SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = '/opt/virtual/myproject/static/localtv/media/'
MEDIA_URL = 'http://example.org/static/localtv/media/'
STATIC_ROOT = '/opt/virtual/myproject/static/'
STATIC_URL = 'http://example.org/static/'

这里是 wsgi.py 文件:

############# wsgi.py #################
import os
import sys
import site
site.addsitedir('/opt/virtual/myapp/lib/python2.7/site-packages')

apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)

sys.path.append('/opt/virtual')
sys.path.append('/opt/virtual/myproject')

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

我在 httpd.conf 中的虚拟主机声明如下所示:

############ virtual host declaration in httpd.conf ##############
<VirtualHost *:80>
ServerName example.org
ServerAlias www.example.org
DocumentRoot /opt/virtual/myproject

Alias /robots.txt /opt/virtual/myproject/static/robots.txt
Alias /favicon.ico /opt/virtual/myproject/static/favicon.ico

AliasMatch ^/([^/]*\.css) /opt/virtual/myproject/static/styles/$1

Alias /static/ /opt/virtual/myproject/static/
Alias /media/ /opt/virtual/myproject/static/media
Alias /images /opt/virtual/myproject/static/images

<Directory /opt/virtual/myproject/static>
Order deny,allow
Allow from all
</Directory>

<Directory /opt/virtual/myproject/static/media>
Order deny,allow
Allow from all
</Directory>

WSGIDaemonProcess example.org python-path=/opt/virtual/myapp/lib/python2.7/site-packages
WSGIProcessGroup example.org

WSGIScriptAlias / /opt/virtual/myproject/application/wsgi.py

<Directory /opt/virtual/myproject>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>

我在 /ROOT 中的 .bashrc 文件如下所示:

########### .bashrc ##################

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific aliases and functions
alias python='/opt/bin/python2.7'

export PYTHONPATH=/opt/virtual/myapp/lib/python2.7/site-packages:$PYTHONPATH

最后,我的 error_log 显示了 Traceback:

    ############### error_log ###############
    [Mon Jul 09 09:21:13 2012] [error] <WSGIRequest
    [Mon Jul 09 09:21:13 2012] [error] path:/,
    [Mon Jul 09 09:21:13 2012] [error] GET:<QueryDict: {}>,
    [Mon Jul 09 09:21:13 2012] [error] POST:<QueryDict: {}>,
    [Mon Jul 09 09:21:13 2012] [error]  'DOCUMENT_ROOT': '/opt/virtual/myproject',
    [Mon Jul 09 09:21:13 2012] [error]  'GATEWAY_INTERFACE': 'CGI/1.1',
    [Mon Jul 09 09:21:13 2012] [error]  'HTTP_ACCEPT': "*/*",
    [Mon Jul 09 09:21:13 2012] [error]  'HTTP_HOST': 'example.org',
    [Mon Jul 09 09:21:13 2012] [error]  'HTTP_USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
    [Mon Jul 09 09:21:13 2012] [error]  'PATH_INFO': u'/',
    [Mon Jul 09 09:21:13 2012] [error]  'PATH_TRANSLATED': '/opt/virtual/myproject/application/wsgi.py/',
    [Mon Jul 09 09:21:13 2012] [error]  'QUERY_STRING': '',
    [Mon Jul 09 09:21:13 2012] [error]  'REMOTE_ADDR': '99.99.99.99',
    [Mon Jul 09 09:21:13 2012] [error]  'REMOTE_PORT': '99999',
    [Mon Jul 09 09:21:13 2012] [error]  'REQUEST_METHOD': 'GET',
    [Mon Jul 09 09:21:13 2012] [error]  'REQUEST_URI': '/',
    [Mon Jul 09 09:21:13 2012] [error]  'SCRIPT_FILENAME': '/opt/virtual/myproject/application/wsgi.py',
    [Mon Jul 09 09:21:13 2012] [error]  'SCRIPT_NAME': u'',
    [Mon Jul 09 09:21:13 2012] [error]  'SERVER_ADDR': '111.111.111.111',
    [Mon Jul 09 09:21:13 2012] [error]  'SERVER_ADMIN': 'root@localhost',
    [Mon Jul 09 09:21:13 2012] [error]  'SERVER_NAME': 'example.org',
    [Mon Jul 09 09:21:13 2012] [error]  'SERVER_PORT': '80',
    [Mon Jul 09 09:21:13 2012] [error]  'SERVER_PROTOCOL': 'HTTP/1.0',
    [Mon Jul 09 09:21:13 2012] [error]  'SERVER_SIGNATURE': '<address>Apache/2.2.15 (CentOS) Server at example.org Port 80</address>\\n',
    [Mon Jul 09 09:21:13 2012] [error]  'SERVER_SOFTWARE': 'Apache/2.2.15 (CentOS)',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.application_group': 'example.org|',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.callable_object': 'application',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.handler_script': '',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.input_chunked': '0',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.listener_host': '',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.listener_port': '80',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.process_group': 'example.org',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.request_handler': 'wsgi-script',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.script_reloading': '1',
    [Mon Jul 09 09:21:13 2012] [error]  'mod_wsgi.version': (3, 3),
    [Mon Jul 09 09:21:13 2012] [error]  'wsgi.errors': <mod_wsgi.Log object at 0x7f34321aa530>,
    [Mon Jul 09 09:21:13 2012] [error]  'wsgi.file_wrapper': <built-in method file_wrapper of mod_wsgi.Adapter object at 0x7f34320e4e40>,
    [Mon Jul 09 09:21:13 2012] [error]  'wsgi.input': <mod_wsgi.Input object at 0x7f34320e02b0>,
    [Mon Jul 09 09:21:13 2012] [error]  'wsgi.multiprocess': False,
    [Mon Jul 09 09:21:13 2012] [error]  'wsgi.multithread': True,
    [Mon Jul 09 09:21:13 2012] [error]  'wsgi.run_once': False,
    [Mon Jul 09 09:21:13 2012] [error]  'wsgi.url_scheme': 'http',
    [Mon Jul 09 09:21:13 2012] [error]  'wsgi.version': (1, 1)}>
    [Mon Jul 09 09:21:13 2012] [error] -------------------------------------------------------------------------------
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] mod_wsgi (pid=21520): Exception occurred processing WSGI script '/opt/virtual/myproject/application/wsgi.py'.
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] Traceback (most recent call last):
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     response = self.get_response(request)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/handlers/base.py", line 179, in get_response
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return callback(request, **param_dict)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     response = view_func(request, *args, **kwargs)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/views/defaults.py", line 33, in server_error
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return http.HttpResponseServerError(t.render(Context({})))
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return self._render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return self.nodelist.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     bit = self.render_node(node, context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return node.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return compiled_parent._render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return self.nodelist.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     bit = self.render_node(node, context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return node.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     result = block.nodelist.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     bit = self.render_node(node, context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     response = view_func(request, *args, **kwargs)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/views/defaults.py", line 33, in server_error
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return http.HttpResponseServerError(t.render(Context({})))
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return self._render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return self.nodelist.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     bit = self.render_node(node, context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return node.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return compiled_parent._render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return self.nodelist.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     bit = self.render_node(node, context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return node.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     result = block.nodelist.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     bit = self.render_node(node, context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return node.render(context)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/templatetags/compress.py", line 91, in render
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     rendered_output = compressor.output(self.mode, forced=forced)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/css.py", line 53, in output
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     ret.append(subnode.output(*args, **kwargs))
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/css.py", line 55, in output
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return super(CssCompressor, self).output(*args, **kwargs)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/base.py", line 221, in output
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     finished_content = self.handle_output(mode, filtered_content, forced)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/base.py", line 233, in handle_output
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     return output_func(mode, content, forced)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/compressor/base.py", line 245, in output_file
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     self.storage.save(new_filepath, ContentFile(content))
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/files/storage.py", line 45, in save
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     name = self._save(name, content)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/site-packages/django/core/files/storage.py", line 168, in _save
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     os.makedirs(directory)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]   File "/opt/virtual/myapp/lib/python2.7/os.py", line 157, in makedirs
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99]     mkdir(name, mode)
    [Mon Jul 09 09:21:13 2012] [error] [client 99.99.99.99] OSError: [Errno 13] Permission denied: '/opt/virtual/myproject/static/CACHE/css'

【问题讨论】:

  • 粗鲁的方式:[ chmod -R 777 /opt/virtual/myproject/static/ ]
  • 你需要让运行Django的用户可以写静态目录。
  • 您的目录应该可以被运行 Apache 的用户访问/写入,最有可能是 www-data
  • 有同样的问题,我不知道为什么会这样。在我的情况下,使用django_compressor,它可以在 Windows 本地运行,但不能在我的生产 Ubuntu 服务器上运行。

标签: python django permissions mod-wsgi django-staticfiles


【解决方案1】:

在几乎相似的情况下(我运行的是 Ubuntu,而不是 CentOS),我一直收到与您相同的错误。

正如您所注意到的,当使用 DEBUG = FALSE 运行时,您实际上是在通过 wsgi 运行。这意味着当使用 DEBUG = TRUE 运行时,您实际上是在使用您的用户权限,而在使用 DEBUG = FALSE 运行时,您正在使用 Apache 的用户权限运行。 Apache 使用的用户是www-data

www-data 既不是所有者,也不是拥有/var/www 的用户组。这意味着www-data 被视为other 并具有设置为其他人的权限。

BAD 解决方案是:

sudo chmod -R 777 /var/www/

这将使每个人都可以完全访问/var/www/ 中的所有内容,这显然是一个非常糟糕的主意。

另一个不好的解决方案是:

sudo chown -R www-data /var/www/

这会将所有者更改为www-data,从而打开安全漏洞。

GOOD 解决方案是:

sudo groupadd varwwwusers
sudo adduser www-data varwwwusers
sudo chgrp -R varwwwusers /var/www/
sudo chmod -R 770 /var/www/

这会将www-data 添加到varwwwusers 组,然后将其设置为/var/www/ 及其所有子文件夹的组。 chmod 将授予所有者和组读取、写入、执行权限,同时阻止任何其他用户访问它。

【讨论】:

  • GOOD解决方案中不应该是sudo usermod -a -G varwwwusers www-data而不是sudo adduser www-data varwwwusers吗?
  • @maciek 你能详细说明一下吗?或提供另一个答案,详细说明为什么不同?
  • @chrisFrisina 当用户存在时,我们不能添加它,所以我们不应该使用adduser。我们应该使用sudo usermod -a -G varwwwusers www-data 而只是将现有用户www-data 添加到组varwwwusers
猜你喜欢
  • 2020-06-21
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 2016-08-07
  • 2017-07-15
  • 2015-11-26
  • 2015-11-29
相关资源
最近更新 更多