django错误记录:[Errno 13]权限被拒绝

时间:2016-11-22 01:23:02

标签: python django centos mod-wsgi centos7

我正在尝试为我的网站启用错误记录。所以我在项目的根目录中创建了一个名为log.txt的文件。

/home/xyz/public_html/projectname/log.txt

和我在此地址中的setting.py:

/home/xyz/public_html/projectname/projectname/settings.py

我有一个简单的日志记录设置:

LOGGINGs = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'log.txt',
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {
            'handlers':['file'],
            'propagate': True,
            'level':'DEBUG',
        },

    }
}

这在我的本地主机上工作正常但是当我尝试启动服务器时我把它上线了:

[Tue Nov 22 04:27:37.817043 2016] [wsgi:error] [pid 3619] [remote 111.113.21.35:0] ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '/log.txt'

这是错误堆栈:

(XID d4tgu5) Database Connect Error: Access denied for user 'leechprotect'@'localhost' (using password: YES)
[Tue Nov 22 04:36:25.934018 2016] [wsgi:info] [pid 4882] mod_wsgi (pid=4882): Create interpreter 'myproject.com|'.
[Tue Nov 22 04:36:25.960785 2016] [wsgi:info] [pid 4882] mod_wsgi (pid=4882): Adding '/home/xyz/public_html/myproject' to path.
[Tue Nov 22 04:36:25.961397 2016] [wsgi:info] [pid 4882] mod_wsgi (pid=4882): Adding '/usr/local/lib/python3.4/site-packages/' to path.
[Tue Nov 22 04:36:25.963133 2016] [wsgi:info] [pid 4882]  mod_wsgi (pid=4882, process='myproject', application='myproject.com|'): Loading WSGI script '/home/xyz/public_html/myproject/myproject/wsgi.py'.
[Tue Nov 22 04:36:26.016998 2016] [wsgi:info] [pid 4925] mod_wsgi (pid=4925): Initializing Python.
[Tue Nov 22 04:36:26.049383 2016] [wsgi:info] [pid 4925] mod_wsgi (pid=4925): Attach interpreter ''.
[Tue Nov 22 04:36:26.386166 2016] [wsgi:error] [pid 4882]  mod_wsgi (pid=4882): Target WSGI script '/home/xyz/public_html/myproject/myproject/wsgi.py' cannot be loaded as Pytho
n module.
[Tue Nov 22 04:36:26.386312 2016] [wsgi:error] [pid 4882]  mod_wsgi (pid=4882): Exception occurred processing WSGI script '/home/xyz/public_html/myproject/myproject/wsgi.py'.
[Tue Nov 22 04:36:26.386413 2016] [wsgi:error] [pid 4882]  Traceback (most recent call last):
[Tue Nov 22 04:36:26.387088 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/config.py", line 557, in configure
[Tue Nov 22 04:36:26.387135 2016] [wsgi:error] [pid 4882]      handler = self.configure_handler(handlers[name])
[Tue Nov 22 04:36:26.387239 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/config.py", line 725, in configure_handler
[Tue Nov 22 04:36:26.387263 2016] [wsgi:error] [pid 4882]      result = factory(**kwargs)
[Tue Nov 22 04:36:26.388223 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/__init__.py", line 1006, in __init__
[Tue Nov 22 04:36:26.388268 2016] [wsgi:error] [pid 4882]      StreamHandler.__init__(self, self._open())
[Tue Nov 22 04:36:26.388337 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/__init__.py", line 1035, in _open
[Tue Nov 22 04:36:26.388360 2016] [wsgi:error] [pid 4882]      return open(self.baseFilename, self.mode, encoding=self.encoding)
[Tue Nov 22 04:36:26.388418 2016] [wsgi:error] [pid 4882]  PermissionError: [Errno 13] Permission denied: '/log.txt'
[Tue Nov 22 04:36:26.388464 2016] [wsgi:error] [pid 4882] 
[Tue Nov 22 04:36:26.388483 2016] [wsgi:error] [pid 4882]  During handling of the above exception, another exception occurred:
[Tue Nov 22 04:36:26.388491 2016] [wsgi:error] [pid 4882] 
[Tue Nov 22 04:36:26.388509 2016] [wsgi:error] [pid 4882]  Traceback (most recent call last):
[Tue Nov 22 04:36:26.388781 2016] [wsgi:error] [pid 4882]    File "/home/xyz/public_html/myproject/myproject/wsgi.py", line 16, in <module>
[Tue Nov 22 04:36:26.388819 2016] [wsgi:error] [pid 4882]      application = get_wsgi_application()
[Tue Nov 22 04:36:26.389032 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Tue Nov 22 04:36:26.389064 2016] [wsgi:error] [pid 4882]      django.setup()
[Tue Nov 22 04:36:26.389245 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/site-packages/django/__init__.py", line 17, in setup
[Tue Nov 22 04:36:26.389273 2016] [wsgi:error] [pid 4882]      configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Nov 22 04:36:26.389521 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/site-packages/django/utils/log.py", line 71, in configure_logging
[Tue Nov 22 04:36:26.389590 2016] [wsgi:error] [pid 4882]      logging_config_func(logging_settings)
[Tue Nov 22 04:36:26.389657 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/config.py", line 789, in dictConfig
[Tue Nov 22 04:36:26.389680 2016] [wsgi:error] [pid 4882]      dictConfigClass(config).configure()
[Tue Nov 22 04:36:26.389726 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/config.py", line 565, in configure
[Tue Nov 22 04:36:26.389745 2016] [wsgi:error] [pid 4882]      '%r: %s' % (name, e))
[Tue Nov 22 04:36:26.389784 2016] [wsgi:error] [pid 4882]  ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '/log.txt'
[Tue Nov 22 04:36:26.390725 2016] [wsgi:info] [pid 4882]  mod_wsgi (pid=4882, process='myproject', application='myproject.com|'): Loading WSGI script '/home/xyz/public_html/neg
inpay/myproject/wsgi.py'.
[Tue Nov 22 04:36:26.392489 2016] [wsgi:error] [pid 4882]  mod_wsgi (pid=4882): Target WSGI script '/home/xyz/public_html/myproject/myproject/wsgi.py' cannot be loaded as Pytho
n module.
[Tue Nov 22 04:36:26.392630 2016] [wsgi:error] [pid 4882]  mod_wsgi (pid=4882): Exception occurred processing WSGI script '/home/xyz/public_html/myproject/myproject/wsgi.py'.
[Tue Nov 22 04:36:26.392724 2016] [wsgi:error] [pid 4882]  Traceback (most recent call last):
[Tue Nov 22 04:36:26.392821 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/config.py", line 557, in configure
[Tue Nov 22 04:36:26.392835 2016] [wsgi:error] [pid 4882]      handler = self.configure_handler(handlers[name])
[Tue Nov 22 04:36:26.392882 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/config.py", line 725, in configure_handler
[Tue Nov 22 04:36:26.392892 2016] [wsgi:error] [pid 4882]      result = factory(**kwargs)
[Tue Nov 22 04:36:26.392931 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/__init__.py", line 1006, in __init__
[Tue Nov 22 04:36:26.392941 2016] [wsgi:error] [pid 4882]      StreamHandler.__init__(self, self._open())
[Tue Nov 22 04:36:26.392976 2016] [wsgi:error] [pid 4882]    File "/usr/local/lib/python3.4/lib/python3.4/logging/__init__.py", line 1035, in _open
[Tue Nov 22 04:36:26.392985 2016] [wsgi:error] [pid 4882]      return open(self.baseFilename, self.mode, encoding=self.encoding)
[Tue Nov 22 04:36:26.393021 2016] [wsgi:error] [pid 4882]  PermissionError: [Errno 13] Permission denied: '/log.txt'

所以我认为这是日志文件权限所以我暂时将其设置为777,直到找到更好的解决方案。现在我有:

ls -l /home/xyz/public_html/myproject/log.txt
-rwxrwxrwx. 1 xyz xyz 0 Nov 22 03:19 /home/xyz/public_html/myproject/log.txt

它没有用,然后我搜索了一下,发现有些因为SELinux而有这个问题。所以我现在已经关闭了这个:

[root@static]# sestatus
SELinux status:                 disabled

但仍然没有运气,我收到了这个错误

2 个答案:

答案 0 :(得分:6)

mod_wsgi可能会将当前目录修改为/(文件系统的根目录)

您的应用尝试写入/log.txt,而不是写入您创建的应用。

  • 修改LOGGING,使'filename'为完整文件路径,而不是相对文件路径

    'filename':'/ home /xyz / public_html / projectname / log.txt',

  • 同时检查运行apache的系统用户是否具有该log.txt的写权限

答案 1 :(得分:2)

使用mod_wsgi时,您不应该使用单独的日志文件。而是直接记录到stdoutstderr并将其记录在Apache错误日志文件中。这样Apache就会担心权限和日志文件的轮换等。请参阅“Python异常日志记录”一节中的示例。的:

如果在一个Apache上托管多个站点,只需确保每个VirtualHost它自己的错误日志文件,以便分离不同的站点。