本文主要是介绍django+websocket+daphne+supervisor+nginx,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在linux系统上部署django项目,一般使用uwsgi来托管,nginx做反向代理,但是uwsgi无法托管websocket channel。
因此面对websocket的django项目,需要使用daphne来运行。
daphne安装:
pip install daphne
daphne启动命令如下(在与 manage.py 文件相同的路径中运行这个命令):
daphne myproject.asgi:application -b 0.0.0.0 -p 8000
说明:
-b 监听地址
-p 监控端口
为防止项目运行中断,需要使用supervisor来运行daphne,supervisor是由python实现的一个进程管理工具,可以确保所管理的进程一直运行,当进程一点中断supervisord会自动进行重启。
安装:
yum install -y supervisor
生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
修改配置文件/etc/supervisord.conf,最后一行增加
[include]
files = supervisord.d/*.ini
表示配置文件读取supervisord.d目录下所有后缀为.ini的文件。
创建配置目录,并创建配置文件
mkdir /etc/supervisord.d/
vi /etc/supervisord.d/asgi.ini
配置示例一:
[program:daphne]
directory=/home/arrow/bmxf #项目目录
command=daphne -b 127.0.0.1 -p 8000 --proxy-headers bmxf.asgi:application #启动命令
autostart=true
autorestart=true
stdout_logfile=/tmp/websocket.log #日志
redirect_stderr=true
配置示例二:
[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:8000
# Directory where your site's project files are located
directory=/tmp/internal_tools
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=/virtualenvs/venv1/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers internal_tools.asgi:application
# Number of processes to startup, roughly the number of CPUs you have
numprocs=4
# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d
# Automatically start and recover processes
autostart=true
autorestart=true
# Choose where you want your log to go
stdout_logfile=/var/log/asgi.log
redirect_stderr=true
启动supervisord
supervisord -c /etc/supervisord.conf
最后配置nginx做反向代理到daphne启动地址(本示例中是8000端口)即可。
查看supervisor状态:
supervisorctl status
关闭supervisor:
supervisorctl stop
重启supervisor:
supervisorctl resart
这篇关于django+websocket+daphne+supervisor+nginx的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!