【问题标题】:Internal Server Error Apache and WSGI (With Flask)内部服务器错误 Apache 和 WSGI(使用 Flask)
【发布时间】:2013-06-29 11:03:41
【问题描述】:

(对不起第一个问题,还没有回答任何问题,我会去的!)

我正在尝试在 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 文件。我读过b

[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')

【问题讨论】:

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


    【解决方案1】:

    你不应该打电话:

    app.run()
    

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

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

    【讨论】:

    • 当我完全删除 app.run 时,我收到以下错误: 内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。所以我不知道在这之外发生了什么。我可以提供更多调试信息吗?
    • 我希望这可能会有所帮助,我删除了 app.run 并重新加载了页面,但我得到了错误。然后我查看了之前清除的错误日志,什么也没得到。所以我重新启动了 apache 并查看了我的错误日志,我收到了一堆错误,这些错误已添加到底部的主帖子中。
    • 您的错误表明您更改了代码,但没有像同样的错误那样正确重新启动 Apache,而是从磁盘上修改的文件中获取代码 sn-ps。尝试完全停止和启动 Apache 以确保重新加载代码。
    • 我停止并启动了服务器,查看了我的错误日志并且没有错误。我加载了页面,但仍然收到“内部服务器错误”错误。然后我重新启动了 apache,我通常如何“sudo service apache2 restart”,我的错误日志中仍然没有错误,但仍然是正常的内部服务器错误。如果您想在 VM 上进行 join.me 会话,我正在徘徊,这可能有助于您诊断它。该死的,也许我会找到一个可以在未来与我交流想法的人,并向我学习,并提高我作为开发人员的能力。 :P
    • 删除任何 .pyc 文件(编译后的字节码文件),它们可能与源代码存在于同一目录中。只要确保你没有删除你的源代码文件。
    猜你喜欢
    • 2015-11-15
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多