英文:
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
<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
答案1
得分: 1
根据您的错误信息和阅读mod_wsgi虚拟环境文档的情况,我最好的猜测是问题在于Apache正在以用户www-data
的身份运行,并且没有权限访问/home/ubuntu/
,这正是虚拟环境所在的位置:
文档建议将虚拟环境放在类似/usr/local/venvs/example
的路径上,这应该对所有用户都可访问。
请注意,其他人之前实际上发布了与您收到的相同错误,并且他们所说的解决方法是更改myuser文件夹的权限:sudo chmod 755 /home/<user>
。
因此,您可以选择:
- 将虚拟环境移动到类似
/usr/local/venvs/
的目录中(推荐的方法),或者 - 更改
/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/<user>
".
So you can either:
- Move the virtualenv to a directory like
/usr/local/venvs/
(the recommended approach), or - Change the permissions on
/home/ubuntu/
to something like755
(not recommended).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论