Search code examples
flaskapache2mod-wsgiwsgipermission-denied

How can I fix "PermissionError: [Errno 13] Permission denied: 'static'"?


I'm running an apache2 server with flask and mod_wsgi.

In my flask script I have a function that creates new directories:

def updateDir():
    dbData = News.query.all()
    for row in dbData:
        rowLink = createLink(row.id, row.title)
        finalPath = "static/img/posts/{}".format(rowLink)
        os.makedirs(finalPath, exist_ok=True)

When I try to run the script it shows this traceback:

ERROR in app: Exception on /refresh [GET]
 Traceback (most recent call last):
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
     response = self.full_dispatch_request()
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
     rv = self.handle_user_exception(e)
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
     reraise(exc_type, exc_value, tb)
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
     raise value
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
     rv = self.dispatch_request()
   File "/var/www/abc/abc/venv3/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
     return self.view_functions[rule.endpoint](**req.view_args)
   File "/var/www/abc/abc/main.py", line 133, in refresh
     updateDir()
   File "/var/www/abc/abc/main.py", line 77, in updateDir
     os.makedirs(finalPath, exist_ok=True)
   File "/usr/local/lib/python3.6/os.py", line 210, in makedirs
     makedirs(head, mode, exist_ok)
   File "/usr/local/lib/python3.6/os.py", line 210, in makedirs
     makedirs(head, mode, exist_ok)
   File "/usr/local/lib/python3.6/os.py", line 210, in makedirs
     makedirs(head, mode, exist_ok)
   File "/usr/local/lib/python3.6/os.py", line 220, in makedirs
     mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: 'static'

I've already given www-data permissions to write to the 'static' folder and it still shows the error.

Feel free to ask questions if I haven't covered everything in order to help me.


Solution

  • Don't use a relative path name. The current working directory of the process will not usually be where your code is. You need to calculate an absolute path relative to some anchor point. See: