Nginx+uwsgi+Django环境部署

1.nginxhtml

是一个Http和反向代理服务器python

2.uwsginginx

uWSGI、WSGI和uwsgi的区别web

WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口sql

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。数据库

uwsgi是一种线路协议,经常使用于在uWSGI服务器与其余网络服务器的数据通讯django

Nginx中HttpUwsgiModule的做用是与uWSGI服务器进行交换后端

3.请求响应的过程浏览器

  用户从网页浏览器中发出请求,nginx 服务器收到请求后,会经过 uwsgi 模块将请求转发给 uwsgi 服务器,uwsgi 服务器经过django处理完毕后将结果返回给 nginx,浏览器将最终的结果展示给用户服务器

 4.搭建

(1)Django运行

将项目上传,使用的是sqlite数据库

安装sqlite3

sudo apt-get install sqlite3

查看sqlite版本

sqlite3 -version

运行

python manage.py runserver 0.0.0.0:8080

说明:

  0.0.0.0表示捆绑监听服务器上的全部网卡IP地址

  Django 启动遇到 Invalid HTTP_HOST header

(2)安装uwsgi

须要在Linux系统上

pip install uwsgi

国内镜像

pip install -i https://pypi.douban.com/simple uwsgi

  查看版本

uwsgi --version

  A.测试

  建立test.py

def application(env,start_response): start_response('200 OK',[('Content-Type','text/html')]) return [b"Hello,this is uwsgi."]

  运行测试

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

浏览器打开http://192.168.2.157:8080/

页面输出

  Hello,this is uwsgi.

  B.uwsgi运行Django

  直接运行

  进入项目根目录

uwsgi --http :8080 --module mydjango.wsgi

说明:

  --module mydjango.wsgi为加载指定的wsgi模块

  热加载

uwsgi --http :8080 --module mydjango.wsgi --py-autoreload=1

浏览器访问

  使用uwsgi配置文件

  在项目根目录添加uwsgi.ini

[uwsgi]
socket=127.0.0.1:8080
master = true
chdir=/www/mydjango
module=mydjango.wsgi
max_requests= 1000
daemonize=/home/baby/mydjango.log
vacuum=true

  运行

uwsgi --ini uwsgi.ini

查看输出的log

 

(3)nginx配置uwsgi

安装nginx

sudo apt-get install nginx

启动

sudo service nginx restart

uwsgi_params配置文件

uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;

  mydjango.conf

server { listen 8000; server_name 192.168.2.157; charset utf-8; access_log /home/freshaccess.log; error_log /home/fresherror.log; # max upload size client_max_body_size 75M; location / { uwsgi_pass 0.0.0.0:8080; include uwsgi_params; uwsgi_send_timeout 3600; # 指定链接到后端uWSGI的超时时间。 uwsgi_param UWSGI_SCRIPT mydjango; uwsgi_param UWSGI_CHDIR /www/mydjango; } }

重启nginx

浏览器访问http://192.168.2.157:8000/blog/

 访问过程

web client  <->  web server(nginx) <-> socket <-> uWSGI <-> Django
相关文章
相关标签/搜索