Django + Uwsgi + Nginx 实现生产环境部署的方法
作者:金角大王 发布时间:2023-01-01 06:02:49
如何在生产上部署Django?
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。
uwsgi介绍
uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。
1.WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。
2.uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
3.而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
4.uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
uwsgi性能非常高
uWSGI的主要特点如下
1.超快的性能
2.低内存占用(实测为apache2的mod_wsgi的一半左右)
3.多app管理(终于不用冥思苦想下个app用哪个端口比较好了-.-)
4.详尽的日志功能(可以用来分析app性能和瓶颈)
5.高度可定制(内存大小限制,服务一定次数后重启等)
总而言之uwgi是个部署用的好东东,正如uWSGI作者所吹嘘的:
If you are searching for a simple wsgi-only server, uWSGI is not for you, but if you are building a real (production-ready) app that need to be rock-solid, fast and easy to distribute/optimize for various load-average, you will pathetically and morbidly fall in love (we hope) with uWSGI.
Uwsgi 安装使用
# Install the latest stable release:
pip install uwsgi
# ... or if you want to install the latest LTS (long term support) release,
pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
基本测试
Create a file called test.py:
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
运行
uwsgi --http :8000 --wsgi-file test.py
用uwsgi 启动django
uwsgi --http :8000 --module mysite.wsgi
可以把参数写到配置文件里
alex@alex-ubuntu:~/uwsgi-test$ more crazye-uwsgi.ini
[uwsgi]
http = :9000
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /home/alex/CrazyEye
# Django's wsgi file
wsgi-file = CrazyEye/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
#monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum = true
启动
/usr/local/bin/uwsgi crazye-uwsgi.ini
Nginx安装使用
sudo apt-get install nginx
sudo /etc/init.d/nginx start # start nginx
为你的项目生成Nginx配置文件
You will need the uwsgi_params file, which is available in the nginx directory of the uWSGI distribution, or from https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
Copy it into your project directory. In a moment we will tell nginx to refer to it.
Now create a file called mysite_nginx.conf, and put this in it:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
This conf file tells nginx to serve up media and static files from the filesystem, as well as handle requests that require Django's intervention. For a large deployment it is considered good practice to let one server handle static/media files, and another handle Django applications, but for now, this will do just fine.
Symlink to this file from /etc/nginx/sites-enabled so nginx can see it:
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
Deploying static files
Before running nginx, you have to collect all Django static files in the static folder. First of all you have to edit mysite/settings.py adding:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
and then run
python manage.py collectstatic
此时启动Nginx 和Uwsgi,你的django项目就可以实现高并发啦!
来源:https://www.cnblogs.com/alex3714/p/6538374.html


猜你喜欢
- 很多用户在网站上会糊弄填写一个电子信箱,请问有什么办法可以阻止这种行为?我们通常用两种方法来进行判断:第一种,设定只有形如aspxhome@
- function BrowseFolder(){ try{ var Message = "请选择文件夹"; //选择框提
- 方法一:利用Cookies对象 因为Cookies对象把变量的值保存在浏览器客户端,所以可以根据Cookies保存的IsVoted的值来判断
- Python信息抽取之乱码解决办法就事论事,直说自己遇到的情况,和我不一样的路过吧,一样的就看看吧信息抓取,用python,beautifu
- 编写tasks.pyfrom celery import Celeryfrom tornado.httpclient import HTTP
- 使用穷举法求两个数的最大公约数for m in range (0,2): a = int(input("
- 一、简介本文旨在使用两种方法来实现sin函数的模拟,具体的模拟方法是使用机器学习来实现的,我们使用Python的torch模块进行机器学习,
- zhanglunray 问:我在mzzx_pic这个层设置了左边距,在ie里显示是正常的,但是在ff里显示时margin-left却没有起到
- 本文实例为大家分享了python实现俄罗斯方块的具体代码,供大家参考,具体内容如下#coding=utf-8 from tkinter im
- 目录一、索引基本知识1.1 索引的优点1.2 索引的用处1.3 索引的分类1.4 面试技术名词1.5 索
- 一、迭代器迭代器就是iter(可迭代对象函数)返回的对象,说人话.......可迭代对象由一个个迭代器组成可以用next()函数获取可迭代对
- 所谓“评论”不是必须得有文本框,用户录入提交数据才算,广义上的评论包括用户的任何“表态”,典型如打分。我接触电子商务时间并不长,最早应该是0
- 内容摘要:现在博客很流行,相信应该上网时间稍微长点的朋友都会在这或者在那的有一个自己的博客。对于一些有一定能力的朋友,可能更喜欢自己去下载一
- 引言您可以使用df.loc()函数在Pandas DataFrame的末尾添加一行:#add row to end of DataFrame
- 前言大家好,我是辣条相信大家都能感觉到最近天气的多变,好几次出门半路天气转变。辣条也深受其扰,直接给我整感冒,就差被隔离起来了,既然天气我没
- 在docker中安装mysqlubuntu官方镜像是精简的ubuntu系统,很多软件和库没有安装,所以直接安装mysql的话依赖较多,建议直
- 方法一:<span style="font-size:14px;">#read txt method one
- 解读model.named_parameters()与model.parameters()model.named_parameters()迭
- 观察一下”插入排序“:其实不难发现她有个缺点:如果当数据是”5, 4, 3, 2, 1“的时候,此时我们将“无序块”中的记录插入到“有序块”
- 本文实例讲述了jQuery实现简单复制json对象和json对象集合操作。分享给大家供大家参考,具体如下:<!DOCTYPE html