栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Django4.0+Daphne+nginx+supervisor 生产环境的部署

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django4.0+Daphne+nginx+supervisor 生产环境的部署

项目中使用了channels 实现websocket,项目使用的是asgi 协议, 通过 pip 安装 Django 正式发布版本
$ python -m pip install Django

查看版本

$ python -m django --version

安装 channels, daphne, 安装 channels 的时候 daphne 就已经连带一起安装了

$ python -m pip install channels
创建项目
$ django-admin startproject mysite

编写例 教程 — 通道 3.0.3 文档 (channels.readthedocs.io)

ALLOWED_HOSTS = [
    '服务器ip'
]

测试 daphne : 

$ daphne -b 0.0.0.0 -p 8001 myproject.asgi:application

配置supervisor

安装 :   $ pip install supervisor

生成supervisor的配置文件:

$ echo_supervisord_conf > /etc/supervisord.conf

编写 supervisord.conf, 添加以下内容:

[program:supervisormydjp]
directory=/mydjp  #项目目录
command=daphne -b 0.0.0.0 -p 8000 mydjp.asgi:application #启动命令
autostart=true
autorestart=true
stdout_logfile=/var/log/python/asgi.log  #日志
redirect_stderr=true

启动supervisor命令:supervisord -c /etc/supervisord.conf 启动或者停止 supervisor 项目服务:

supervisorctl start supervisormydjp
supervisorctl stop supervisormydjp

配置nginx

server {
        listen 8080;
        server_name localhost;

        location / {
            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_pass http://127.0.0.1:8000; #
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/679414.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号