FlaskApp使用mod_wsgi在apache中返回http 500 [英] FlaskApp returning http 500 in apache with mod_wsgi

查看:165
本文介绍了FlaskApp使用mod_wsgi在apache中返回http 500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过apache和mod_wsgi托管我的python 3.4 flask应用程序。通过烧瓶自己的服务器运行应用程序正常工作。该应用程序是在一个虚拟的环境,pyvenv-3.4。



然而,当试图连接到浏览器中的Apache服务器,它会引发500 HTTP错误。配置和日志已附加。我认为这与使用pyvenv而不是virtualenv(来自pip)有关。
Flask文档告诉我用这行激活虚拟环境

$ p $激活这个路径到/ env / bin / activate_this.py'

但是,由于文件不存在,会产生IOError。我试着将它指向activate文件,而不是使用activate.csh,activate.fish。所有文件都会在停用行上产生SyntaxError。

如何通过Apache和我的virtualenv运行这个应用程序?



< #!/ usr / bin / python
activate_this =

'/ var / www / FlaskApp / FlaskApp / bin / activate'
execfile(activate_this,dict(__ file __ = activate_this))
import sys
导入日志

日志.basicConfig(stream = sys.stderr)
从FlaskApp导入应用程序到应用程序
应用程序

$ sys.path.insert(0,/ var / www / FlaskApp /) .secret_key ='一些密钥'

Apache VirtualHost

 < VirtualHost *:80> 
ServerName example.org#我的服务器名称
ServerAlias gallifrey 192.168.0.84
ServerAdmin admin@example.org#my admin
WSGIScriptAlias / flask / var / www / FlaskApp / flaskapp。 wsgi
< Directory / var / www / FlaskApp / FlaskApp />
允许,拒绝
允许所有
< / Directory>
Alias / static / var / www / FlaskApp / FlaskApp / static
< Directory / var / www / FlaskApp / FlaskApp / static />
允许,拒绝
允许所有
< / Directory>
ErrorLog $ {APACHE_LOG_DIR} /error.log
LogLevel警告
CustomLog $ {APACHE_LOG_DIR} /access.log合并
< / VirtualHost>

层次结构

< pre $ / var / www / FlaskApp
flaskapp.wsgi
FlaskApp /
bin /
activate
activate.csh
activate.fish
easy_install,easy_install-3.4
pip,pip3,pip3.4
python,python3,python3.4
include /
lib /
scripts /
static /
templates /
app.py
__init__.py

尝试打开网页,我得到一个http 500错误:
$ b Apache error.log

  [2014年5月2日22:22:58] [error] [client 192.168.0.81] mod_wsgi(pid = 31629):Target WSGI脚本'/var/www/FlaskApp/flaskapp.wsgi'不能被加载为Python模块。 
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] mod_wsgi(pid = 31629):处理WSGI脚本'/var/www/FlaskApp/flaskapp.wsgi'时发生异常。
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] Traceback(最近调用最后一次):
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81]文件/var/www/FlaskApp/flaskapp.wsgi,第3行,在< module>
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] execfile(activate_this,dict(__ file __ = activate_this))
[Fri May 02 10:22:58 2014] [错误] [客户端192.168.0.81]文件/ var / www / FlaskApp / FlaskApp / bin / activate,第4行
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81 ] 2014年5月02日10:22:58 [错误] [client 192.168.0.81] ^
[2014年5月2日10:22:58] [错误] [客户端] 192.168.0.81] SyntaxError:invalid syntax


解决方案

请遵循 VirtualEnvironments说明,也可以复制 virtualenv 的 activate_this.py 脚本确实:

  import sys 
import os
$ b $ old_os_path = os.environ ['PATH']
os.environ ['PATH'] = os.path.dirname(os.path .abspath(__ file__))+ os.pathsep + old_o s_path
ase = os.path.dirname(os.path.dirname(os.path.abspath(__ file__)))
if sys.platform =='win32':
site_packages = os .path.join(base,'Lib','site-packages')
else:
site_packages = os.path.join(base,'lib','python%s'%sys.version [:3],'site-packages')
prev_sys_path = list(sys.path)
导入网站
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
#将添加的项目移动到路径前面:
new_sys_path = []
列表中的项目($ sys $ b如果item不在prev_sys_path中:
new_sys_path.append(item)
sys.path.remove(item)
sys.path [:0] = new_sys_path

$ b

你可以使这个更通用的函数:

 导入sys 
导入os

def activate_venv(路径):
如果sys.platform =='win32':
bin_dir = os.path。加入(路径,'脚本')
site_p ackages = os.path.join(base,'Lib','site-packages')
else:
bin_dir = os.path.join(path,'bin')
site_packages = os.path.join(BASE,'lib','python%s'%sys.version [:3],'site-packages')
os.environ ['PATH'] = bin_dir + os.pathsep + os.environ ['PATH']
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.prefix,sys.real_prefix = path ,sys.prefix

#移动添加的项目到路径的前面:
new_sys_path = []
列表中的项目(sys.path):
如果item不在prev_sys_path中:
new_sys_path.append(item)
sys.path.remove(item)
sys.path [:0] = new_sys_path
activate_venv
,然后通过 $ b

os.path.dirname(os.path.abspath(__ file __))的结果中

  from somemodule import activate_venv 
import os.path
activate_venv(os.path.dirname(os.path.abspath(__ file__)))


I'm trying to host my python 3.4 flask app through apache and mod_wsgi. Running the app through flasks own server works fine. The app was made in a virtual environment, pyvenv-3.4.

However, when trying to connect to the apache server in a browser, it throws a 500 http error. Configs and logs are attached. I think this has to do with using pyvenv and not virtualenv (from pip). Flask documentation tells me to activate the virtual environment using this line

activate_this = '/path/to/env/bin/activate_this.py'

however, that produces an IOError as the file does not exist. I tried pointing it to the 'activate'-file instead, and activate.csh, activate.fish, with no luck. All files produces SyntaxError on the deactivate-line.

How can I run this app through Apache with my virtualenv?

flaskapp.wsgi

#!/usr/bin/python
activate_this = '/var/www/FlaskApp/FlaskApp/bin/activate'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'some secret key'

Apache VirtualHost

<VirtualHost *:80>
            ServerName example.org # my server name
            ServerAlias gallifrey 192.168.0.84
            ServerAdmin admin@example.org # my admin
            WSGIScriptAlias /flask /var/www/FlaskApp/flaskapp.wsgi
            <Directory /var/www/FlaskApp/FlaskApp/>
                    Order allow,deny
                    Allow from all
            </Directory>
            Alias /static /var/www/FlaskApp/FlaskApp/static
            <Directory /var/www/FlaskApp/FlaskApp/static/>
                    Order allow,deny
                    Allow from all
            </Directory>
            ErrorLog ${APACHE_LOG_DIR}/error.log
            LogLevel warn
            CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Hierarchy

/var/www/FlaskApp
  flaskapp.wsgi
  FlaskApp/
    bin/
      activate
      activate.csh
      activate.fish
      easy_install, easy_install-3.4
      pip, pip3, pip3.4
      python, python3, python3.4
    include/
    lib/
    scripts/
    static/
    templates/
    app.py
    __init__.py

Trying to open the webpage I get a http 500 error:

Apache error.log

[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] mod_wsgi (pid=31629): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] mod_wsgi (pid=31629): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] Traceback (most recent call last):
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81]   File "/var/www/FlaskApp/flaskapp.wsgi", line 3, in <module>
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81]     execfile(activate_this, dict(__file__=activate_this))
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81]   File "/var/www/FlaskApp/FlaskApp/bin/activate", line 4
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81]     deactivate () {
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81]                   ^
[Fri May 02 10:22:58 2014] [error] [client 192.168.0.81] SyntaxError: invalid syntax

解决方案

You can either follow the VirtualEnvironments instructions, or you can replicate what virtualenv's activate_this.py script does:

import sys
import os

old_os_path = os.environ['PATH']
os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if sys.platform == 'win32':
    site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
    site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path

You could make this a more general function:

import sys
import os

def activate_venv(path):
    if sys.platform == 'win32':
        bin_dir = os.path.join(path, 'Scripts')
        site_packages = os.path.join(base, 'Lib', 'site-packages')
    else:
        bin_dir = os.path.join(path, 'bin')
        site_packages = os.path.join(BASE, 'lib', 'python%s' % sys.version[:3], 'site-packages')
    os.environ['PATH'] = bin_dir + os.pathsep + os.environ['PATH']
    prev_sys_path = list(sys.path)
    import site
    site.addsitedir(site_packages)
    sys.prefix, sys.real_prefix = path, sys.prefix

    # Move the added items to the front of the path:
    new_sys_path = []
    for item in list(sys.path):
        if item not in prev_sys_path:
            new_sys_path.append(item)
            sys.path.remove(item)
    sys.path[:0] = new_sys_path

Put that in a module on your default Python module search path, import activate_venv and pass in the result of os.path.dirname(os.path.abspath(__file__)):

from somemodule import activate_venv
import os.path
activate_venv(os.path.dirname(os.path.abspath(__file__)))

这篇关于FlaskApp使用mod_wsgi在apache中返回http 500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆