python-3.x - Gunicorn 没有自动启动

标签 python-3.x nginx flask centos gunicorn

我创建了一个 flask 应用程序。我正在尝试在 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即使在创建服务之后。谁能解释我如何做到这一点。

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

最佳答案

根据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
这将在与您的套接字的第一个连接开始时启动服务。

关于python-3.x - Gunicorn 没有自动启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60167661/

相关文章:

Python 网络服务器不工作

sql - 使用对列表有效地选择 SQL Server 中的行?

python - 从 python 中的 sqlite 表中读取原始数据

flask - 如何向 Jinja2 的渲染标签添加属性

python - python中的二叉搜索树递归实现

url - 如果没有尾部斜杠,Nginx 会导致 301 重定向

nginx - yii2 中的速率限制与使用 nginx 进行速率限制

nginx - 如何使用 nginx 将多个 URL 映射到单个文件?

python - Flask 服务器运行 Bokeh 应用程序 : how to access plots from a script generated by server_document() function

javascript - 在 Ajax 中使用数组