【问题标题】:What is the correct way to leave gunicorn running?让 gunicorn 运行的正确方法是什么?
【发布时间】:2012-11-19 05:35:17
【问题描述】:

我想做一个 Flask+Nginx+Gunicorn 部署。我已经设置并运行了 Nginx,并且按照文档中的说明运行了 gunicorn:

gunicorn app:app

但是当我注销服务器时,gunicorn 进程会退出吗?确保 Nginx 连接到它时保持运行并在崩溃时重新启动的正确方法是什么?

【问题讨论】:

    标签: python nginx flask gunicorn


    【解决方案1】:

    试试这个:

    nohup gunicorn app:app &
    

    【讨论】:

    • 那你为什么不回答??
    • 因为我同意接受的答案,这就是我使用@tonino.j
    【解决方案2】:

    我会研究Supervisor之类的东西。

    非常有用的教程可以在这里找到https://www.codingforentrepreneurs.com/blog/hello-linux-setup-gunicorn-and-supervisor/

    【讨论】:

    • 这正是 Supervisor 的用途。
    • supervisor 适用于生产级系统,但这不是问题的答案。检查下面的其他答案
    • 现在supervisor仍然是goto解决方案吗?
    【解决方案3】:

    要注意的关键是,当您从命令行启动进程时,它是终端进程的子进程(即bash 的子进程)。当您注销服务器时,您的 bash 进程将被终止 - 它的所有子进程也是如此。

    您将希望使用现有的任何系统来管理 nginx 以及管理 gunicorn(从 init.d 或 Upstart 脚本到专门的应用程序进程监视器,如 Monit、Supervisor、Bluepill、Foreman 等)

    【讨论】:

      【解决方案4】:

      注意肖恩。

      但是您可以像这样动态运行它:

      nohup gunicorn -c config.py </dev/null >/dev/null 2>&1,它将不再依赖于终端连接。如果要保存任何输出,可以将 >/dev/null 替换为 >somelogfile

      但对于生产用途,最好将其集成到您用于管理流程的任何工具中。

      【讨论】:

        【解决方案5】:

        在运行 gunicorn 时使用 --daemon 选项。 示例:

        gunicorn grand56.wsgi:application --name grand56 --workers 3 --user=root --group=root --bind=127.0.0.1:1001 --daemon

        【讨论】:

        • 如果失败,--daemon 标志也会重新启动我的进程吗?还是我仍然需要使用 upstart、systemd、sysv(甚至主管)来完成类似的事情?
        • 启动 gunicorn 时使用 linux "nohup" 是否安全?
        【解决方案6】:

        我尝试了 systemd 选项,它运行良好,下面的链接包含我的完整答案并包含所有步骤,以将您的应用程序作为 gunicorn 服务调用。

        https://askubuntu.com/questions/930589/running-upstart-script-on-17-04/1010398#1010398

        【讨论】:

          【解决方案7】:

          像这样运行hug api。

          --daemon 是将进程保持在后台。

          --access-logfile 保存请求日志

          --bind=: 提供 IP 将允许从其他系统访问(如果不需要代理)。

          gunicorn <pyscirpt_name>:__hug_wsgi__ --name  caassist -w 4 --access-logfile /var/logs/gunicorn/gunicorn_access.log --daemon --bind=<ip>:<port>
          

          【讨论】:

            【解决方案8】:

            使用 --daemon 来绑定 gunicorn 的命令。 例如:

            gunicorn --bind 0.0.0.0:8001 your_project.wsgi --daemon
            

            【讨论】:

              【解决方案9】:

              Supervisor 是一个出色的跨平台流程管理解决方案。它的功能非常丰富,并且(在我看来)比一些普通的 Linux 替代品(upstart、sysv、systemd)需要更多的配置。您绝对应该使用这样的东西来启动、监控和(如果需要)重新启动您的流程。

              无论您最终使用什么进程管理器,您仍然可以很容易地让 gunicorn “运行不正常”(即作为 root 用户)。我认为其他答案遗漏的一些重要细节是,您可能应该让一个(非 root)用户拥有 gunicorn 进程,该进程绑定到该用户和 nginx 组拥有的 unix 套接字并具有权限@987654323 @。对于gunicorn,您可以指定掩码,因此将770 反转为007 并使用-m 标志。这样,只有 gunicorn 和 nginx 可以对套接字进行读/写/执行,不需要端口。您可以使用-u-g 标志指定gunicorn 进程的用户和组,它将使用这些所有者创建套接字。无论您最终使用进程 mgmt 还是 nginx/gunicorn,您都可能希望在启动脚本中使用类似的内容:

              exec gunicorn wsgi:app -u gunicorn -g nginx -m 007 -b gunicorn.sock >> /var/log/$<service_name>.sys.log 2>&1
              

              确保 gunicorn 用户对日志文件具有写入权限。然后,在 nginx 中,您以前拥有 ip/port(即0.0.0.0:5000),将路径放入套接字(即/usr/share/nginx/html/gunicorn.sock)。请注意,我在这里没有使用--daemon 标志,但我使用了exec,这假定一个进程管理器,它将使用exec 作为子进程运行gunicorn。

              您可以找到所有不同的标志here

              【讨论】:

                猜你喜欢
                • 2015-08-08
                • 1970-01-01
                • 1970-01-01
                • 2015-11-07
                • 1970-01-01
                • 1970-01-01
                • 2017-11-18
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多