【问题标题】:Gunicorn not starting automaticallyGunicorn 没有自动启动
【发布时间】:2020-05-26 18:57:59
【问题描述】:

我创建了一个烧瓶应用程序。我正在尝试在 Centos 7 中使用 WSGI Gunicorn 服务器和 Nginx 反向代理将其部署到生产环境中。以下是我的项目工作目录。

projet
    |--templates
       |--static
   |---app.py
   |---wsgi.py 

这里我使用的是虚拟环境“my_app”。当我要去项目工作文件夹并运行应用程序时 gunicorn wsgi:app 我可以在网页中看到结果。但是当我关闭系统时,我得到了502 Bad GateWay。这意味着每次我需要手动运行 gunicorn 命令才能在网页中看到结果。但我想自动启动它。为此,我在 systemd 的帮助下创建了一个服务文件。以下是 Nginx 和 Gunicorn 的配置文件。

在 /etc/nginx/sites-enabled/flaskapp.conf 中

server{
     listen 80;
     server_name xxxx.xxx.xx.xx;
     access_log  /var/log/nginx/error.log;
     uwsgi_read_timeout 120;

     location = /favicon.ico { access_log off; log_not_found off; }


     location / {
        proxy_pass http://unix://home/centos/Python-3.6.4/my_flask_app/pre_owned_model/evaluation_calculator/gunicorn.sock;
        #proxy_pass http://127.0.0.1:8000;
     }
}

在 /etc/systemd/system/gunicorn.service 中

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
Type=simple
User=centos
Group=centos
RuntimeDirectory=gunicorn
WorkingDirectory=/home/centos/Python-3.6.4/my_flask_app/pre_owned_model/evaluation_calculator
ExecStart=/home/centos/Python-3.6.4/my_flask_app/my_app/bin/gunicorn  --workers 3 --bind unix:/home/centos/Python-3.6.4/my_flask_app/pre_owned_model/evaluation_calculator/gunicorn.sock -m 007 wsgi:app
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
TimeoutStopSec=3
PrivateTmp=true
Restart=always

[Install]
WantedBy=multi-user.target

但是,我没有得到预期的结果。每次我得到502 Bad Gateway,即使在创建服务之后也是如此。谁能解释我如何实现这一目标。

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 你能出示你对应的/etc/systemd/system/gunicorn.socket文件吗?
  • 嗨,当我重新加载 systemctl 时,gunicorn.socket 文件完全为空。

标签: python-3.x nginx flask centos gunicorn


【解决方案1】:

根据https://docs.gunicorn.org/en/stable/deploy.html 您需要一个文件 /etc/systemd/system/gunicorn.socket 对应于您的 gunicorn.service

它应该包含以下内容:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/home/centos/Python-3.6.4/my_flask_app/pre_owned_model/evaluation_calculator/gunicorn.sock
# Our service won't need permissions for the socket, since it
# inherits the file descriptor by socket activation
# only the nginx daemon will need access to the socket
User=centos
# Optionally restrict the socket permissions even more.
# Mode=600

[Install]
WantedBy=sockets.target

创建文件后,您很可能需要重新加载 systemd:

systemctl daemon-reload

要启用套接字,您还必须运行命令

systemctl enable --now gunicorn.socket

这将在与您的套接字的第一个连接开始时启动服务。

【讨论】:

  • 嗨,谢谢。我按照上述步骤操作。但是我得到了套接字文件错误。错误是“ [crit] 10187#0: *21 connect() to unix:/home/centos/Python-3.6.4/my_flask_app/pre_owned_model/evaluation_calculator/gunicorn.sock failed (13: Permission denied) 同时连接到上游”。你能解释一下为什么我得到这个错误。
  • 好像你的nginx用户无法访问socket。
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 2017-10-07
  • 1970-01-01
  • 2019-01-07
  • 2016-11-08
  • 2021-08-26
  • 2020-10-10
  • 1970-01-01
相关资源
最近更新 更多