【问题标题】:Unable to run django wsgi with virtual environment无法使用虚拟环境运行 django wsgi
【发布时间】: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 手动启动应用程序相比,可以明显看出它没有使用虚拟环境。

我的设置有什么问题没有使用虚拟环境?

【问题讨论】:

标签: python django apache2 mod-wsgi wsgi


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-03-06
    • 2019-01-19
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2014-11-09
    • 2021-09-13
    • 2018-01-11
    • 2020-05-25
    相关资源
    最近更新 更多