Python – Permission issues when writing files on web servers (flask, apache, and wsgi).

Permission issues when writing files on web servers (flask, apache, and wsgi)…. here is a solution to the problem.

Permission issues when writing files on web servers (flask, apache, and wsgi).

I’m trying to deploy my first web app, but I don’t know anything about it. It’s funny, but I feel like I don’t know what I’m doing when I’m trying to solve the problem.

I

created a server on Digital Ocean running on Ubuntu 18.04 and for the last 2 days I’ve been trying to get my website running smoothly.

I

feel like it’s almost over now, but I’m stuck with permission access issues.

My app runs through Flask. I’m using wsgi_mod and apache to run it.

I’ve put down 2 settings files that can help you (I think?). ) and the error log.

I also tried applying the following fix:

sudo chown -R www-data:www-data/var/www/website.com

sudo cmod -R 775/var/www/website.com

I tried running wsgi using the WSGIDaemon process (it didn’t work whatever it was used for).

Please help me!

Let me know if you need more questions.

Thanks in advance!

Error log:

Traceback (most recent call last):
File "/var/www/website.com/wsgi/website.com.wsgi", line 10, in <module>
    from __init__ import app as application
File "/var/www/website.com/akb/__init__.py", line 19, in <module>
    flask_session. Session(app)
File "/var/www/website.com/akb/env3.7/lib/python3.6/site-packages/flask_session/__init__.py", line 5$
    self.init_app(app)
File "/var/www/website.com/akb/env3.7/lib/python3.6/site-packages/flask_session/__init__.py", line 6$
    app.session_interface = self._get_interface(app)
File "/var/www/website.com/akb/env3.7/lib/python3.6/site-packages/flask_session/__init__.py", line 9$
    config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
File "/var/www/website.com/akb/env3.7/lib/python3.6/site-packages/flask_session/sessions.py", line 3$
    self.cache = FileSystemCache(cache_dir, threshold=threshold, mode=mode)
File "/var/www/website.com/akb/env3.7/lib/python3.6/site-packages/werkzeug/contrib/cache.py", line 7$
    os.makedirs(self._path)
File "/usr/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/flask_session'

/var/www/website/wsgi/website.com.wsgi

#!/var/www/website.com/akb/env3.7/bin/python
# -*- coding: utf-8 -*-

import sys
import logging

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/website.com/akb")

from __init__ import app as application
application.secret_key = 'iuhAqshdad123_&é"_JIHfduh3i123d!!:'

/etc/apache2/sites-available/website.com.conf:

WSGIPythonHome /var/www/website.com/akb/env3.7

<VirtualHost *:80>
    ServerName website.com
    ServerAdmin [email protected]
    ServerAlias www.website.com

DocumentRoot /var/www/website.com/akb
    <Directory /var/www/website.com/akb>
        <IfVersion < 2.4>
            Order allow,deny,
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>        

WSGIApplicationGroup %{GLOBAL}

WSGIScriptAlias / /var/www/website.com/wsgi/website.com.wsgi

<Directory /var/www/website.com/akb>
        WSGIProcessGroup website.com
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>

Alias /static /var/www/website.com/akb/static
    <Directory /var/www/website.com/akb/static>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>

Alias /templates /var/www/website.com/akb/templates
    <Directory /var/www/website.com/akb/templates>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>

ErrorLog /var/www/website.com/errors/error.log
    LogLevel info
    CustomLog /var/www/website.com/errors/access.log combined

Solution

I finally solved this problem :

  • By changing the OS directory to one of my environments (os.chdir).
  • Use chown/chmod to give edit/create access to www-data

Related Problems and Solutions