python-FlaskApp使用mod_wsgi在Apache中返回http 500

我正在尝试通过apache和mod_wsgi托管我的python 3.4 flask应用程序.通过flasks自己的服务器运行应用程序可以正常工作.该应用程序是在pyvenv-3.4虚拟环境中制作的.

但是,当尝试在浏览器中连接到apache服务器时,它将引发500 http错误.随附配置和日志.我认为这与使用pyvenv而不是virtualenv(来自pip)有关.
Flask文档告诉我使用此行激活虚拟环境

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

但是,由于文件不存在,因此会产生IOError.我尝试将其指向“ activate”文件,然后没有任何运气,activate.csh,activate.fish.所有文件都会在停用行上产生SyntaxError.

如何使用我的virtualenv通过Apache运行此应用程序?

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'

阿帕奇虚拟主机

<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>

层次结构

/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错误:

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

解决方法:

您可以遵循VirtualEnvironments instructions,也可以复制virtualenv的activate_this.py脚本执行的操作:

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

您可以使它成为更通用的功能:

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

将其放在默认Python模块搜索路径中的模块中,导入activate_venv并传递os.path.dirname(os.path.abspath(__ file__))的结果:

from somemodule import activate_venv
import os.path
activate_venv(os.path.dirname(os.path.abspath(__file__)))
上一篇:js生成随机用户名函数


下一篇:进入自动包围目录时,如何只调用一次workon?