mod_wsgi: 权限被拒绝: 无法统计 Python 主目录

huangapple go评论90阅读模式
英文:

mod_wsgi: Permission denied: Unable to stat Python home

问题

我尝试使用mod_wsgi(版本=4.9.4),apache2和pyenv部署Django应用程序,还使用pyenv创建了Python虚拟环境,下面是使用的Apache配置:

<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

<pyenv-virtualenv>/bin/python manage.py migrate --no-input这两个命令都执行成功,并且应用程序在我的本地也正常工作。但是在EC2(Ubuntu 22.04)上部署时,应用程序遇到了以下错误:

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

我已检查了虚拟环境的权限,它们设置为ubuntu:ubuntu,我也安装了libpython3.11-minimal

我在这一点上遇到了困难,有人能提出解决方案吗?

先感谢。我尝试重新安装了虚拟环境、Python版本和Python最小安装包,但这没有解决问题。

英文:

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

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

        ErrorLog &quot;${APACHE_LOG_DIR}/timesheet_internal.log&quot;
        CustomLog &quot;${APACHE_LOG_DIR}/timesheet_internal.log&quot; common
        LogLevel Warn

        Alias /static /var/www/&lt;projectpath&gt;/static
        &lt;Directory /var/www/&lt;projectpath&gt;/static&gt;
            Require all granted
        &lt;/Directory&gt;
    
    Alias /.well-known/acme-challenge/ &quot;/var/www/&lt;projectpath&gt;/.well-known/acme-challenge/&quot;
    &lt;Directory &quot;/var/www/&lt;projectpath&gt;/&quot;&gt;
        AllowOverride None
        Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
        Require method GET POST DELETE PATCH OPTIONS
    &lt;/Directory&gt;

    WSGIScriptAlias / /var/www/&lt;path&gt;/wsgi.py
    &lt;Directory &quot;/var/www/&lt;path&gt;/wsgi.py&quot;&gt;
      Require all granted
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;

&lt;pyenv-virtualenv&gt;/bin/python manage.py --collectstatic --no-input --clear and

&lt;pyenv-virtualenv&gt;/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 = &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env&#39;
  PYTHONPATH = (not set)
  program name = &#39;python3&#39;
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11&#39;
  sys._base_executable = &#39;/usr/bin/python3&#39;
  sys.base_prefix = &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env&#39;
  sys.base_exec_prefix = &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env&#39;
  sys.platlibdir = &#39;lib&#39;
  sys.executable = &#39;/usr/bin/python3&#39;
  sys.prefix = &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env&#39;
  sys.exec_prefix = &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env&#39;
  sys.path = [
    &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python311.zip&#39;,
    &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11&#39;,
    &#39;/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11/lib-dynload&#39;,
  ]
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 &#39;encodings&#39;

Current thread 0x00007f62db592780 (most recent call first):
  &lt;no Python frame&gt;

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

答案1

得分: 1

根据您的错误信息和阅读mod_wsgi虚拟环境文档的情况,我最好的猜测是问题在于Apache正在以用户www-data的身份运行,并且没有权限访问/home/ubuntu/,这正是虚拟环境所在的位置:

文档建议将虚拟环境放在类似/usr/local/venvs/example的路径上,这应该对所有用户都可访问。

请注意,其他人之前实际上发布了与您收到的相同错误,并且他们所说的解决方法是更改myuser文件夹的权限:sudo chmod 755 /home/<user>

因此,您可以选择:

  1. 将虚拟环境移动到类似/usr/local/venvs/的目录中(推荐的方法),或者
  2. 更改/home/ubuntu/的权限为755(不推荐)。
英文:

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/&lt;user&gt;".

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).

huangapple
  • 本文由 发表于 2023年6月12日 22:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定