django error logging : [Errno 13] Permission denied

11,327

Solution 1

mod_wsgi probably modifies the current directory to / (root of filesystem)

Your app tries to write to /log.txt, not into the one you created.

  • Modify the LOGGINGs so that 'filename' is a full filepath, not a relative one

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

  • Also check that that the system user than runs apache has write rights on that log.txt

Solution 2

When using mod_wsgi you shouldn't use a separate log file. Instead direct logging to stdout or stderr and have it be captured in the Apache error log file. This way Apache worries about permissions and log file rotation etc. See example in section 'Logging of Python exceptions' of:

If hosting multiple sites on one Apache, just make sure each VirtualHost its up its own error log file so different sites are separated.

Share:
11,327
hretic
Author by

hretic

Updated on June 17, 2022

Comments

  • hretic
    hretic almost 2 years

    I'm trying to enable error logging for my site. So I've created a file called log.txt in the root of my project.

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

    and in my setting.py in this address:

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

    I have a simple logging setting:

    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',
            },
    
        }
    }
    

    This works fine in my localhostbut when I put it online when I'm trying to start the server I get:

    [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'
    

    here is the error stack:

    (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'
    

    So I thought it's the log file permission so I temporarily set it to 777 until I find a better solution. Now I have:

    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
    

    It didn't work then I've searched around and found some have this problem because of SELinux. SO I'VE TURNED THAT OFF AS WELL NOW I HAVE:

    [root@static]# sestatus
    SELinux status:                 disabled
    

    But still no luck and I'm getting this error