Python 3.x Gunicorn未自动启动

Python 3.x Gunicorn未自动启动,python-3.x,nginx,flask,centos,gunicorn,Python 3.x,Nginx,Flask,Centos,Gunicorn,我已经创建了一个flask应用程序。我正试图在Centos 7中使用WSGI Gunicorn服务器和Nginx反向代理将其部署到生产环境中。下面是我的项目工作目录 projet |--templates |--static |---app.py |---wsgi.py 这里我使用的是虚拟环境“我的应用程序”。当我转到项目工作文件夹并运行应用程序时 gunicorn wsgi:app 我可以在网页上看到结果。但是当我关闭系统时,我得到了502坏网关。这意味着

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

projet
    |--templates
       |--static
   |---app.py
   |---wsgi.py 
这里我使用的是虚拟环境“我的应用程序”。当我转到项目工作文件夹并运行应用程序时
gunicorn wsgi:app
我可以在网页上看到结果。但是当我关闭系统时,我得到了
502坏网关
。这意味着每次我需要手动运行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坏网关
,即使是在创建服务之后。谁能解释一下我是如何做到这一点的

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

根据 您需要一个文件
/etc/systemd/system/gunicorn.socket
,该文件对应于您的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
它应包含以下内容:

[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后台程序重新加载

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

systemctl启用--现在是gunicorn.socket


这将在与套接字的第一次连接启动时启动服务。

您能否显示相应的
/etc/systemd/system/gunicorn.socket
文件?您好,重新加载systemctl时,gunicorn.socket文件完全为空。您好,谢谢。我遵循上述步骤。但是我得到了套接字文件错误。错误是“[crit]10187#0:*21 connect()到unix:/home/centos/Python-3.6.4/my_flask_app/pre_owned_model/evaluation_calculator/gunicorn.sock在连接到上游时失败(13:权限被拒绝)”。您能解释一下我为什么会出现这个错误吗?您的nginx用户似乎无法访问套接字。