我正在尝试使用 wsgi 使用 apache2 运行我的 django 应用程序,但似乎无法设置虚拟环境的使用。
要安装mod_wsgi
,我按照 Serve Python 3.7 with mod_wsgi on Ubuntu 16
操作因为我使用的是python3.7。
我使用virtualenv -p python3.7 venv
创建了一个虚拟环境
通过 python manage.py runserver
手动使用虚拟环境可以正常工作并按预期启动服务器。
为了使用 apache2 启动它,我配置了 my-app.conf
:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@example.com
ProxyPass / http://0.0.0.0:8000/
ProxyPassReverse / http://127.0.0.1:8000/
<Directory /home/path/to/project/static>
Require all granted
</Directory>
<Directory /home/path/to/venv/>
Require all granted
</Directory>
<Directory /home/path/to/project/server>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess project \
python-home=/home/path/to/venv/ \
python-path=/home/path/to/project/server/
WSGIProcessGroup project
WSGIScriptAlias /project /home/path/to/project/server/wsgi.py \
process-group=example.com \
application-group=%{GLOBAL}
Alias /static/ /home/path/to/project/static/
ErrorLog /var/log/apache2/mysite-error.log
LogLevel warn
CustomLog /var/log/apache2/mysite-access.log combined
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
和my-app-ssl.conf
:
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
</IfModule>
我最终收到错误:ImportError:没有名为“django”的模块
这是基于 venv 设置不正确。我在 wsgi.py
中添加了一些代码:
for k in sorted(os.environ.keys()):
v = os.environ[k]
print ('%-30s %s' % (k,v[:70]))
并且可以明显地看到,与使用 virtualenv 手动启动应用程序相比,它没有使用虚拟环境。
我的设置有什么问题,没有使用虚拟环境?
最佳答案
最后我找到了解决方案。问题是mod_wsgi模块是用Python3.5构建的,而虚拟环境使用的是Python3.7。
按照此处所述手动构建 mod_wsgi Mod wsgi installation guide并按照描述链接正确的链接 here问题解决了
关于python - 无法在虚拟环境中运行 django wsgi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59933564/