我试图为我的网站启用错误日志记录。因此,我在项目的根目录下创建了一个名为log.txt的文件。
/home/xyz/public_html/projectname/log.txt
在我的www.example.com上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',
},
}
}
这在我的localhost上运行得很好,但是当我把它放到网上时,当我试图启动服务器时,我得到:
[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
但仍然没有运气,我得到这个错误
3条答案
按热度按时间ibrsph3r1#
mod_wsgi可能会将当前目录修改为/(文件系统的根目录)
您的应用尝试写入/log. txt,而不是您创建的文件。
“文件名”:“/home/xyz/公共项目名称/日志. txt”,
n53p2ov02#
当使用mod_wsgi时,你不应该使用单独的日志文件。相反,直接记录到
stdout
或stderr
,并将其捕获到Apache错误日志文件中。这样一来,Apache会担心权限和日志文件循环等问题。请参见“Python异常的日志记录”一节中的示例:如果在一个Apache上托管多个站点,只需确保每个
VirtualHost
都有自己的错误日志文件,这样不同的站点就可以分开了。h6my8fg23#
我不知道这是否会帮助任何人,但我有一个非常类似的问题,这个问题。我正在AWS弹性Beanstalk上部署,当SSH到一个示例作为***root***用户和运行
python3 manage.py shell
,没有问题。当我以***ec2_user***的身份通过SSH进入示例并运行
python3 manage.py shell
时,看到以下错误消息:当我检查文件权限时...
它显示我的django.log文件拥有
rw-r--r--
(644)的权限。还要注意文件的所有者名为webapp
。因此,我将我的***ec2_user***添加到
webapp
组(这里不做解释),然后更改日志文件的文件权限:我再次检查了文件权限(注意
rw-rw-r--
):然后我可以运行
python3 manage.py shell
因此,通过提升日志文件的权限解决了问题。需要了解的关键是,我的 ec2_user 是
webapp
组的一部分,权限模式664允许读取和写入权限。“写入”权限是所需的权限。请参阅chmod calculator。