欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Django+Uwsgi+Nginx 实现生产环境部署

程序员文章站 2023-09-07 22:44:56
uwsgi介绍 uwsgi是一个web服务器,它实现了wsgi协议、uwsgi、http等协议。nginx中httpuwsgimodule的作用是与uwsgi服务器进...

uwsgi介绍

uwsgi是一个web服务器,它实现了wsgi协议、uwsgi、http等协议。nginx中httpuwsgimodule的作用是与uwsgi服务器进行交换。

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

wsgi是一种web服务器网关接口。它是一个web服务器(如nginx,uwsgi等服务器)与web应用(如用flask框架写的程序)通信的一种规范。

uwsgi是一种线路协议而不是通信协议,在此常用于在uwsgi服务器与其他网络服务器的数据通信。

而uwsgi是实现了uwsgi和wsgi两种协议的web服务器。

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

安装uwsgi

pip install uwsgi

uwsgi不支持windows

测试启动

创建测试文件并写入:

def application(environ, start_response): 
status = '200 ok' 
output = 'hello world! powerde by wsgi' 
response_headers = [('content-type', 'text/plain'),
('content-length', str(len(output)))] 
start_response(status, response_headers)
return [output.encode('utf8'),]

执行命令:

uwsgi --http :8080 --file test.py

浏览器访问该端口,正常情况下能得到输出。

用 uwsgi 启动django

uwsgi --http :8080 --file django_project/wsgi.py

页面能访问,但是静态文件无法加载,需要

uwsgi --http :8080 --file django_project/wsgi.py --static-map=/static=static

静态文件就能加载了。

参数说明:

  • http 这个就和 runserver 一样指定 ip 端口
  • file 这个文件就里有一个反射,如果你在调用他的时候没有指定web server就使用默认的
  • static 做一个映射,指定静态文件

uwsgi配置文件启动django项目

uwsgi 支持的参数还挺多的,可以将他们写在配置文件中。在项目同级目录创建 uwsgi.ini 文件:

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/opt/webvirtcloud/
# 指定项目的application
module=webvirtcloud.wsgi:application
# 指定sock的文件路径 
socket=/tmp/uwsgi.sock
# 进程个数 
workers=5
pidfile=/tmp/uwsgi.pid
# 指定ip端口 
http=0.0.0.0:8080 # 如果和ngxin结合,本行注释掉
# 指定静态文件
static-map=/static=/opt/webvirtcloud/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/var/log/uwsgi.log

更多参数可见: https://uwsgi-docs.readthedocs.io/en/latest/options.html

执行命令: uwsgi --ini uwsgi.ini ,命令都不再用shell终端,即使断开shell连接,页面仍然能访问。

那要如何关闭或重启它呢?

uwsgi --stop /tmp/uwsgi.pid

配置nginx

找到nginx的配置文件,用虚拟域名的就在虚拟域名里写:

server {
listen 80;
server_name localhost;

location / { 
include uwsgi_params;
uwsgi_pass 127.0.0.1:8080; //必须和uwsgi中socket的设置一致
client_max_body_size 35m;
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。