【问题标题】:Possible to run a Flask app with mod_wsgi on a specific port?可以在特定端口上运行带有 mod_wsgi 的 Flask 应用程序吗?
【发布时间】:2017-06-17 10:29:28
【问题描述】:

我正在尝试在网络机器的端口上运行一个非常简单的烧瓶应用程序。我当前的服务器代码如下所示:

from flask import Flask
import json
app = Flask(__name__)

@app.route('/my_route')
def my_route():
    return json.dumps({'data': 'this is an example'})

假设我网络上机器的 IP 是123.123.123.123,我想在端口5000 上运行我的应用程序。我希望能够在浏览器中导航或向123.123.123.123:5000/my_route 发出简单的HTTP 请求并获得JSON 响应。

我尝试关注mod_wsgi Flask documentation 未成功。在使用这样的指令编辑httpd.conf 后,我收到了500 Internal Server 错误显示:

Listen 5000
NameVirtualHost *:5000
<VirtualHost *:5000>
        ServerName gcr_app

        WSGIDaemonProcess gcr_app user=apache threads=5
        WSGIScriptAlias / /var/www/gcr_app/gcr_app.wsgi

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

并重新启动httpd 服务。

我在这里做错了什么?其他信息:

  • RHEL 6.7
  • Python 3.4.4
  • 烧瓶 0.12

Apache 版本:

$ /usr/sbin/httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Feb  4 2016 08:22:15

如果我能提供其他有用的信息,请告诉我。

错误日志信息

[Mon Jan 30 17:55:22 2017] [error] [client x.x.x.x] (13)Permission denied: mod_wsgi (pid=2118): Unable to connect to WSGI daemon process 'gcr_app' on '/etc/httpd/logs/wsgi.2109.0.1.sock' after multiple attempts.
[Mon Jan 30 17:55:26 2017] [error] [client x.x.x.x] (13)Permission denied: mod_wsgi (pid=2114): Unable to connect to WSGI daemon process 'gcr_app' on '/etc/httpd/logs/wsgi.2109.0.1.sock' after multiple attempts.
[Mon Jan 30 17:55:27 2017] [error] [client x.x.x.x] (13)Permission denied: mod_wsgi (pid=2115): Unable to connect to WSGI daemon process 'gcr_app' on '/etc/httpd/logs/wsgi.2109.0.1.sock' after multiple attempts.
[Mon Jan 30 17:55:46 2017] [error] [client x.x.x.x] (13)Permission denied: mod_wsgi (pid=2113): Unable to connect to WSGI daemon process 'gcr_app' on '/etc/httpd/logs/wsgi.2109.0.1.sock' after multiple attempts.
[Mon Jan 30 17:57:48 2017] [error] [client x.x.x.x] File does not exist: /var/www/html/gcr_distance
[Mon Jan 30 17:57:48 2017] [error] [client x.x.x.x] File does not exist: /var/www/html/favicon.ico, referer: http://x.x.x.x/my_route
[Mon Jan 30 18:43:49 2017] [error] [client x.x.x.x] (13)Permission denied: mod_wsgi (pid=2116): Unable to connect to WSGI daemon process 'gcr_app' on '/etc/httpd/logs/wsgi.2109.0.1.sock' after multiple attempts.

谢谢

【问题讨论】:

  • 您应该将您看到的错误发布在/var/log/httpd/*
  • 看看它们,看看有没有什么重要的东西
  • 您运行的是 Apache 2.2 还是 Apache 2.4?您的配置适用于 2.2,这与 RHEL 6.7 一致,但如果您已升级到 Apache 2.4,则可能会出现不同的配置格式导致权限错误。
  • apache 是不是错误的用户?我只是尝试与我自己的用户一起做,但也没有用。
  • Apache 主要以用户www_data 运行

标签: python apache flask


【解决方案1】:

感谢所有对此问题发表评论的人。事实证明,我用来运行Flask 的Python 版本和mod_wsgi 的Python 版本存在问题。由于我使用的是 RHEL 6.7,mod_wsgi 已安装 Python 2.7,而我使用 Python 3.4 编写了我的应用程序。这意味着 mod_wsgi 在导入我正在使用的模块时遇到问题。

除此之外,我还没有正确导入我的Flask 应用程序。我的.wsgi 文件中有这个:

import my_app as application

代替:

from my_app import app as application

在将一些 Python 代码更改为与 2.7 兼容并修复我的 .wsgi 文件后,我能够在我想要的端口上运行 Flask 应用程序。

如果使用我的 Python 3.4 版本的 pip 安装 mod_wsgi 适用于使用 Python 3.4 运行 Flask 应用程序,我将更新此答案。

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 1970-01-01
    • 2013-04-04
    • 2019-05-27
    • 2019-09-20
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多