Skip to main content
 首页 » 编程设计

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

2025年05月04日90bhlsheji

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

我正在尝试在 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') 

请您参考如下方法:

你不应该打电话:

app.run() 

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

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