本文将nginx、WSGI、uwsgi、uWSGI、django这几个关系梳理一下。
wsgi 全称web server gateway interface,wsgi不是服务器,也不是python模块,只是一种协议,描述web server如何和web application通信的规则。
运行在wsgi上的web框架有bottle,flask,django
uwsgi 和wsgi一样是通信协议,是uWSGI服务器的单独协议,用于定义传输信息的类型
uWSGI 是一个web服务器,实现了WSGI协议,uwsgi协议。a
nginx web服务器,更加安全,更好的处理处理静态资源,缓存功能,负载均衡,因此nginx的强劲性能,配合uWSGI服务器会更加安全,性能有保障。
django 高级的python web框架,用于快速开发,解决web开发的大部分麻烦,程序员可以更专注业务逻辑,无须重新造轮子
逻辑图
web服务器
传统的c/s架构,请求的过程是 客户端 > 服务器 服务器 > 客户端 服务器就是:1.接收请求 2.处理请求 3.返回响应
web框架层
HTTP的动态数据交给web框架,例如django遵循MTV模式处理请求。 HTTp协议使用url定位资源,urls.py将路由请求交给views视图处理,然后返回一个结果,完成一次请求。 web框架使用者只需要处理业务的逻辑即可。
如果将一次通信转化为“对话”的过程
Nginx:hello wsgi,我刚收到一个请求,你准备下然后让django来处理吧
WSGI:好的nginx,我马上设置环境变量,然后把请求交给django
Django:谢谢WSGI,我处理完请求马上给你响应结果
WSGI:好的,我在等着
Django:搞定啦,麻烦wsgi吧响应结果传递给nginx
WSGI:太棒了,nginx,响应结果请收好,已经按照要求传递给你了
nginx:好滴。我把响应交给用户。合作愉快
在前面的章节中我们使用 python manage.py runserver 来运行服务器。这只适用测试环境中使用。
正式发布的服务,需要一个可以稳定而持续的服务器。
基础开发环境配置
yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
提前安装好python3环境
https://www.cnblogs.com/pyyu/p/7402145.html
virtualenv
请确保你的虚拟环境正常工作
https://www.cnblogs.com/pyyu/p/9015317.html
安装django1.11
pip3 install django==1.11
#创建django项目mysite
django-admin startproject mysite
#创建app01
python3 manage.py startapp app01
mysite/settings.py
#settings.py设置 ALLOWED_HOSTS = ['*'] install app01
mysite/urls.py
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello_django/', views.hello),
]
app01/views.py
from django.shortcuts import render,HttpResponse
# Create your views here.
def hello(request):
print('request is :',request)
return HttpResponse('django is ok ')
安装uWSGI
进入虚拟环境venv,安装uwsgi (venv) [root@slave 192.168.11.64 /opt]$pip3 install uwsgi
检查uwsgi版本
(venv) [root@slave 192.168.11.64 /opt]$uwsgi --version
2.0.17.1
#检查uwsgi python版本
uwsgi --python-version
运行简单的uWSGI
#启动一个python uwsgi --http :8000 --wsgi-file test.py
-
http :8000: 使用http协议,端口8000 -
wsgi-file test.py: 加载指定的文件,test.py
#test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
uWsgi热加载python程序
在启动命令后面加上参数 uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1
#发布命令
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#此时修改django代码,uWSGI会自动加载django程序,页面生效
运行django程序
#mysite/wsgi.py 确保找到这个文件
uwsgi --http :8000 --module mysite.wsgi
-
module mysite.wsgi: 加载指定的wsgi模块
uwsgi配置文件
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi_nginx.ini,添加如下配置: # mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /opt/mysite # Django's wsgi file module = mysite.wsgi # the virtualenv (full path) home = /opt/venv # process-related settings # master master = true # maximum number of worker processes processes = 1 # the socket (use the full path to be safe socket = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true