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

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

问题描述

我有一个通过端口5000运行的Flask服务器,并没有问题。我可以通过 http://example.com:5000 进行访问。



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

  Traceback(最近一次调用的最后一个):
在< module>文件中的第31行xxxxxx.py
app.run(host =0.0.0.0,port = int(80),debug = True)
文件/usr/local/lib/python2.6/dist-packages/ flask / app.py,第772行,运行
run_simple(host,port,self,** options)
文件/usr/local/lib/python2.6/dist-packages/werkzeug /serve.py,第706行,在run_simple中
test_socket.bind((hostname,port))
绑定
中的文件< string>,第1行socket.error: [Errno 98]已经使用的地址

运行 lsof -i:80 返回

 命令PID用户FD类型设备大小/关节点名称
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 145 73 www-data 3u IPv4 92108840 0t0 TCP *:www(LISTEN)

首先处理?那安全吗?或者有另一种方法来保持Flask在5000端口上运行,但有主网站域名重定向?



谢谢。

apache2 。



如果这是为了开发,我会把它保留在5000端口上。



如果是生产版本:


$ b

不推荐


  • 停止 apache2 第一个;



不建议在文档中说明:

lockquote

您可以在开发过程中使用内置服务器,但是应该为生产应用程序使用完整的部署选项。 (不要在生产中使用内置的开发服务器。)

推荐


  • 代理 HTTP 流量通过 apache2 到Flask。 / li>


通过这种方式, apache2 可以处理所有的静态文件 - 比Flask内置的调试服务器好得多),并作为您的动态内容的反向代理,将这些请求传递给Flask。


$ b

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



编辑1 - 澄清@Djack
$ b


代理HTTP流量通过apache2发送给Flask

当请求到达端口80上的服务器时( HTTP )或端口443( HTTPS )一个web服务器Apache或Nginx处理请求的连接,并找出如何处理它。在我们的例子中,接收到的请求应该被配置为通过WSGI协议上的Flask并由Python代码处理。这是动态的一部分。
$ b


动态内容的反向代理

配置上面的Web服务器有一些优点,


  • SSL终止 - Web服务器将被优化为只处理一些配置来处理HTTPS请求。不要在Python中滚动自己,这可能是非常不安全的。

  • 安全性 - 打开一个端口到互联网需要仔细考虑安全性。 Flask的开发服务器并不是为此而设计的,与为此设计的Web服务器相比,Flask的开发服务器可能有开放的缺陷或安全问题。请注意,配置错误的Web服务器也可能不安全!

  • 静态文件处理 - 内置的Flask web服务器可以处理静态文件,但不建议这样做。 Nginx / Apache在处理图片,CSS,Javascript文件等静态文件方面效率更高,并且只能通过动态请求(通常从数据库读取内容或者内容发生变化的请求),由Python代码处理。

  • + more。这与这个问题的范围接近。如果你想了解更多信息,请在这方面进行一些研究。


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

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

Running lsof -i :80 returns

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)

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?

Thanks.

解决方案

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

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

If it's for production either:

Not Recommended

  • Stop apache2 first;

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.)

Recommended

  • Proxy HTTP traffic through apache2 to 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.

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

Edit 1 - Clarification for @Djack

Proxy HTTP traffic to Flask through apache2

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.

reverse proxy for dynamic content

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

  • SSL Termination - The web server will be optimized to handle HTTPS requests with only a little configuration. Don't "roll your own" in Python which is probably very insecure in comparison.
  • Security - Opening a port to the internet requires careful consideration of security. Flask's development server is not designed for this and could have open bugs or security issues in comparison to a web server designed for this purpose. Note that a badly configured web server can also be insecure!
  • Static File Handling - It is possible for the builtin Flask web server to handle static files however this is not recommended; Nginx/Apache are much more efficient at handling static files like images, CSS, Javascript files and will only pass "dynamic" requests (those where the content is often read from a database or the content changes) to be handled by the Python code.
  • +more. This is bordering on scope for this question. If you want more info do some research into this area.

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

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