内部服务器错误Apache和WSGI(使用Flask)

时间:2013-06-29 11:03:42

标签: python flask mod-wsgi wsgi internal-server-error

(对不起拳头问题,还没有回答任何问题,我会接受它!)

我正在尝试在ubuntu 12.04虚拟机中使用Apache2和mod_wsgi设置Flask,主要是为了在将来部署Flask应用程序时我知道如何做到这一点。大家都知道,我是Python和Flask的新手,但我熟悉PHP,因此也是一般的编程实践。

我正在关注this tutorial来设置Flask。我已经成功设置了本教程中定义的测试应用程序,但是当我尝试使用本教程设置现有应用程序时,我收到以下错误:

Internal Server Error
    The server encountered an internal error and was unable to complete your request.     
    Either the server is overloaded or there is an error in the application.

这是我的app.py文件,我怀疑这里有一个错误,但我找不到它,我在应用程序中添加了教程中的运行代码希望它可以工作,但它没有,我得到了相同的错误,我永远不会得到错误日志,所以我切换回“app.run()”,当我重新启动apache时,我得到一个错误日志:

import flask
import settings

# Views
from main import Main
from login import Login
from remote import Remote
from music import Music    

app = flask.Flask(__name__)
app.secret_key = settings.secret_key

# Routes
app.add_url_rule('/',
                view_func=Main.as_view('main'),
                methods=["GET"])
app.add_url_rule('/<page>/',
                 view_func=Main.as_view('page'),
                 methods=["GET"])
app.add_url_rule('/login/',
                 view_func=Login.as_view('login'),
                 methods=["GET", "POST"])
app.add_url_rule('/remote/',
                 view_func=Remote.as_view('remote'),
                 methods=['GET', 'POST'])
app.add_url_rule('/music/',
                 view_func=Music.as_view('music'),
                 methods=['GET'])

@app.errorhandler(404)
def page_not_found(error):
  return flask.render_template('404.html'), 404

#app.debug = True
app.run()

#if __name__ == '__main__':
    #"Are we in the __main__ scope? Start test server."
    #app.run(host='0.0.0.0',port=5000,debug=True)

这是返回的error.log文件。我读过它了

[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1] mod_wsgi (pid=5739): Target WSGI script '/home/carwyn/public_html/wsgi/learningflask.wsgi' cannot be loaded as Python module.
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1] mod_wsgi (pid=5739): Exception occurred processing WSGI script '/home/carwyn/public_html/wsgi/learningflask.wsgi'.
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/home/carwyn/public_html/wsgi/learningflask.wsgi", line 3, in <module>
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     from app import app as application
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/home/carwyn/public_html/apps/learningflask/app.py", line 35, in <module>
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     app.run()
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     run_simple(host, port, self, **options)
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 710, in run_simple
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     inner()
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 692, in inner
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     passthrough_errors, ssl_context).serve_forever()
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 436, in serve_forever
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     HTTPServer.serve_forever(self)
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     r, w, e = select.select([self], [], [], poll_interval)
[Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1] error: (4, 'Interrupted system call')

如果您需要更多信息,我很乐意提供,我将非常感谢所有帮助。

编辑:

这是我的learningflask.wsgi文件:

import sys
sys.path.insert(0, '/home/carwyn/public_html/apps/learningflask')
from app import app as application

这是我的可用站点apache文件 - 事物(称为learningflask),它几乎只是遵循教程,但是为教程应用程序设置了:

<VirtualHost *:8081>

        # ---- Configure VirtualHost Defaults ----

    ServerAdmin carwynjohnnelson@aol.com 

        DocumentRoot /home/carwyn/public_html/http/learningflask/

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        <Directory /home/carwyn/public_html/http/learningflask/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        # ---- Configure WSGI Listener(s) ----

        WSGIDaemonProcess learningflask user=www-data group=www-data threads=5
        WSGIScriptAlias /learningflask /home/carwyn/public_html/wsgi/learningflask.wsgi 

        <Directory /home/carwyn/public_html/http/learningflask>
                WSGIProcessGroup learningflask
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>

        # ---- Configure Logging ----

    ErrorLog /home/carwyn/public_html/logs/error.log
    LogLevel warn
    CustomLog /home/carwyn/public_html/logs/access.log combined

</VirtualHost>

第二次编辑:

我希望这可能会有所帮助,我删除了app.run并重新加载了页面,我收到了错误消息。然后我查看了我之前清除的错误日志,什么也没得到。所以我重新启动了apache并查看了我的错误日志,我得到了这个:

[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1] mod_wsgi (pid=5908): Target WSGI script '/home/carwyn/public_html/wsgi/learningflask.wsgi' cannot be loaded as Python module.
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1] mod_wsgi (pid=5908): Exception occurred processing WSGI script '/home/carwyn/public_html/wsgi/learningflask.wsgi'.
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/home/carwyn/public_html/wsgi/learningflask.wsgi", line 3, in <module>
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     from app import app as application
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/home/carwyn/public_html/apps/learningflask/app.py", line 35, in <module>
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     #app.run()
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     run_simple(host, port, self, **options)
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 710, in run_simple
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     inner()
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 692, in inner
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     passthrough_errors, ssl_context).serve_forever()
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 436, in serve_forever
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     HTTPServer.serve_forever(self)
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     r, w, e = select.select([self], [], [], poll_interval)
[Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1] error: (4, 'Interrupted system call')

1 个答案:

答案 0 :(得分:0)

你不应该打电话:

app.run()

这是在Apache进程中启动Flask自己的HTTP服务器。

有一个原因是它在if()语句中检查__name__是否为'__main__'。这是为了确保仅在从命令行Python解释器运行脚本时调用它。你不希望它在Apache下运行。