djangoapachemod-wsgipyenv

mod_wsgi: Permission denied: Unable to stat Python home


I was trying to deploy django app using mod_wsgi (version=4.9.4), apache2 and pyenv, also created the python virtual-env using pyenv, here is the apache configuration used

<VirtualHost *:80>
        ServerName dev.<domain>.com
        WSGIDaemonProcess dev.<domain>.com python-home=<path><to><pyenv-virtualenv> python-path=<projectdir>
        WSGIProcessGroup dev.<domain>.com
        WSGIApplicationGroup %{GLOBAL}

        ErrorLog "${APACHE_LOG_DIR}/timesheet_internal.log"
        CustomLog "${APACHE_LOG_DIR}/timesheet_internal.log" common
        LogLevel Warn

        Alias /static /var/www/<projectpath>/static
        <Directory /var/www/<projectpath>/static>
            Require all granted
        </Directory>
    
    Alias /.well-known/acme-challenge/ "/var/www/<projectpath>/.well-known/acme-challenge/"
    <Directory "/var/www/<projectpath>/">
        AllowOverride None
        Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
        Require method GET POST DELETE PATCH OPTIONS
    </Directory>

    WSGIScriptAlias / /var/www/<path>/wsgi.py
    <Directory "/var/www/<path>/wsgi.py">
      Require all granted
    </Directory>
</VirtualHost>

<pyenv-virtualenv>/bin/python manage.py --collectstatic --no-input --clear and

<pyenv-virtualenv>/bin/python manage.py migrate --no-input both these commands are executed successfully and the app also worked in my local, but on deploying in ec2(ubuntu 22.04) the application ran into following errors

[Mon Jun 12 14:17:51.520596 2023] [wsgi:warn] [pid 1224676:tid 140062563575680] (13)Permission denied: mod_wsgi (pid=1224676): Unable to stat Python home /home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Python path configuration:
  PYTHONHOME = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11'
  sys._base_executable = '/usr/bin/python3'
  sys.base_prefix = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  sys.base_exec_prefix = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  sys.platlibdir = 'lib'
  sys.executable = '/usr/bin/python3'
  sys.prefix = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  sys.exec_prefix = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  sys.path = [
    '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python311.zip',
    '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11',
    '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f62db592780 (most recent call first):
  <no Python frame>

i have checked the permissions of the virtualenv and they are set to ubuntu:ubuntu , i have also libpython3.11-minimal

i got stuck at this point, can anyone please suggest a solution?

Thanks in advance.

i tried reinstalling the virtual-env, python version and python minimals, but that didnt resolve the issue


Solution

  • My best guess from looking at your error and reading the mod_wsgi docs on virtual environments is that the issue is that Apache is running as user www-data and doesn't have permission to access /home/ubuntu/, which is where the virtual environment is:

    Do be aware that the user that Apache runs your code as will need to be able to access the Python virtual environment. On some Linux distributions, the home directory of a user account is not accessible to other users. Rather than change the permissions on your home directory, it might be better to consider locating your WSGI application code and any Python virtual environment outside of your home directory.

    The docs suggest having the virtual environment at a path like /usr/local/venvs/example, which should be accessible to all users.

    Note that someone else actually posted the same error you're getting before and what they said solved it was "changing the permission of myuser's folder: sudo chmod 755 /home/<user>".

    So you can either:

    1. Move the virtualenv to a directory like /usr/local/venvs/ (the recommended approach), or
    2. Change the permissions on /home/ubuntu/ to something like 755 (not recommended).