【发布时间】:2020-01-27 14:55:34
【问题描述】:
我正在尝试使用 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: No module named 'django'
这是基于未正确设置的venv。我在wsgi.py 中添加了一些代码:
for k in sorted(os.environ.keys()):
v = os.environ[k]
print ('%-30s %s' % (k,v[:70]))
与使用 virtualenv 手动启动应用程序相比,可以明显看出它没有使用虚拟环境。
我的设置有什么问题没有使用虚拟环境?
【问题讨论】:
-
使用 django 的指南作为参考 docs.djangoproject.com/en/3.0/howto/deployment/wsgi/modwsgi
-
谢谢,这就是我用的,还有pawelzny.com/server/django/2018/02/26/…,但不幸的是我无法让它工作
标签: python django apache2 mod-wsgi wsgi