django 错误记录:[Errno 13] 权限被拒绝
django error logging : [Errno 13] Permission denied
我正在尝试为我的站点启用错误日志记录。所以我在项目的根目录中创建了一个名为 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
但还是不行,我收到了这个错误
mod_wsgi 可能将当前目录修改为 /(文件系统的根目录)
您的应用尝试写入 /log.txt,而不是您创建的应用。
修改 LOGGINGs 以便 'filename' 是完整文件路径,而不是相对文件路径
'filename': '/home/xyz/public_html/projectname/log.txt',
还要检查运行 apache 的系统用户是否有写权限log.txt
使用 mod_wsgi 时,您不应使用单独的日志文件。而是直接记录到 stdout
或 stderr
并将其捕获到 Apache 错误日志文件中。这样 Apache 就会担心权限和日志文件轮换等问题。请参阅 'Logging of Python exceptions' 部分中的示例:
如果在一个 Apache 上托管多个站点,只需确保每个 VirtualHost
都有自己的错误日志文件,这样不同的站点就会分开。
我不知道这是否对任何人有帮助,但我遇到了与这个问题非常相似的问题。我正在 AWS Elastic Beanstalk 上部署,当 SSH 以 root 用户和 运行 python3 manage.py shell
身份进入实例时,没有问题.
当我以 ec2_user 和 运行 python3 manage.py shell
身份通过 SSH 进入一个实例时,我请参阅以下错误消息:
(staging) [ec2-user@ip-172-31-9-242 current]$ python3 manage.py shell
Traceback (most recent call last):
File "/usr/lib64/python3.7/logging/config.py", line 563, in configure
handler = self.configure_handler(handlers[name])
File "/usr/lib64/python3.7/logging/config.py", line 736, in configure_handler
result = factory(**kwargs)
File "/usr/lib64/python3.7/logging/__init__.py", line 1087, in __init__
StreamHandler.__init__(self, self._open())
File "/usr/lib64/python3.7/logging/__init__.py", line 1116, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: '/var/app/current/.logs/django.log'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
django.setup()
File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/utils/log.py", line 75, in configure_logging
logging_config_func(logging_settings)
File "/usr/lib64/python3.7/logging/config.py", line 800, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib64/python3.7/logging/config.py", line 571, in configure
'%r' % name) from e
ValueError: Unable to configure handler 'file'
当我检查文件权限时...
(staging) [root@ip-172-31-9-242 current]# ls ./.logs -lash
total 16K
0 drwxr-xr-x 2 webapp webapp 24 Oct 8 00:29 .
4.0K drwxr-xr-x 18 webapp webapp 4.0K Oct 8 00:31 ..
12K -rw-r--r-- 1 webapp webapp 12K Oct 8 00:40 django.log
它显示我的 django.log 文件具有 rw-r--r--
(644) 的权限。另请注意文件的所有者名为 webapp
.
因此,我把我的ec2_user加入了webapp
组(这里不解释),然后更改了日志文件的文件权限:
chmod 664 /var/app/current/.logs/django.log
我再次检查了文件权限(注意rw-rw-r--
):
(staging) [ec2-user@ip-172-31-9-242 current]$ ls -lash .logs
total 16K
0 drwxr-xr-x 2 webapp webapp 24 Oct 8 17:27 .
4.0K drwxr-xr-x 18 webapp webapp 4.0K Oct 8 17:29 ..
12K -rw-rw-r-- 1 webapp webapp 11K Oct 8 17:29 django.log
那我可以运行python3 manage.py shell
(staging) [ec2-user@ip-172-31-9-242 current]$ python3 manage.py shell
Python 3.7.10 (default, Jun 3 2021, 00:02:01)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
所以通过提升日志文件的权限解决了问题。要理解的关键是我的 ec2_user 是 webapp
组的一部分,然后权限模式 664 允许读取和写入权限。 “写入”权限是必需的权限。
参见 chmod calculator。
我正在尝试为我的站点启用错误日志记录。所以我在项目的根目录中创建了一个名为 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
但还是不行,我收到了这个错误
mod_wsgi 可能将当前目录修改为 /(文件系统的根目录)
您的应用尝试写入 /log.txt,而不是您创建的应用。
修改 LOGGINGs 以便 'filename' 是完整文件路径,而不是相对文件路径
'filename': '/home/xyz/public_html/projectname/log.txt',
还要检查运行 apache 的系统用户是否有写权限log.txt
使用 mod_wsgi 时,您不应使用单独的日志文件。而是直接记录到 stdout
或 stderr
并将其捕获到 Apache 错误日志文件中。这样 Apache 就会担心权限和日志文件轮换等问题。请参阅 'Logging of Python exceptions' 部分中的示例:
如果在一个 Apache 上托管多个站点,只需确保每个 VirtualHost
都有自己的错误日志文件,这样不同的站点就会分开。
我不知道这是否对任何人有帮助,但我遇到了与这个问题非常相似的问题。我正在 AWS Elastic Beanstalk 上部署,当 SSH 以 root 用户和 运行 python3 manage.py shell
身份进入实例时,没有问题.
当我以 ec2_user 和 运行 python3 manage.py shell
身份通过 SSH 进入一个实例时,我请参阅以下错误消息:
(staging) [ec2-user@ip-172-31-9-242 current]$ python3 manage.py shell
Traceback (most recent call last):
File "/usr/lib64/python3.7/logging/config.py", line 563, in configure
handler = self.configure_handler(handlers[name])
File "/usr/lib64/python3.7/logging/config.py", line 736, in configure_handler
result = factory(**kwargs)
File "/usr/lib64/python3.7/logging/__init__.py", line 1087, in __init__
StreamHandler.__init__(self, self._open())
File "/usr/lib64/python3.7/logging/__init__.py", line 1116, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: '/var/app/current/.logs/django.log'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
django.setup()
File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/utils/log.py", line 75, in configure_logging
logging_config_func(logging_settings)
File "/usr/lib64/python3.7/logging/config.py", line 800, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib64/python3.7/logging/config.py", line 571, in configure
'%r' % name) from e
ValueError: Unable to configure handler 'file'
当我检查文件权限时...
(staging) [root@ip-172-31-9-242 current]# ls ./.logs -lash
total 16K
0 drwxr-xr-x 2 webapp webapp 24 Oct 8 00:29 .
4.0K drwxr-xr-x 18 webapp webapp 4.0K Oct 8 00:31 ..
12K -rw-r--r-- 1 webapp webapp 12K Oct 8 00:40 django.log
它显示我的 django.log 文件具有 rw-r--r--
(644) 的权限。另请注意文件的所有者名为 webapp
.
因此,我把我的ec2_user加入了webapp
组(这里不解释),然后更改了日志文件的文件权限:
chmod 664 /var/app/current/.logs/django.log
我再次检查了文件权限(注意rw-rw-r--
):
(staging) [ec2-user@ip-172-31-9-242 current]$ ls -lash .logs
total 16K
0 drwxr-xr-x 2 webapp webapp 24 Oct 8 17:27 .
4.0K drwxr-xr-x 18 webapp webapp 4.0K Oct 8 17:29 ..
12K -rw-rw-r-- 1 webapp webapp 11K Oct 8 17:29 django.log
那我可以运行python3 manage.py shell
(staging) [ec2-user@ip-172-31-9-242 current]$ python3 manage.py shell
Python 3.7.10 (default, Jun 3 2021, 00:02:01)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
所以通过提升日志文件的权限解决了问题。要理解的关键是我的 ec2_user 是 webapp
组的一部分,然后权限模式 664 允许读取和写入权限。 “写入”权限是必需的权限。
参见 chmod calculator。