使用mod_wsgi在Apache上运行最简单的Flask应用程序的一些问题

时间:2012-04-23 17:34:07

标签: python apache wsgi flask

我正在尝试运行一个使用Flask微框架开发的非常简单的Python Web应用程序。我正在使用Ubuntu 10.04 LTS进行VPS。我跟着this instructions以获得令人欣慰的“Hello world!”,这是一次成功的体验。但现在,我正在设置一个稍微复杂的配置,我无法使其正常工作。

我的应用程序的Apache Virtual Host配置是:

<VirtualHost *:80>
    ServerAdmin    user@example.org
    ServerName    domain.com
    ServerAlias    www.domain.com

    DocumentRoot    /srv/www/domain.com/public_html/

    LogLevel    info    # I've put this for debugging purposes.
    ErrorLog    /srv/www/domain.com/logs/error.log
    CustomLog    /srv/www/domain.com/logs/access.log combined

    WSGIDaemonProcess   username   user=username    group=username   threads=5   processes=5
    WSGIScriptAlias /myapplication/     /srv/www/domain.com/myapplication/myapplication.wsgi

    <Directory /srv/www/domain.com/myapplication/myapplication>
        WSGIProcessGroup    username
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>

    Alias /css    /srv/www/domain.com/public_html/css
    Alias /js    /srv/www/domain.com/public_html/js
    Alias /images    /srv/www/domain.com/public_html/images
</VirtualHost>

我的WSGI配置文件是:

# -*- coding: utf-8 -*-
from myapplication import app as application

上述配置有问题吗?

最后,Apache给了我一个500错误页面。所以我查看日志:

[info] mod_wsgi (pid=3132, process='', application='domain.com|/myapplication/'): Loading WSGI script '/srv/www/domain.com/myapplication/myapplication.wsgi'.
[error] mod_wsgi (pid=3132): Target WSGI script '/srv/www/domain.com/myapplication/myapplication.wsgi' cannot be loaded as Python module.
[error] mod_wsgi (pid=3132): Exception occurred processing WSGI script '/srv/www/domain.com/myapplication/myapplication.wsgi'.
[error] Traceback (most recent call last):
[error] File "/srv/www/domain.com/myapplication/myapplication.wsgi", line 3, in <module>
[error] from myapplication import app as application
[error] ImportError: No module named myapplication

树结构类似于:

/myapplication
    myapplication.wsgi
    myapplication (python package)

显然,Python无法在myapplication中找到sys.path模块。但我已经检查了印刷品,它就在那里。我知道这个问题可能很难解决,但我有点困惑。我怎么能找到正确的方向来解决这个问题?

1 个答案:

答案 0 :(得分:3)

@larsks向我指出了一个暂时但有效的解决方案。现在,myapplication.wsgi看起来像:

import os
import sys

sys.path.append('/srv/www/domain.com/myapplication/')
os.environ['PYTHON_EGG_CACHE'] = '/srv/www/domain.com/myapplication/.python-egg'

from myapplication import app as application

谢谢!