Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > Python网站web

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

来源:中文源码网    浏览:360 次    日期:2024-04-27 12:35:07
【下载文档:  详解Django+Uwsgi+Nginx 实现生产环境部署.txt 】


详解Django+Uwsgi+Nginx 实现生产环境部署
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
更多参数可见: http://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;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中文源码网。

相关内容