apache mod_wsgi:Permission denied:无法stat Python home

2w2cym1i  于 2023-06-24  发布在  Apache
关注(0)|答案(1)|浏览(182)

我尝试使用mod_wsgi(version=4.9.4)、apache 2和pyenv部署django应用程序,还使用pyenv创建了python virtual-env,这里是使用的apache配置

  1. <VirtualHost *:80>
  2. ServerName dev.<domain>.com
  3. WSGIDaemonProcess dev.<domain>.com python-home=<path><to><pyenv-virtualenv> python-path=<projectdir>
  4. WSGIProcessGroup dev.<domain>.com
  5. WSGIApplicationGroup %{GLOBAL}
  6. ErrorLog "${APACHE_LOG_DIR}/timesheet_internal.log"
  7. CustomLog "${APACHE_LOG_DIR}/timesheet_internal.log" common
  8. LogLevel Warn
  9. Alias /static /var/www/<projectpath>/static
  10. <Directory /var/www/<projectpath>/static>
  11. Require all granted
  12. </Directory>
  13. Alias /.well-known/acme-challenge/ "/var/www/<projectpath>/.well-known/acme-challenge/"
  14. <Directory "/var/www/<projectpath>/">
  15. AllowOverride None
  16. Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
  17. Require method GET POST DELETE PATCH OPTIONS
  18. </Directory>
  19. WSGIScriptAlias / /var/www/<path>/wsgi.py
  20. <Directory "/var/www/<path>/wsgi.py">
  21. Require all granted
  22. </Directory>
  23. </VirtualHost>

<pyenv-virtualenv>/bin/python manage.py --collectstatic --no-input --clear
<pyenv-virtualenv>/bin/python manage.py migrate --no-input这两个命令都成功执行,应用程序也在我的本地工作,但在ec2(ubuntu 22.04)中部署时,应用程序遇到以下错误

  1. [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.
  2. Python path configuration:
  3. PYTHONHOME = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  4. PYTHONPATH = (not set)
  5. program name = 'python3'
  6. isolated = 0
  7. environment = 1
  8. user site = 1
  9. safe_path = 0
  10. import site = 1
  11. is in build tree = 0
  12. stdlib dir = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11'
  13. sys._base_executable = '/usr/bin/python3'
  14. sys.base_prefix = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  15. sys.base_exec_prefix = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  16. sys.platlibdir = 'lib'
  17. sys.executable = '/usr/bin/python3'
  18. sys.prefix = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  19. sys.exec_prefix = '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env'
  20. sys.path = [
  21. '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python311.zip',
  22. '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11',
  23. '/home/ubuntu/.pyenv/versions/3.11.3/envs/timesheet_env/lib/python3.11/lib-dynload',
  24. ]
  25. Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
  26. Python runtime state: core initialized
  27. ModuleNotFoundError: No module named 'encodings'
  28. Current thread 0x00007f62db592780 (most recent call first):
  29. <no Python frame>

我已经检查了virtualenv的权限,它们被设置为ubuntu:ubuntu,我也有libpython3.11-minimal
我在这一点上卡住了,有人能提出解决方案吗?
先谢谢你了。
我尝试重新安装virtual-env、python版本和python minimals,但这并没有解决问题

jjjwad0x

jjjwad0x1#

通过查看您的错误和阅读the mod_wsgi docs on virtual environments,我最好的猜测是,问题是Apache以用户www-data运行,并且没有访问/home/ubuntu/的权限,这是虚拟环境所在:
请注意,Apache运行代码的用户需要能够访问Python虚拟环境。在某些Linux发行版上,其他用户无法访问用户帐户的主目录。与其更改主目录上的权限,不如考虑将WSGI应用程序代码和任何Python虚拟环境定位在主目录之外。
文档建议将虚拟环境设置在/usr/local/venvs/example这样的路径上,所有用户都可以访问。
请注意,someone else actually posted the same error you're getting beforewhat they said solved it正在“更改myuser文件夹的权限:sudo chmod 755 /home/<user>“。
因此,您可以:
1.将virtualenv移动到像/usr/local/venvs/这样的目录(推荐的方法),或者
1.将/home/ubuntu/上的权限更改为类似755的权限(不推荐)。

相关问题