nginx、uwsgi部署django项目理论+实战
- 基本環境:
- Ubuntu 16.04.4 LTS
- python 3.5 virtualenv
- django project deployable
#理論分析
??部署django項目有很多方法,方法推薦可見 django官方文檔-deployment,作為一個強迫癥患者,當然是要選擇一個既簡單又高效的部署方式了。在網上找到了一張這個對比圖:
??由此我就基本決定用uwsgi部署我的django項目了,下面我們正式來介紹一下uwsgi以及相關必要理論。
##WSGI
??WSGI,the Python Web Server Gateway Interface, 也就是說WSGI 是作為 Web 服務器與Web 應用程序或應用框架之間的一種低級別的接口,完成協議之間的轉換。WSGI 是基于現存的 CGI 標準而設計的。Django框架自帶了WSGI_Server,但是性能不好,所以自帶的web server更多的是測試(debug)用途,發布時則使用生產環境的WSGI server。
??從上圖來看WSGI就像一個橋梁連接服務器和應用程序。當瀏覽器發送請求時,服務方調用應用方并提供環境信息,以及一個回調函數(提供給應用程序用來將消息頭傳遞給服務器方),并接收Web內容作為返回值。故,我們可稱WSGI是一個中間件,主要功能如下:
-
重寫環境變量后,根據目標URL,將請求消息路由到不同的應用對象。
-
允許在一個進程中同時運行多個應用程序或應用框架。
-
負載均衡和遠程處理,通過在網絡上轉發請求和響應消息。
-
進行內容后處理,例如應用XSLT樣式表。
##uWSGI
??uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI服務器進行交換。而前述WSGI是一種Web服務器網關接口,是一個Web服務器(如nginx,uWSGI等服務器)與web應用通信的一種規范。
uWSGI的主要特點如下:
- 超快的性能
- 低內存占用
- 多app管理
- 詳盡的日志功能
- 高度可定制(內存大小限制,服務一定次數后重啟等)
##uwsgi
??uwsgi是一個協議,是一個uWSGI服務器自有的協議,它用于定義傳輸信息的類型(type of information),每一個uwsgi packet前4byte為傳輸信息類型描述。而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。
nginx
??所以到現在你應該清楚的明白一點,我們要配置一個 django項目,配置一個uWSGI服務器就夠了。但是考慮到性能問題,我們在uWSGI服務器之前再加一個nginx服務器。為什么呢?原因很簡單,因為nginx具備優秀的靜態內容處理能力,所以靜態內容由nginx自己處理;動態內容轉發給uWSGI服務器,這樣就可以達到更好的客戶端響應。
#部署實戰
??根據上面的分析,我們的部署架構如下圖所示:
uWSGI部署
??uwsgi部署更詳細請參考uwsgi官方文檔,安裝命令pip install uwsgi,這里我就簡單列出條目:
# Django-related settings [uwsgi] socket = 127.0.0.1:8001# the base directory (full path) chdir = /share_dir/newsite/# Django s wsgi file wsgi-file = newsite/wsgi.py# process-related settings # master master = true# maximum number of worker processes processes = 10 enable-threads = true threads = 1# ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true daemonize = /share_dir/newsite/uwsgi.log pidfile=/share_dir/newsite/uwsgi.pid # 啟動uwsgi的用戶名和用戶組 uid=root gid=rootnginx
?安裝apt install nginx,之后修改配置文件如下:
server {# the port your site will be served onlisten 80 default_server;# the domain name it will serve for#server_name 127.0.0.1; # substitute your machine's IP address or FQDNcharset utf-8;# max upload sizeclient_max_body_size 75M; # adjust to taste# Django medialocation /media {alias /share_dir/newsite/media; # your Django project's media files - amend as required}location /static {alias /share_dir/newsite/global_static; # your Django project's static files - amend as required}# Finally, send all non-media requests to the Django server.location / {include /etc/nginx/uwsgi_params; # the uwsgi_params file you installeduwsgi_pass 127.0.0.1:8001;} }uwsgi和nginx參數映射文件(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 UWSGI_SCHEME $scheme;uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;然后再介紹幾個常用命令:
// uwsgi命令 ps -ax | grep uwsgi //查看uwsgi運行進程 uwsgi yourfile.ini //啟動uwsgi服務,必須說明這是其中一種方式,其他的請參考官方文檔 uwsgi --stop uwsgi.pid // 停止服務 uwsgi --reload uwsgi.pid // 重啟服務 // nginx 命令 systemctl status/stop/restart/start nginx.services??必須說這個總結的太簡單,當時部署我是做了大量的工作,比如了解nginx的工作模型等,有時間我再總結!
總結
以上是生活随笔為你收集整理的nginx、uwsgi部署django项目理论+实战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab 程序设计
- 下一篇: pandas 数据分析常用技巧