django项目,中间使用websocket进行通讯,想部署到服务器上,按照之前部署项目的思路进行部署,但是失败了。解决了一下,在此记录。
主项目下有一子app,一模版文件,一静态文件。项目中主要用到dwebsocket
了解之后,进行部署
修改settings.py文件
#在文件中加入以下变量
WEBSOCKET_FACTORY_CLASS = 'dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory'
定义Uwsgi文件
装载uwsgi
#安装依赖
yum install python36-devel#安装
pip3 install uwsgi#设置软连接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi#进入项目目录启动服务
uwsgi --http :8000 --module Httpdweb.wsgi效果和 python3 manage.py runserver 0.0.0.0:8000一样#热启动
uwsgi --http :8000 --module Httpdweb.wsgi --py-autoreload=1
#创建 Httpdweb_uwsgi.ini
[uwsgi]chdir = /home/aaa/dweb/Httpdweb
module = Httpdweb.wsgi
master = true
processes = 3
socket = 0.0.0.0:8000
vacuum = true
pythonpath = /usr/bin/python3
pidfile = /home/aaa/dweb/Httpdweb/Httpdweb.pid
daemonize = /home/aaa/dweb/Httpdweb/uwsgi.log
async = 30
ugreen = ''
http-timeout = 300
#用uwsgi启动项目
uwsgi --ini Httpdweb_uwsgi.ini
#后台自启动
uwsgi -d --ini Httpdweb_uwsgi.ini
启动之后,没毛病
部署nginx
按照之前的惯例需要用到nginx,之前直接在location中定义项目地址,增加一系列配置就行了,但是初步部署却不是那么回事,于是乎有了以下的配置
#vim nginx.conf
server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;access_log /root/myweb_access.log;error_log /root/myweb_error.log;client_max_body_size 300M; # 设置nginx最大请求主体#http请求location / {uwsgi_read_timeout 600;include uwsgi_params;uwsgi_pass 127.0.0.1:8000;uwsgi_param UWSGI_SCRIPT Httpdweb.wsgi;uwsgi_param UWSGI_CHDIR /home/aaa/dweb/Httpdweb;}#websocket请求location /ws/ {include uwsgi_params;uwsgi_pass 127.0.0.1:8000;proxy_redirect off;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";}location /static/ {root /home/aaa/dweb/Httpdweb;}
}
之前部署因为没有websocket的请求所以只需要配置http的设置,现在因为需要websocket请求,所以需要添加一项socket长连接配置
cd usr/local/nginx/sbin
./nginx
开起nginx请求主页,没问题 一切ok!