带有mod_wsgi和python的Apache会定期返回500

时间:2013-03-21 00:45:48

标签: python apache2 flask wsgi

我有一个在Python和Flask上运行的网站。据我所知,10次中有4次会返回5​​00错误。

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello world!"

这是我的.wsgi文件(我正在使用virtualenv,并在导入应用程序之前激活它):

activate_this = '/path/to/app/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from myapp import app as application

这是我的Apache主机配置:

<VirtualHost *:80>
    ServerName website.com

    WSGIDaemonProcess myapp user=ubuntu group=ubuntu processes=2 threads=5
    WSGIScriptAlias / /path/to/app/myapp.wsgi

    <Directory /path/to/app>
        WSGIProcessGroup myapp
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

欢迎任何指向调试的指针!

修改

Apache error.log:

mod_wsgi (pid=29581): Target WSGI script '/path/to/app/myapp.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=29581): Exception occurred processing WSGI script '/path/to/app/myapp.wsgi'.
Traceback (most recent call last):
  File "/path/to/app/myapp.wsgi", line 4, in <module>
    from myapp import app as application
ImportError: No module named myapp

似乎两个进程中的一个(29581)没有运行virtualenv位,因此没有找到要导入的文件。

编辑2:

这是在PATH中找不到的本地文件myapp.py。我需要将它包含在我的.wsgi文件的顶部:

import sys
sys.path.insert(0, '/path/to/myapp')

activate_this = '/path/to/myapp/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from myapp import app as application

0 个答案:

没有答案