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; #
}
}
更新代码后的重启: 修改了访问地址可能需要 杀死端口
netstat -lnp|grep 8000
kill -9 31800
supervisorctl stop all
supervisorctl shutdown
supervisord -c /etc/supervisord.conf
supervisorctl start all
supervisorctl start myd