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

Python 内部服务器错误Apache和WSGI(带Flask),python,flask,mod-wsgi,wsgi,internal-server-error,Python,Flask,Mod Wsgi,Wsgi,Internal Server Error,(对不起,第一个问题还没有回答,我会开始的!) 我试图在Ubuntu12.04虚拟机中使用Apache2和mod_wsgi设置Flask,主要是为了在将来部署Flask应用程序时知道如何操作。让你们知道,我对Python和Flask还不熟悉,但我熟悉PHP,因此也熟悉一般编程实践 我正在安装烧瓶。我已成功设置了本教程中定义的测试应用程序,但是,当我尝试使用本教程设置现有应用程序时,出现以下错误: Internal Server Error The server encountered a

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

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

我正在安装烧瓶。我已成功设置了本教程中定义的测试应用程序,但是,当我尝试使用本教程设置现有应用程序时,出现以下错误:

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)
下面是我的可用站点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>

#----配置VirtualHost默认值----
服务器管理员carwynjohnnelson@aol.com 
DocumentRoot/home/carwyn/public_html/http/learningflask/
选项如下符号链接
不允许超限
选项索引跟随符号链接多视图
不允许超限
命令允许,拒绝
通融
#----配置WSGI侦听器----
WSGIDaemonProcess learningflask用户=www数据组=www数据线程=5
WSGIScriptAlias/learningflask/home/carwyn/public_html/wsgi/learningflask.wsgi
WSGIProcessGroup learningflask
WSGIApplicationGroup%{GLOBAL}
命令拒绝,允许
通融
#----配置日志记录----
ErrorLog/home/carwyn/public_html/logs/error.log
日志级别警告
CustomLog/home/carwyn/public_html/logs/access.log组合
第二次编辑:


我希望这可能会有所帮助,我删除了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')
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]mod_wsgi(pid=5908):无法将目标wsgi脚本“/home/carwyn/public_html/wsgi/learningflask.wsgi”作为Python模块加载。
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]mod_wsgi(pid=5908):处理wsgi脚本'/home/carwyn/public_html/wsgi/learningflask.wsgi'时发生异常。
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]回溯(最近一次通话):
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]文件“/home/carwyn/public_html/wsgi/learningflask.wsgi”,第3行,在
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]来自应用程序导入应用程序作为应用程序
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]文件“/home/carwyn/public_html/apps/learningflask/app.py”,第35行,在
[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]文件“/usr/local/lib/python2.7/dist packages/flask/app.py”,第772行,运行中
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]运行简单(主机、端口、自身,**选项)
[2013年6月29日星期六21:33:19][error][client 127.0.0.1]文件“/usr/local/lib/python2.7/dist packages/werkzeug/service.py”,第710行,运行中
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]内部()
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]文件“/usr/local/lib/python2.7/dist packages/werkzeug/service.py”,第692行,在内部
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]传递错误,ssl上下文)。永远服务()
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]文件“/usr/local/lib/python2.7/dist packages/werkzeug/serving.py”,第436行,永远服务
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]HTTPServer.永远为您服务(self)
[Sat Jun 29 21:33:19 2013][error][client 127.0.0.1]文件“/usr/lib/python2.7/SocketServer.py”,第225行,永远服务
[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]错误:(4,“系统调用中断”)

您不应该呼叫:

app.run()
这是在Apache进程中启动Flask自己的HTTP服务器


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

当我删除app.run complete时,我收到以下错误:内部服务器错误服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序中存在错误。所以我不知道在这之外发生了什么。我可以提供更多的调试信息吗?我希望这可能会有所帮助,我删除了app.run并重新加载了页面,我发现了错误。然后,我查看了以前清除的错误日志,什么也没有得到。因此,我重新启动了apache并查看了错误日志,我在底部的主要帖子中添加了一系列错误。您的错误表明您更改了代码,但没有正确地重新启动apache,因为这是相同的错误,但从磁盘上修改的文件中获取了代码片段。尝试执行Apache的完全停止和启动,以确保重新加载代码。我停止和启动了服务器,查看了错误日志,没有错误。我加载了页面,但仍然出现“内部服务器错误”错误。然后,我按照通常的方式重新启动了apache“sudo service apache2 restart”,我的错误日志中仍然没有错误,但是
[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()