django(channel)到 ubuntu
1。准备工作
删除各app/migrations/下的以数字开头的数据库同步日志文件:
假设你使用的是Pycharm,我们要生成环境包:
pip freeze > requirements.txt
阿里云安全组的配置,也就是打开80和22端口
2、正式部署
// pip -V 无效 // 安装pip apt install python-pip pip install --upgrade pip // 1.虚拟环境安装 pip install virtualenv // 2.虚拟环境管理包 apt install virtualenvwrapper // 3.新建.virtualenvs文件夹 mkdir .virtualenvs // 4.修改.bashrc文件,在最后添加 export WORKON_HOME=/root/.virtualenvs source /usr/share/virtualenvwrapper/virtualenvwrapper.sh // 5.执行以下命令 source .bashrc //安装Python3.6.4
sudo mkidr /usr/local/python3 wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz cd /usr/local/python3 sudo tar -xvf Python-3.6.4.tgz cd /usr/local/python3/Python-3.6.4 sudo ./configure --prefix=/usr/local/python3 make make install //注:若出现 zipimport.ZipImportError: can't decompress data; zlib not available Makefile:1079: recipe for target 'install' failed make: *** [install] Error 1 等错误,需要安装依赖文件: sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
// 注最好不要修改默认python 为3.6,某些系统功能需要python3.5 // 6.新建虚拟环境 mkvirtualenv EnvName -p /usr/local/python3.6.4/bin/python3
// 查看 可用的环境
lsvirtualenv
// 切换环境
workon online
注: 此时pip install -r requirements 还是失败。所以我就一个一个装了
pip install django
pip install mysqlclient
pip3 install mysqlclient 报错 “/bin/sh: 1: mysql_config: not found”的解决方法
执行 sudo apt-get install libmysqlclient-dev,
然后执行 pip install mysqlclient
pip install -U channels mysql -uroot -h 127.0.0.1 -p create database mydatabase charset utf8; exit; python manage.py makemigrations python manage.py migrate python manage.py runserver 0.0.0.0:80 pip install uwsgi
uwsgi --ini uwsgi.ini
// 关闭uwsgi
killall -9 uwsgi
// 安装nginx apt-get install nginx // 配置 参照: https://www.cnblogs.com/Mvloveyouforever/p/8627777.html //
修复样式
nginx 配置
location /static {
alias /var/searchlog/static/;
}
apt-get install redis-server
安装驱动
pip3 install channels_redis
安装 supervisor
apt install supervisor
现在,您将需要创建超级用户配置文件(通常位于/etc/supervisor/conf.d/
- 这里,我们让Supervisor监听TCP端口,然后将该套接字关闭到子进程,以便它们可以共享相同的绑定端口:
[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:8002
# Directory where your site's project files are located
directory=/var/www/SevOnline
#environment=PATH="/root/.virtualenvs/online/bin"
# 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=/root/.virtualenvs/online/bin/daphne -u /tmp/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers SevOnline.asgi:application
# Number of processes to startup, roughly the number of CPUs you have
numprocs=2
# 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/www/SevOnline/asgi.log
redirect_stderr=true
// supervisor 相关命令
// 开启/关闭 supervisor
/etc/init.d/supervisor start
/etc/init.d/supervisor stop
// 查看所管进程状态
supervisorctl status
让主管(程序管理)重读并更新其工作:
$ sudo supervisorctl reread
$ sudo supervisorctl update
创建 asgi.py 放在 setting同目录下
""" ASGI entrypoint. Configures Django and then runs the application defined in the ASGI_APPLICATION setting. """ import os import django from channels.routing import get_default_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") django.setup() application = get_default_application()
运行 daphne 仅为测试用,不能保存日志。推荐使用上边的supervisor
daphne -p 8001 myproject.asgi:application
配置nginx跳转
upstream channels-backend {
server localhost:8002;
}
location /chat { proxy_pass http://channels-backend; # 转发到daphne-worker 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 36000s; proxy_send_timeout 36000s; }