我尝试使用mod_wsgi(version=4.9.4)、apache 2和pyenv部署django应用程序,还使用pyenv创建了python virtual-env,这里是使用的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>
我已经检查了virtualenv的权限,它们被设置为ubuntu:ubuntu
,我也有libpython3.11-minimal
我在这一点上卡住了,有人能提出解决方案吗?
先谢谢你了。
我尝试重新安装virtual-env、python版本和python minimals,但这并没有解决问题
1条答案
按热度按时间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 before和what they said solved it正在“更改myuser文件夹的权限:
sudo chmod 755 /home/<user>
“。因此,您可以:
1.将virtualenv移动到像
/usr/local/venvs/
这样的目录(推荐的方法),或者1.将
/home/ubuntu/
上的权限更改为类似755
的权限(不推荐)。