简体   繁体   English

从虚拟环境运行Flask应用程序

[英]Running Flask application from virtual environment

I have a simple flask app that i want to be accessible from internet. 我有一个简单的烧瓶应用程序,我希望可以从互联网访问它。 OS is debian7, and unfortunately it can't be changed. 操作系统是debian7,很遗憾,它无法更改。

What I did: - downloaded and installed Python3.4 into /usr/local/opt/python3.4.3 - installed wsgi lib for py3 - installed virtualenv and created new env in my flask project folder - installed flask together with some other packages that I need within this env - created VirtualHost as follows: 我的工作:-将Python3.4下载并安装到/usr/local/opt/python3.4.3-为py3安装了wsgi lib-在flask项目文件夹中安装了virtualenv并创建了新的env-与我安装的一些其他软件包一起安装了flask在此环境中需要-如下创建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>

now when i try to access the app from the internet I see these errors in error.log 现在,当我尝试从互联网访问该应用程序时,我在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

From what I could find in the internet, such errors appear when trying to start flask app with python version <= 3.3, which is not my case I think (how do i check?) 从我在互联网上可以找到的信息来看,当尝试使用python版本<= 3.3来启动flask应用程序时,会出现此类错误,这不是我想的情况(我应如何检查?)

here is my wsgi that starts the app 这是我启动应用程序的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

Any help would be very much appreciated. 任何帮助将不胜感激。 Any additional information will be supplied if necessary. 如有必要,将提供任何其他信息。

Default Python3 on Debian 7 is 3.2.3, libapache2-mod-wsgi is in version 3.3. Debian 7上的默认Python3是3.2.3,libapache2-mod-wsgi是版本3.3。

You Apache run Your app with default Python because mod-wsgi has to be compiled against one and only one python version (in this case python 3.3). 您的Apache使用默认的Python运行您的应用程序,因为mod-wsgi必须针对一个且只有一个python版本(在本例中为python 3.3)进行编译。

mod-wsgi 3.3 mod-wsgi 3.3

If You want to run only one app (or many apps) with Python 3.4, try to recompile mod-wsgi with python 3.4 如果您只想使用Python 3.4运行一个应用程序(或多个应用程序),请尝试使用python 3.4重新编译mod-wsgi

mod-wsgi 4.1.x or newer mod-wsgi 4.1.x或更高版本

This newer version of mod_wsgi provides a way of installing mod_wsgi against multiple Python versions and running up an Apache instance for each using a provided script. 较新的mod_wsgi版本提供了一种针对多个Python版本安装mod_wsgi并使用提供的脚本为每个版本运行Apache实例的方法。 The script takes over all the setup of the Apache configuration so you do not need to worry about it. 该脚本接管了Apache配置的所有设置,因此您不必担心它。

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

old answer 旧答案

To change Python version, set in Apache conf: 要更改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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM