Python 让 Gunicorn 在 80 端口上运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16225872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:03:25  来源:igfitidea点击:

Getting Gunicorn to run on port 80

pythonhttpgunicorn

提问by disappearedng

I built an API with flask. My app does not have any static assets hence there's no reason for me to use nginx.

我用烧瓶构建了一个 API。我的应用程序没有任何静态资产,因此我没有理由使用 nginx。

I wish to run gunicorn on port 80.

我希望在端口 80 上运行 gunicorn。

I have a "deploy script":

我有一个“部署脚本”:

mkdir .log 2> /dev/null
DEBUG=0 gunicorn -b 0.0.0.0:80 backend:app --access-logfile .log/access.log --error-logfile .log/general.log

I wish to run gunicorn on port 80 with authbind. I followed this guidehere.

我希望在端口 80 上运行 gunicorn authbind。我在这里遵循了本指南

Note that I am able to run authbind python -m SimpleHTTPServer 80

请注意,我可以运行 authbind python -m SimpleHTTPServer 80

When I try to run authbind ./deployment.run 80,

当我尝试跑步时authbind ./deployment.run 80

I am seeing the following error:

我看到以下错误:

2013-04-25 15:32:55 [24006] [ERROR] Can't connect to ('0.0.0.0', 80)
2013-04-25 15:33:08 [24018] [INFO] Starting gunicorn 0.17.4
2013-04-25 15:33:08 [24018] [ERROR] Retrying in 1 second.
2013-04-25 15:33:09 [24018] [ERROR] Retrying in 1 second.
2013-04-25 15:33:10 [24018] [ERROR] Retrying in 1 second.
2013-04-25 15:33:11 [24018] [ERROR] Retrying in 1 second.
2013-04-25 15:33:12 [24018] [ERROR] Retrying in 1 second.

Any ideas why I am unable to bind gunicorn to port 80?

为什么我无法将 gunicorn 绑定到端口 80 的任何想法?

Any recommendations?

有什么建议吗?

采纳答案by rmunn

Try putting authbind inside your deployment script, e.g.:

尝试将 authbind 放在您的部署脚本中,例如:

mkdir .log 2> /dev/null
DEBUG=0 authbind gunicorn -b 0.0.0.0:80 backend:app --access-logfile .log/access.log --error-logfile .log/general.log

Then just run ./deployment.run 80.

然后就跑./deployment.run 80

(Also, your script doesn't seem to be using any parameters; perhaps replace 80in your script with $1?)

(此外,您的脚本似乎没有使用任何参数;也许80在您的脚本中替换为$1?)

回答by Uku Loskit

If you are on a unix-like environment, ports < 1024 (like 80) will require superuser privileges.

如果您在类 Unix 环境中,端口 < 1024(如 80)将需要超级用户权限。

回答by bhatman

You can use authbindto achieve this. Install authbind

您可以使用它authbind来实现这一点。安装验证绑定

sudo apt-get install authbind

Then use auth bind to modify port 80 to make sure that port 80 can be used by non-superusers (aka without superuser privileges). Here are the three commands you can use to achieve this.

然后使用 auth bind 修改端口 80 以确保端口 80 可以被非超级用户(也就是没有超级用户权限)使用。以下是您可以用来实现此目的的三个命令。

sudo touch /etc/authbind/byport/80
sudo chmod 500 /etc/authbind/byport/80
sudo chown USER /etc/authbind/byport/80

USER- can be any user on your system like bhatman or ubuntu or ec2-user.

用户- 可以是系统上的任何用户,如 bhatman 或 ubuntu 或 ec2-user。

NOTE: just change 80 to any desired port and it will work for any port. Use this responsibly my friend. :)

注意:只需将 80 更改为任何所需的端口,它将适用于任何端口。我的朋友,请负责任地使用它。:)

Now your gunicorn command will look something like this:

现在您的 gunicorn 命令将如下所示:

authbind gunicorn -c gunicorn.conf wsgi:app

Just append authbindbefore your gunicorn command

只需authbind在您的 gunicorn 命令之前附加

BONUS: If you are using some command before the gunicorn like newrelic etc, then you need to add --deep flag after authbind

奖励:如果你在 gunicorn 之前使用了一些命令,比如 newrelic 等,那么你需要在 authbind 之后添加 --deep 标志

authbind --deep newrelic-admin run-program gunicorn -c gunicorn.conf wsgi:app

for more info about authbind checkout its ubuntu manpage: here

有关 authbind 的更多信息,请查看其 ubuntu 联机帮助页:此处

But before running these commands blindly I would suggest you to read the following points.

但在盲目运行这些命令之前,我建议您阅读以下几点。

  1. Gunicorn is an appplication server and is not meant to serve the request directly there it is better to use it behind a web server like Nginx or AWS ALB etc.
  2. Ports less than 1024 are privileged ports and should not be opened or used just like that, you should have a strong reason to run applications on such ports.
  1. Gunicorn 是一个应用程序服务器,并不意味着直接在那里处理请求,最好在 Nginx 或 AWS ALB 等 Web 服务器后面使用它。
  2. 小于 1024 的端口是特权端口,不应像那样打开或使用,您应该有充分的理由在此类端口上运行应用程序。

NGINX is not a necessity for gunicorn, you can use any web server. Your architecture should always look something like this.

NGINX 不是 gunicorn 的必需品,您可以使用任何 Web 服务器。您的架构应该始终看起来像这样。

WEB SERVER (NGINX, AWS ALB etc) -> APPLICATION SERVER (Gunicorn, uWsgi etc) -> Application (Flask, Django etc)

Hope this helps you.

希望这对你有帮助。