【问题标题】:Can't run Apache2 with virtualenv无法使用 virtualenv 运行 Apache2
【发布时间】:2018-09-04 19:56:03
【问题描述】:

我正在制作一个基于 Django 的网站,服务器上安装了 Python 3.5,但我的项目需要 Python 3.6。我决定使用 virtualenv。我成功安装了所需的 Python 版本,但无法使用 virtualenv 使其与 Apatche2 一起使用。

网站只能在 Python 2.7 上运行,否则什么都不会发生,页面加载很长时间没有任何错误。

这是我尝试在 Python 3.6 上运行的 VirtualHost 配置。

<VirtualHost *:443>
    ServerName <site_adress>:443
    ServerAdmin admin@<site_adress>

    DocumentRoot /var/www/html/MMServer

    ErrorLog /var/www/logs/error.log
    CustomLog /var/www/logs/custom.log combined

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/mm.cert
    SSLCertificateKeyFile /etc/apache2/ssl/mm.key

    Alias /static/ /var/www/html/MMServer/static/

    <Directory /var/www/html/MMServer/static>
        Require all granted
    </Directory>

    WSGIDaemonProcess MMServer python-path=/var/www/html/MMServer python-home=/var/www/html/venv
    WSGIProcessGroup MMServer
    WSGIScriptAlias / /var/www/html/MMServer/mm_server/wsgi.py

    <Directory /var/www/html/MMServer/mm_server>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

</VirtualHost>

下面是我的wsgi.py文件:

import os
import sys

def application(environ, start_response):
    start_response('200 OK',[('Content-type','text/html')])
    return [sys.version]

我能通过这种方式得到的只有一件事(WSGIDaemonProcessWSGIProcessGroup 被删除)是:

2.7.13(默认,2017 年 11 月 24 日,17:33:09)[GCC 6.3.0 20170516]

编辑 1:

有可能我缺少软件包,因为我重新安装了 python 3.5,需要哪些软件包才能工作?

解决方案:

我做了两件事,我不确定什么对我有帮助,但首先我禁用了一个 mod (a2dismod wsgi) 并删除了一个包 libapache2-mod-wsgi

选项 1:

apt-get install libapache2-mod-wsgi-py3

选项 2:

我从源安装了mod-wsgi
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.3.tar.gz
tar -xf 4.6.3.tar.gz
./configure --with-python=/usr/local/bin/python3.6
make
make install
现在一切正常。
谢谢@Graham Dumpleton,你的回答很有帮助。

【问题讨论】:

  • 谢谢。这对我有用。这需要记录在 Django 官方网站上。

标签: python django apache virtualenv mod-wsgi


【解决方案1】:

mod_wsgi 模块是链接到 Python 库的 C 代码。因此,为其编译的 Python 版本嵌入在模块中。它不只是执行python 程序。这意味着它必须针对您要使用的 Python 版本进行编译。您不能通过虚拟环境强制它使用不同的 Python 版本。这在关于在虚拟环境中使用 mod_wsgi 的文档中有说明。

简而言之,您需要卸载 mod_wsgi 模块(可能是操作系统打包的模块),然后自己从源代码安装 mod_wsgi,针对您要使用的 Python 版本进行编译。最简单的方法是使用pip install 方法。

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 2012-10-18
    • 2019-07-07
    • 2012-10-31
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    相关资源
    最近更新 更多