如何让 Flask 在端口 80 上运行? [英] How do I get Flask to run on port 80?

查看:32
本文介绍了如何让 Flask 在端口 80 上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过端口 5000 运行的 Flask 服务器,它很好.我可以在 http://example.com:5000

I have a Flask server running through port 5000, and it's fine. I can access it at http://example.com:5000

但是是否可以简单地在 http://example.com 上访问它?我假设这意味着我必须将端口从 5000 更改为 80.但是当我在 Flask 上尝试时,我在运行它时收到此错误消息.

But is it possible to simply access it at http://example.com? I'm assuming that means I have to change the port from 5000 to 80. But when I try that on Flask, I get this error message when I run it.

Traceback (most recent call last):
  File "xxxxxx.py", line 31, in <module>
app.run(host="0.0.0.0", port=int("80"), debug=True)
   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.6/dist-packages/werkzeug/serving.py", line 706, in run_simple
    test_socket.bind((hostname, port))
  File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use

运行 lsof -i :80 返回

COMMAND   PID     USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
apache2   467     root    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2  4413 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14346 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14570 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14571 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14573 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)

我需要先杀死这些进程吗?那安全吗?或者是否有另一种方法可以让 Flask 在端口 5000 上运行但以某种方式重定向主网站域?

Do I need to kill these processes first? Is that safe? Or is there another way to keep Flask running on port 5000 but have the main website domain redirect somehow?

推荐答案

因此它抛出该错误消息,因为您在端口 80 上运行 apache2.

So it's throwing up that error message because you have apache2 running on port 80.

如果这是用于开发,我会将其保留在端口 5000 上.

If this is for development, I would just leave it as it is on port 5000.

如果是用于生产:

不推荐

  • 先停止apache2

不推荐,因为它在文档中声明:

Not recommended as it states in the documentation:

您可以在开发期间使用内置服务器,但您应该为生产应用程序使用完整的部署选项.(请勿在生产中使用内置开发服务器.)

You can use the builtin server during development, but you should use a full deployment option for production applications. (Do not use the builtin development server in production.)

推荐

  • 代理 HTTP 流量通过 apache2 到 Flask.
  • Proxy HTTP traffic through apache2 to Flask.

这样,apache2 可以处理所有静态文件(它非常擅长 - 比 Flask 内置的调试服务器要好得多)并充当动态内容的反向代理,传递那些对 Flask 的请求.

This way, apache2 can handle all your static files (which it's very good at - much better than the debug server built into Flask) and act as a reverse proxy for your dynamic content, passing those requests to Flask.

这里是关于设置 Flask 的官方文档的链接使用 Apache + mod_wsgi.

Here's a link to the official documentation about setting up Flask with Apache + mod_wsgi.

编辑 1 - 澄清@Djack

通过apache2代理HTTP流量到Flask

Proxy HTTP traffic to Flask through apache2

当请求通过端口 80 (HTTP) 或端口 443 (HTTPS) 到达服务器时,Apache 或 Nginx 之类的 Web 服务器会处理请求的连接并弄清楚如何处理它.在我们的例子中,接收到的请求应该被配置为通过 WSGI 协议传递到 Flask 并由 Python 代码处理.这是动态"部分.

When a request comes to the server on port 80 (HTTP) or port 443 (HTTPS) a web server like Apache or Nginx handles the connection of the request and works out what to do with it. In our case a request received should be configured to be passed through to Flask on the WSGI protocol and handled by the Python code. This is the "dynamic" part.

动态内容的反向代理

像上面那样配置您的网络服务器有几个优点;

There are a few advantages to configuring your web server like the above;

  • SSL 终止 - 网络服务器将经过优化,只需少量配置即可处理 HTTPS 请求.不要在 Python 中自己动手",相比之下,这可能非常不安全.
  • 安全性 - 打开 Internet 端口需要仔细考虑安全性.Flask 的开发服务器不是为此目的而设计的,与为此目的而设计的 Web 服务器相比,它可能存在未解决的错误或安全问题.请注意,配置不当的 Web 服务器也可能不安全!
  • 静态文件处理 - 内置的 Flask Web 服务器可以处理静态文件,但不推荐这样做;Nginx/Apache 在处理静态文件(如图像、CSS、Javascript 文件)方面效率更高,并且只会传递由 Python 代码处理的动态"请求(那些内容经常从数据库读取或内容更改的请求).
  • +更多.这接近于这个问题的范围.如果您想了解更多信息,请对该领域进行一些研究.

这篇关于如何让 Flask 在端口 80 上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆