supervisor+nginx+gunicorn 部署

安装gunicorn && 启动

(env) apt install gunicorn
gunicorn --workers=4 --bind=0.0.0.0:8002 wsgi:app
上面的命令等价于
gunicorn -w 4 -b 0.0.0.0:8002 wsgi:app
wsgi 为项目根目录下的wsgi.py app是该文件中实例对象
我们后面将使用supervisor来启动gunicorn

nginx 安装 && 配置

apt install nginx

cat /etc/nginx/nginx.conf
......
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
....
我们只需要在nginx/conf.d 目录下创建.conf后缀的配置文件即可

nginx 常用配置指令
events:事件设置
http : http设置 包括 server 和upstream
server:主机设置(域名) ,含location
location:URL设置
upstream:负载均衡设置

在/etc/nginx/conf.d目录下新建 sayhello.conf,内容如下
proxy_pass 为gunicorn 启动监听的服务,即你的应用程序

server{
        listen 8003;
        server_name 39.106.217.14;
        access_log /root/log/sayhello_nginx.log;
        error_log /root/log/sayhello_nginx_error.log;
        location / {
                proxy_pass http://127.0.0.1:8002;
                
        }

}

**重启即可 service nginx restart **

supervisor 配置 && install

  1. apt install supervisor
  2. supervisor 的配置文件放置在 /etc/supervisor 下的 supervisor.conf 中,
  3. 在supervisor.conf中
......
......
[include]
files = /etc/supervisor/conf.d/*.conf -----> 我们只需要在这个目录先建立.conf后缀的配置文件即可

在 /etc/supervisor/conf.d/ 中建立 sayhello.conf 文件 来启动gunicorn

[program:sayhello]
command=/root/.virtualenvs/sayhello/bin/gunicorn -w 4 -b 0.0.0.0:8002 wsgi:app
directory=/root/code/sayhello
user=root
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stdout_logfile=/root/log/sayhello.log

** service supervisor restart** 重启即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章