从虚拟环境

时间:2015-11-14 19:31:01

标签: python apache flask virtualenv wsgi

我有一个简单的烧瓶应用程序,我想从互联网访问。 操作系统是debian7,不幸的是它无法更改。

我做了什么: - 下载并安装Python3.4到/usr/local/opt/python3.4.3 - 为py3安装了wsgi lib - 安装了virtualenv并在我的flask项目文件夹中创建了新的env - 在本环境中安装烧瓶和其他一些我需要的包装 - 按如下方式创建VirtualHost:

cat /etc/apache2/sites-available/Monitor
Listen 8080

<VirtualHost *:8080>
                ServerName {ip address}
                ServerAdmin {my email}
                WSGIScriptAlias / /var/www/Monitor/monitor.wsgi

                <Directory /var/www/Monitor/Monitor/>
                        Order allow,deny
                        Allow from all
                </Directory>

                Alias /static /var/www/Monitor/Monitor/static

                <Directory /var/www/Monitor/Monitor/static/>
                        Order allow,deny
                        Allow from all
                </Directory>

                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

现在当我尝试从互联网上访问应用程序时,我在error.log中看到了这些错误

[Sat Nov 14 22:16:04 2015] [error] /usr/local/bin/python3
[Sat Nov 14 22:16:04 2015] [error] [client ip] mod_wsgi (pid=13450): Target WSGI script '/var/www/Monitor/monitor.wsgi' cannot be loaded as Python module.
[Sat Nov 14 22:16:04 2015] [error] [client ip] mod_wsgi (pid=13450): Exception occurred processing WSGI script '/var/www/Monitor/monitor.wsgi'.
[Sat Nov 14 22:16:04 2015] [error] [client ip] Traceback (most recent call last):
[Sat Nov 14 22:16:04 2015] [error] [client ip]   File "/var/www/Monitor/monitor.wsgi", line 21, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip]     from Monitor import app as application
[Sat Nov 14 22:16:04 2015] [error] [client ip]   File "/var/www/Monitor/Monitor/__init__.py", line 4, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip]     from flask import Flask, render_template, url_for
[Sat Nov 14 22:16:04 2015] [error] [client ip]   File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/flask/__init__.py", line 17, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip]     from werkzeug.exceptions import abort
[Sat Nov 14 22:16:04 2015] [error] [client ip]   File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/werkzeug/__init__.py", line 152, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip]     __import__('werkzeug.exceptions')
[Sat Nov 14 22:16:04 2015] [error] [client ip]   File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/werkzeug/exceptions.py", line 113
[Sat Nov 14 22:16:04 2015] [error] [client ip]     return u'<p>%s</p>' % escape(self.description)
[Sat Nov 14 22:16:04 2015] [error] [client ip]                       ^
[Sat Nov 14 22:16:04 2015] [error] [client ip] SyntaxError: invalid syntax

从我在互联网上找到的内容中,当尝试使用python版本&lt; = 3.3启动烧瓶应用程序时出现这样的错误,这不是我认为的情况(我如何检查?)

这是启动应用程序的wsgi

#!/usr/bin/env python3

import os
import sys
import logging


logging.basicConfig(stream=sys.stderr)

PROJECT_DIR = '/var/www/Monitor/'
sys.path.insert(0, PROJECT_DIR)

def execfile(filename):
    globals = dict(__file__ = filename)
    exec(open(filename).read(), globals)


activate_this = '/var/www/Monitor/Monitor/venv/bin/activate_this.py'
execfile(activate_this)

from Monitor import app as application

非常感谢任何帮助。如有必要,将提供任何其他信息。

1 个答案:

答案 0 :(得分:0)

Debian 7上的默认Python3是3.2.3,libapache2-mod-wsgi是3.3版本。

你使用默认的Python运行你的应用程序,因为mod-wsgi必须针对一个且只有一个python版本编译(在本例中为python 3.3)。

mod-wsgi 3.3

如果您想使用Python 3.4只运行一个应用程序(或许多应用程序),请尝试使用python 3.4重新编译mod-wsgi

mod-wsgi 4.1.x或更新

这个较新版本的mod_wsgi提供了一种方法,可以针对多个Python版本安装mod_wsgi,并使用提供的脚本为每个版本运行一个Apache实例。该脚本接管了Apache配置的所有设置,因此您无需担心它。

https://pypi.python.org/pypi/mod_wsgi

旧答案

要更改Python版本,请在Apache conf:

中设置
WSGIPythonHome /usr/local/opt/python3.4.3/
WSGIPythonPath /var/www/Monitor:/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages:/usr/local/opt/python3.4.3/lib/site-packages

有关详细信息,请阅读https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonHome