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

2024-04-26 03:07:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我试图在Ubuntu12.04虚拟机中用Apache2和mod_wsgi设置Flask,主要是为了将来在部署Flask应用程序时知道如何实现。为了让你们知道,我对Python和Flask还不熟悉,但我对PHP很熟悉,因此也熟悉一般的编程实践。

我正跟随this tutorial来设置烧瓶。我已经成功地设置了本教程中定义的测试应用程序,但是当我尝试使用本教程设置现有应用程序时,会出现以下错误:

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代码,希望它能正常工作,但没有成功,我遇到了同样的错误,而且我永远无法得到错误日志,所以我切换回“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),它基本上遵循了教程1,但是是为教程应用程序设置的:

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

Tags: inpyclientappwsgihomehtmlline
1条回答
网友
1楼 · 发布于 2024-04-26 03:07:10

你不应该打电话给:

app.run()

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

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

相关问题 更多 >