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** 重啓即可

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