Vue+Django 前后端分离项目部署(Nginx+Gunicorn+Supervisor)

image.png

image.png

nginx: 反向代理服务器,处理静态资源,负载均衡等。
gunicorn: Python WSGI HTTP Server for UNIX。
supervisor: 基于linux操作系统的一款服务器管理工具,用以监控服务器的运行,发现问题能立即自动预警及自动重启等功能。

supervisor关键理解:

配置文件的目录结构
supervisor
├── conf.d
│ └── echo_time.conf -- 业务配置文件
└── supervisord.conf -- 主配置文件,一般不需要改动
使用 apt-get 安装后,supervisor 的主配置文件在 /etc/supervisor/supervisord.conf
子进程配置文件在 /etc/supervisor/conf.d/*.conf

supervisord 和 supervisorctl的关系
supervisord 是主进程。supervisorctl 是客户端程序,用于向supervisord服务发起命令。
常用命令

使用默认的配置文件 /etc/supervisord.conf

supervisord

明确指定配置文件

supervisord -c /etc/supervisor/supervisord.conf

使用 user 用户启动 supervisord

supervisord -u user
其他参考# Linux进程管理工具Supervisor

/etc/supervisor/supervisord.conf配置,注意:文件名必须是supervisord.conf,不然后面启动时会有问题,因为supervisord程序启动时,会从/etc/和~/找名称为supervisord.conf的文件,如果找不到,则会启动不起来或出错。

[unix_http_server]
file=/data/credit_core/qa-portal/qa-portal-be/supervisor.sock   ;UNIX socket 文件,supervisorctl 会使用
;chmod=0700                 ;socket文件的mode,默认是0700
;chown=nobody:nogroup       ;socket文件的owner,格式:uid:gid

[inet_http_server]         ;HTTP服务器,提供web管理界面
port=127.0.0.1:9001        ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
;username=user              ;登录管理后台的用户名
;password=123               ;登录管理后台的密码

[supervisord]
logfile=/data/credit_core/qa-portal/qa-portal-be/supervisord.log ;日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB        ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
logfile_backups=10           ;日志文件保留备份数量默认10,设为0表示不备份
loglevel=info                ;日志级别,默认info,其它: debug,warn,trace
pidfile=/data/credit_core/qa-portal/qa-portal-be/supervisord.pid ;pid 文件
nodaemon=false               ;是否在前台启动,默认是false,即以 daemon 的方式启动
minfds=1024                  ;可以打开的文件描述符的最小值,默认 1024
minprocs=200                 ;可以打开的进程数的最小值,默认 200

[supervisorctl]
serverurl=unix:///data/credit_core/qa-portal/qa-portal-be/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord

;包含其它配置文件
[include]
files = supervisord.d/*.ini
# 指定配置文件目录,个人习惯置于 /etc/supervisor/conf.d 目录下
files = /etc/supervisor/conf.d/*.conf

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

/etc/supervisor/conf.d/qap.conf配置

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

[program:qap]
directory=/data/credit_core/qa-portal/qa-portal-be
command=/root/.pyenv/shims/gunicorn --config /data/credit_core/qa-portal/qa-portal-be/deploy/gunicorn.conf.py
; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
autostart=true                 ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
autorestart=true               ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
user=root                      ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
stdout_logfile=/data/credit_core/qa-portal/qa-portal-be/logs/gunicorn_stdout.log        ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=50MB   ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=10       ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB    ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false    ; emit events on stdout writes (default false)
stderr_logfile=/data/credit_core/qa-portal/qa-portal-be/logs/gunicorn_stderr.log        ; stderr log path, NONE for none; default AUTO
stderr_logfile_maxbytes=50MB    ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=10      ; # of stderr logfile backups (default 10)
stderr_capture_maxbytes=1MB    ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=PYPATH="/home/user_00/.pyenv/versions/airpay-qa-portal-be-3.8.1"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

gunicorn.conf.py配置

# 参考文档 https://docs.gunicorn.org/en/stable/settings.html

wsgi_app = 'config.wsgi:application'
accesslog = '/data/credit_core/qa-portal/qa-portal-be/gunicorn.acess.log'
errorlog = '/data/credit_core/qa-portal/qa-portal-be/gunicorn.error.log'
loglevel = 'info'
capture_output = True
pidfile = 'gunicorn.pid'
bind = '0.0.0.0:5088'
workers = 2
worker_class = 'gevent'
worker_connections = 100
timeout = 120
reload = True

/etc/nginx/conf.d/core-tool-master.conf配置

server {
    listen 8001;
    server_name ****; #这里填ip或者域名都可以
    access_log logs/qap/access.log;
    error_log logs/qap/error.log;

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    try_files $uri $uri/index.html $uri.html =404;

    # backend
    location ~ ^/(api|admin) {
        proxy_pass http://127.0.0.1:5088;
    }
    # backend static files
    location ~ /static {
        root /data/credit_core/qa-portal/qa-portal-be/;
        access_log off;
    }

    # frontend
    location ~ ^(/|/assets/) {
        root /data/credit_core/qa-portal/qa-portal-fe/frontend;
        access_log off;
    }

}

将vue前端编译好的文件,放置在Nginx中配置的static文件位置
这样输入ip:8001就能打开你的网站了。

这样就可以方便的管理我们的 Djnago 项目了,下面是实际工作开发中用到的部署项目命令,一键部署重启。
git pull --rebase && ./manage.py collectstatic -c --noinput && supervisorctl restart demo

你可能感兴趣的:(Vue+Django 前后端分离项目部署(Nginx+Gunicorn+Supervisor))