centos django错误日志:[Errno 13]权限被拒绝

ufj5ltwl  于 2022-11-08  发布在  Go
关注(0)|答案(3)|浏览(206)

我试图为我的网站启用错误日志记录。因此,我在项目的根目录下创建了一个名为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

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

ibrsph3r

ibrsph3r1#

mod_wsgi可能会将当前目录修改为/(文件系统的根目录)
您的应用尝试写入/log. txt,而不是您创建的文件。

  • 修改LOGGING,使“filename”是完整的文件路径,而不是相对路径

“文件名”:“/home/xyz/公共项目名称/日志. txt”,

  • 还要检查运行apache的系统用户是否对该log.txt具有写入权限
n53p2ov0

n53p2ov02#

当使用mod_wsgi时,你不应该使用单独的日志文件。相反,直接记录到stdoutstderr,并将其捕获到Apache错误日志文件中。这样一来,Apache会担心权限和日志文件循环等问题。请参见“Python异常的日志记录”一节中的示例:

如果在一个Apache上托管多个站点,只需确保每个VirtualHost都有自己的错误日志文件,这样不同的站点就可以分开了。

h6my8fg2

h6my8fg23#

我不知道这是否会帮助任何人,但我有一个非常类似的问题,这个问题。我正在AWS弹性Beanstalk上部署,当SSH到一个示例作为***root***用户和运行python3 manage.py shell,没有问题。
当我以***ec2_user***的身份通过SSH进入示例并运行python3 manage.py shell时,看到以下错误消息:

(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_userwebapp组的一部分,权限模式664允许读取和写入权限。“写入”权限是所需的权限。
请参阅chmod calculator

相关问题