无法在虚拟环境中运行django wsgi

时间:2020-01-27 14:55:34

标签: python django apache2 mod-wsgi wsgi

我正在尝试通过使用wsgi在apache2上运行django应用,但似乎无法设置虚拟环境的使用。

要安装mod_wsgi,我在使用python3.7时遵循了Serve Python 3.7 with mod_wsgi on Ubuntu 16

我用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手动启动应用程序相比,它没有使用虚拟环境。

设置不使用虚拟环境有什么问题?

1 个答案:

答案 0 :(得分:0)

所以最终我找到了解决方案。问题在于mod_wsgi模块是使用Python3.5构建的,而虚拟环境使用的是Python3.7。

Mod wsgi installation guide所述手动构建mod_wsgi并按here所述链接正确的mod_wsgi解决了问题