【问题标题】:Configuring Flask and WSGI with Virtualenv使用 Virtualenv 配置 Flask 和 WSGI
【发布时间】:2018-11-09 19:56:41
【问题描述】:

我成功地使用 Python 3.6、Apache 和 mod_wsgi 配置了 Ubuntu 16.04 LTS 机器。这个文件夹有一个安装了 Flask 的 virtualenv。

$ ls -l /var/www/html/odb/
  bin/
  lib/
  include/
  config.wsgi

.wsgi 文件非常简单,Apache conf 也是如此:

$ cat /var/www/html/odb/config.wsgi

  activate_this = '/home/ubuntu/odb/bin/activate_this.py'
  with open(activate_this) as file_:
  exec(file_.read(), dict(__file__=activate_this))

  import sys
  sys.path.insert(0, '/var/www/html/odb/')

  def application(environ,start_response):
      start_response('200 OK',[('Content-type','text/html')])
      return ['Hello world']

$ cat /etc/apache2/sites-available/odb.conf
  <VirtualHost *>
      ServerName example.com

      WSGIDaemonProcess webserver threads=5
      WSGIScriptAlias / /var/www/html/odb/config.wsgi

      <Directory /var/www/html/odb>
          WSGIProcessGroup webserver
          WSGIApplicationGroup %{GLOBAL}
          Order deny,allow
          Allow from all
      </Directory>
  </VirtualHost>

我可以访问虚拟网页。但是当我修改 .wsgi 文件并添加一个 Python Flask 应用程序时,我得到一个 HTTP 500。这是文件:

$ cat /var/www/html/odb/config.wsgi
  activate_this = '/home/ubuntu/odb/bin/activate_this.py'
  with open(activate_this) as file_:
      exec(file_.read(), dict(__file__=activate_this))

  import sys
  sys.path.insert(0, '/var/www/html/odb/')

  from webserver import app as application

$ cat /var/www/html/odb/webserver.py
  from flask import Flask
  app = Flask(__name__)

  @app.route('/')
  def hello_world():
      return 'Hello, World!'

  if __name__ == "__main__":
      app.run(debug=True)

当我通过$ flask run --host=0.0.0.0 运行应用程序时,我可以通过互联网访问它,所以我在 .wsgi 文件中指向 Flask 应用程序的方式似乎有问题。我做错了什么?

日志记录:$ cat /var/log/apache2/*log 在 HTTP 500 之后没有给出任何信息。我​​注意到 /var/www/html/odb/webserver.pyc 出现在 HTTP 500 之后,如果这有帮助的话..?

【问题讨论】:

  • 很久没有使用 Apache 为 WSGI 服务了,但是日志可能会给你一个想法。您能否显示相关日志(如果有记忆,它们将在error.logs
  • 感谢您的快速回复。我现在在上面添加了评论。没有可用的日志。

标签: python flask


【解决方案1】:

相信您也可以通过在 app.run() 命令中启用“use_reloader”来获取这些日志,

以下来自http://flask.pocoo.org/docs/0.12/api/

'如果你没有设置记住 Flask 将使用通用错误页面抑制任何服务器错误,除非它处于 >debug 模式。因此,要仅启用交互式调试器而不重新加载代码,您必须使用 debug=True 和 use_reloader=False 调用 run()。 >在不处于调试模式的情况下将 use_debugger 设置为 True 不会捕获任何 >异常,因为不会捕获任何异常。'

(抱歉不能评论,积分不够)

【讨论】: