django 项目部署(django + uwsgi + nginx + surpervisor)

django + uwsgi + nginx + surpervisor

服务简介

uwsgi

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。

  • WSGI是一种通信协议。
  • uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
  • uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。

uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。- 百度百科

整体的项目结构如下所示:
在这里插入图片描述

django

创建django项目

# 创建helloWord项目
django-admin startproject helloWorld 
# 查看项目目录
tree
.
├── helloWorld
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
# 运行项目
python manager.py runserver 0.0.0.0:9000

虚拟环境

虚拟环境实现可以达到环境隔离,避免污染

# 安装软件
yum install python-setuptools python-devel
python3 -m pip install virtualenvwrapper

# 执行初始化脚本  如果环境变量当中不存在用find 查找
$ virtualenvwrapper.sh

# 进入项目目录
cd /opt/helloWorld 

# 创建虚拟环境
 virtualenv -p python3 venv
 
# 查看目录
$ ls
venv

# 启用虚拟环境
source venv/bin/activate

# 启动django程序
python manager.py runserver 0.0.0.0:8000

在这里插入图片描述

uwsgi

# 安装软件
pip install uwsgi

在项目下面创建文件 uwsgi.ini

# 配置信息
 [uwsgi]
# 使用nginx连接时使用,Django程序所在服务器地址
socket=0.0.0.0:9000
#直接做web服务器使用,Django程序所在服务器地址
#项目目录
chdir=/opt/helloWorld
#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=helloWorld/wsgi.py
# 进程数
processes=4
# 线程数
threads=2
# uwsgi服务器的角色
master=True
# 存放进程编号的文件
pidfile=uwsgi.pid
# 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。我们以前的runserver是依赖终端的
daemonize=uwsgi.log
# 指定依赖的虚拟环境
virtualenv=/opt/helloWorld/venv

启动服务器

uwsgi --ini uwsgi.ini

nginx

server {
      listen       80;
      #server_name  hello.com;

      access_log /var/log/hello/nginx_access.log ;
      error_log /var/log/hello/nginx_error.log ;

      location / {
          include uwsgi_params;
          uwsgi_pass 127.0.0.1:9000;  # 后台服务端口
      }
      # 如果存在静态文件, 指定后台静态文件位置,要不可能会找不到返回404
      location ^~/static {
          alias /opt/helloWorld/static;
      }
  }

surpervisor 进程管理

安装服务

# 安装服务
yum install supervisor

# 启动服务
supervisord -c /etc/supervisord.conf

配置

[program:uwsgi]
# 执行目录
directory=/opt/helloWorld/
# 执行命令
command=/usr/local/bin/uwsgi --ini /opt/helloWorld/uwsgi.ini
# 启动用户
user=root
# 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
autorestart=true
# supervisor启动的时候是否随着同时启动
autostart=true
# 启动失败自动重试次数,默认是3
startretries=3
# 把stderr重定向到stdout,默认 false
redirect_stderr=true
启动5秒后没有异常退出,就表示进程正常启动了,默认为1秒
startsecs=5
# 输出日志路径
stdout_logfile=/var/log/hello/supervisor.log
#stdout日志文件大小,默认 50MB
stdout_logfile_maxbytes = 10MB
#stdout日志文件备份数
stdout_logfile_backups = 10
# 进程启动优先级,默认999,值小的优先启动
priority=999

重新载入配置

# 重新载入配置
supervisorctl reload
# 查看服务运行状态
supervisorctl status

supervisorctl 常用命令

supervisorctl status:查看所有进程的状态
supervisorctl stop uwsgi:停止uwsgi
supervisorctl start uwsgi:启动uwsgi
supervisorctl restart uwsgi: 重启 uwsgi
supervisorctl update :配置文件修改后可以使用该命令加载新的配置
supervisorctl reload: 重新启动配置中的所有程序

注意:

  1. 使用supervisor ,修改配置文件后要执行supervisorctl update 来读取配置信息,否则不生效。

  2. 关于surpervisor reload 出现错误

error: <class 'xmlrpclib.Fault'>, <Fault 6: 'SHUTDOWN_STATE'>: file: /usr/lib64/python2.7/xmlrpclib.py line: 794 

解决方法:
重新加载配置,todo 查找更优雅的解决方式

supervisorctl shutdown 
supervisord -c /etc/supervisor/supervisord.conf 

【参考】
django 官方文档
https://docs.djangoproject.com/zh-hans/2.2/howto/deployment/wsgi/uwsgi/

【后记】

2021年200篇文章 之 第1篇 django项目部署

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值