内部服务器错误Apache和WSGI(带有烧瓶) - python

(很抱歉,第一个问题还没有回答,我会解决的!)

我试图在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 [email protected] 

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

参考方案

您不应致电:

app.run()

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

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

单行的'if'/'for'语句是否使用Python样式好? - python

我经常在这里看到某人的代码,看起来像是“单线”,这是一条单行语句,以传统的“if”语句或“for”循环的标准方式执行。我在Google周围搜索,无法真正找到可以执行的搜索类型?任何人都可以提出建议并最好举一些例子吗?例如,我可以一行执行此操作吗?example = "example" if "exam" in exam…

为什么使用'=='或'is'比较字符串有时会产生不同的结果? - python

我有一个Python程序,其中将两个变量设置为'public'值。在条件表达式中,我有比较var1 is var2失败,但如果将其更改为var1 == var2,它将返回True。现在,如果我打开Python解释器并进行相同的“是”比较,则此操作成功。>>> s1 = 'public' >>…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

python JSON对象必须是str,bytes或bytearray,而不是'dict - python

在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…