说明:

项目中需要websocket通信,使用了channels模块,一般Django部署是使用uwsgi,但是对websocket支持不太好,官方推荐使用daphne。主要记录下channels部署的相关步骤和配置!

参考文档https://channels.readthedocs.io/en/latest/deploying.html

一、源码修改

1.1、将settings.py中的DEBUGTrue改为False

1.2、收集静态文件,在settings.py添加STATIC_ROOT = os.path.join(BASE_DIR, "build/")

然后在 manage的终端中运行collectstatic可以把静态文件全部复制到build目录下。

二、运行Daphne

Daphne是一个用于HTTP、HTTP2和WebSocket协议的ASGI服务器。

# Django2.0.13安装daphne2.2.5版本
pip3 install daphne==2.25
# 可以先cd到项目里面运行下,没报错则继续(静态文件会404,无视它)
cd 项目的路径
cd /root/DeepLearningtaskscopy
# pytho环境路径 -p 监听的端口号 -b 监听的ip asgi的相对位置
/root/py3-env/bin/daphne -p 8002 -b 0.0.0.0 -t 650 DeepLearningtaskscopy.asgi:application

三、安装Nginx

Nginx是一个高性能的Web服务器/反向代理服务器。

3.1、打开/etc/yum.repos.d/nginx.repo输入:

[nginx]
name=nginx.repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enable=1

3.2、安装:

yum clean all
yum install -y nginx
systemctl enable nginx
systemctl start nginx

四、配置Nginx

4.1、打开/etc/nginx/nginx.conf修改如下:

主要是把第一行用户nginx修改为root,其他修改为优化选项。

include指定包含其他扩展配置文件,从而简化nginx主配置文件,实现多个站点功能。


user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  6500;
    types_hash_max_size 2048;
    client_max_body_size 10240m;
    proxy_connect_timeout       6500;
    proxy_send_timeout          6500;
    proxy_read_timeout          6500;
    send_timeout                6500;
    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

4.2、在/etc/nginx/conf.d目录下新建deeplearning_nginx.conf配置文件:

location /static/ 是匹配static开头的静态文件直接在alias目录下查找。

proxy_to_app是代理Daphne 的配置。

upstream channels-backend {
    # http和websocket请求转发配置
    server localhost:8001;
}
# configuration of the server
server {
    # the port your site will be served on
    listen      8008;
    # the domain name it will serve for
    server_name 192.168.10.180; # substitute your machine's IP address or FQDN
    charset     utf-8;


    location /favicon.ico {
        alias /root/DeepLearningtaskscopy/static/favicon.ico;

    }

    location /static/ {
        alias /root/DeepLearningtaskscopy/static/;

    }


    location / {
         try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_pass http://channels-backend;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";


        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;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_read_timeout 6500;
        proxy_send_timeout 6500;
        keepalive_timeout  6500;
    }
}

4.3、 重新加载 nginx以应用更改:sudo service nginx reload

五、安装Supervisor

Supervisor是一套通用的进程管理程序,能监控进程状态,异常退出时能自动重启。

yum install epel-release
yum install -y supervisor
systemctl enable supervisord
systemctl start supervisord

六、配置Supervisor

6.1、Supervisor的默认配置文件是/etc/supervisord.conf

/etc/supervisord.d目录下新建deeplearning_daphne.ini配置文件,管理daphne 进程 :

[program:daphne]
# 执行用户
user = root
# 在该目录下执行下面command命令
directory = /root/DeepLearningtaskscopy
# 执行的命令
command = /root/py3-env/bin/daphne -p 8002 -t 650 DeepLearningtaskscopy.asgi:application
# 日志文件配置,当指定目录不存在时无法正常启动,需要手动创建目录。
loglevel = info
stdout_logfile = /root/deeplearninglogs/daphne.log
stderr_logfile = /root/deeplearninglogs/daphne_error.log
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 3
# 给每个进程命名,便于管理
process_name = daphne_worker%(process_num)s
# 启动的进程数
numprocs_start = 1
numprocs = 1
max-requests = 5000
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = true

6.2、重新加载 Supervisor以应用更改:

sudo supervisorctl reread
sudo supervisorctl update

6.3、访问IP:端口,成功!

附:supervisor相关命令

supervisorctl restart <application name> ;重启指定应用
supervisorctl stop <application name> ;停止指定应用
supervisorctl start <application name> ;启动指定应用
supervisorctl restart all ;重启所有应用
supervisorctl stop all ;停止所有应用
supervisorctl start all ;启动所有应用
supervisorctl status ;查看当前应用状态

Last modification:March 4th, 2020 at 11:07 am