User-Profile-Image
hankin
  • 5
  • 首页
  • 留言
  • 个人简介
  • 分类
    • 留言
    • 未分类
    • 数据库
    • Django
    • celery
  • 页面
  • 友链
    • 社会我毛哥
Help?

Please contact us on our email for need any support

Support
    首页   ›   Django   ›   正文
Django

使用Django+uwsgi+nginx部署Django(附带daphne+celery+supervisor)

2020-06-03 16:41:27
1043  0 5

Contents

  • 1 环境
  • 2 安装Nginx
  • 3 安装git
  • 4 安装 python2.7 环境
  • 5 部署Uwsgi
  • 6 部署celery (项目中使用了异步任务Celery)
  • 7 部署daphne(项目中使用了异步通信websocket:channels)

环境

  • CentOS 6.9+
  • python 2.7+
  • nginx 1.16.1

安装Nginx

安装教程: https://nginx.org/en/linux_packages.html#RHEL-CentOS

rpm -ivh http://nginx.org/packages/centos/6/x86_64/RPMS/nginx-1.12.2-1.el6.ngx.x86_64.rpm

启动:service nginx start
开机自启:chkconfig nginx on

将项目的nginx配置文件添加到 /etc/nginx/conf/ 目录中

安装git

安装基础环境
yum groupinstall "Development Tools"
yum install zlib-devel perl-ExtUtils-MakeMaker asciidoc xmlto openssl-devel curl-devel

下载
wget https://github.com/git/git/archive/v2.14.0.tar.gz
tar -zvxf v2.14.0.tar.gz 

安装
cd git-2.14.0
autoconf
./configure
make && make install

yum remove git
ln -s /usr/local/bin/git /usr/bin/git
git config --global credential.helper store

将项目代码git clone 下来cd /data/idb; git clone http://gitlab.xxxx.com/xxx.git

安装 python2.7 环境

# 安装 python3.6教程
yum install https://centos6.iuscommunity.org/ius-release.rpm
yum install python36u python36u-pip
yum install python36u-devel
ln -s /usr/bin/python3.6 /usr/bin/python3
ln -s /usr/bin/pip3.6 /usr/bin/pip3

# 安装python2.7教程
wget https://centos6.iuscommunity.org/ius-release.rpm
rpm -Uvh ius-release.rpm
yum install python27 python27-devel python27-pip
cd ~
mkdir .pip
cd .pip
vi pip.conf

[global]
index-url=http://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

安装环境

pip install -r requirements.txt
# 遇到错误解决
1、EnvironmentError: mysql_config not found
yum install mysql-devel
2、error: command 'gcc' failed with exit status 1
yum install openldap; yum install openldap24-libs ;  yum install openldap-clients; yum install openldap-devel; yum install openssl-devel

部署Uwsgi

uwsgi --ini uwsgi.ini
或者并发模式
uwsgi --gevent 40 --gevent-early-monkey-patch uwsgi.ini

日志:/var/log/uwsgi9001.log

部署celery (项目中使用了异步任务Celery)

安装 supervisor

pip install supervisor

修改 /etc/supervisord.conf (如没有此文件,请拷贝默认配置文件)文件,末尾

[include]
;files = relative/directory/*.ini
files = /etc/supervisor/*.ini

创建目录/etc/supervisor/,将文件supervisord_idb_celery.ini(注意确认修改相关路径)上传至该目录

文件配置示例:

[program:celerywoker]
user=root
command=/usr/local/python/python2.7.9/bin/python -u manage.py celery worker --loglevel=info
directory=/data/idb/idb-api
stdout_logfile=/var/log/celery/worker_info.log
autostart=true
stderr_logfile=/var/log/celery/worker_err.log
autorestart=true
redirect_stderr=true
stopsignal=QUIT

[supervisord]
[supervisorctl]

相关命令:

重启
supervisord -c /etc/supervisord.conf
查看守护进程
supervisorctl status
重启守护进程
supervisorctl restart xxx

部署daphne(项目中使用了异步通信websocket:channels)

supervisor 配置文件示例(根据实际情况自行修改):

[fcgi-program:oddstorm-ws]
# TCP socket used by Nginx backend upstream
socket=tcp://127.0.0.1:9010

# Directory where your site's project files are located
directory=/home/OddStormServer

# 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=daphne -u /run/daphne/daphne-pro%(process_num)d.sock --fd 0 --access-log /var/log/oddstorm-ws.log --proxy-headers OddStorm.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/log/oddstorm-ws.log
redirect_stderr=true

nginx 中的配置

upstream django-ws {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:9010; # for a web port socket (we'll use this first)
}
...
server {
    ...
        location /ws/ {
        uwsgi_pass  django-ws;

        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;
    }
    ...
}

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

5 打赏
评论 (2)

点击这里取消回复。

欢迎您 游客  

目录树
  • 1 环境
  • 2 安装Nginx
  • 3 安装git
  • 4 安装 python2.7 环境
  • 5 部署Uwsgi
  • 6 部署celery (项目中使用了异步任务Celery)
  • 7 部署daphne(项目中使用了异步通信websocket:channels)
疯狂小土豆
专注Python Web开发
13文章 26评论 25点赞 9346浏览

最新评论
+7
随机文章
日志配置: 如何正确配置日志(logging)
2年前
Django基础:如何获得SSL证书与配置HTTPS
2年前
Django get_or_create()不是线程安全的
2年前
浅谈爬虫与反爬虫(附PPT)
1年前
Django基础: Utils模块精选实用工具介绍
2年前
图像链接
归档文章
  • celery (1)
  • Django (7)
  • 数据库 (1)
  • 未分类 (4)
  • 留言 (1)
Copyright © 2022 网站备案号: 12313213213
                   疯狂的小土豆 主题. Designed by Mundy
主页
页面
博主
疯狂小土豆
疯狂小土豆 管理员
专注Python Web开发
13 文章 26 评论 9346 浏览
测试
测试
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付