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
相關文章
相關標籤/搜索