日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Django之部署NGINX+uWSGI

發布時間:2025/3/8 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Django之部署NGINX+uWSGI 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考地址:http://www.cnblogs.com/CongZhang/p/6548529.html

????????? http://www.cnblogs.com/alex3714/p/6538374.html

??????????http://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

------------------------------------------------------------------------------------------

使用NGINX+uWSGI部署django的生產環境,以提供高并發。

訪問網站的流程:web請求/響應 <---> nginx服務器 <---> uWSGI服務器 <---> django

uWSGI概念:

????uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI服務器進行交換。

?????要注意 WSGI / uwsgi / uWSGI 這三個概念的區分。

??????1. WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx,uWSGI等服務器)與web應用(如用Flask框架寫的程序)通信的一種規范。

????? 2. uwsgi是一種線路協議而不是通信協議,在此常用于在uWSGI服務器與其他網絡服務器的數據通信。

????? 3. 而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。

????? 4. uwsgi協議是一個uWSGI服務器自有的協議,它用于定義傳輸信息的類型(type of information),每一個uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣東西。

?????uWSGI的主要特點如下:

????? 1. 超快的性能

????? 2. 低內存占用(實測為apache2的mod_wsgi的一半左右)

????? 3. 多app管理(終于不用冥思苦想下個app用哪個端口比較好了)

????? 4. 詳盡的日志功能(可以用來分析app性能和瓶頸)

????? 5. 高度可定制(內存大小限制,服務一定次數后重啟等)

-------------------------------------------------------------------------------------------

部署:

總共分為兩步,安裝uWSGI和編寫配置文件,安裝NGINX和編寫配置文件

我的環境Centos6.8,Django1.11,python3.6.1

? ? 1. 首先確保django項目可以使用manage.py正常運行

????2. 安裝uWSGI

????[root@localhost?~]#pip3?install?uwsgi

????3. 測試uWSGI ?

????#?創建?test.py?文件????def?application(env,?start_response):start_response('200?OK',?[('Content-Type','text/html')])return?[b"Hello?World"]?#?python3#return?["Hello?World"]?#?python2#?啟動?uWSGI?服務[root@localhost?~]#?uwsgi?--http?:8000?--wsgi-file?test.py?#?查看啟動進程[root@localhost?~]#?netstat?-lnpt?|?grep?uwsgitcp????????0??????0?127.0.0.1:26685?????????????0.0.0.0:*???????????????????LISTEN??????22120/uwsgi?????????tcp????????0??????0?0.0.0.0:8000????????????????0.0.0.0:*???????????????????LISTEN??????22120/uwsgi??#?在瀏覽器中訪問?http://服務器ip地址:8000?就可以看到?Hello?World?字樣了

????4.?將啟動參數寫入到配置文件中,然后啟動 django 項目

????????4.1 創建配置文件

????[root@localhost?~]#?cd?/myproject???#?進入到?django項目的主目錄[root@localhost?myproject]#?vim?myproject-uwsgi.ini?#?文件名隨意

?????4.2 寫入以下內容

????[uwsgi]#?對外提供?http?服務的端口http?=?:9000#the?local?unix?socket?file?than?commnuincate?to?Nginx?用于和?nginx?進行數據交互的端口socket?=?127.0.0.1:8001#?the?base?directory?(full?path)??django?程序的主目錄chdir?=?/myproject#?Django's?wsgi?file??django項目的項目目錄中的wsgi文件wsgi-file?=?/myproject/myproject/wsgi.py#?maximum?number?of?worker?processesprocesses?=?4#thread?numbers?startched?in?each?worker?processthreads?=?2#monitor?uwsgi?status??通過該端口可以監控?uwsgi?的負載情況stats?=?127.0.0.1:9191#?clear?environment?on?exitvacuum??????????=?true#?后臺運行,并輸出日志daemonize?=?/var/log/uwsgi.log

????????4.3?通過 uwsgi 配置文件啟動 django 程序(此時訪問項目其靜態文件加載不了)

????[root@localhost?myproject]#?/usr/local/bin/uwsgi?myproject_uwsgi.ini#?在瀏覽器中?通過訪問?http://服務器ip地址:9000?可以看到django項目

????5 安裝NGINX(百度即可,安裝完成后測試是否成功)

????6 配置NGINX的配置文件

????????6.1?在 django 的主目錄下創建下面的 nginx 配置文件,然后做軟連接到 nginx 的配置文件目錄

????????????????????或者直接在 nginx 配置文件目錄中添加該文件也可以

????[root@localhost?myproject]#?vim?/usr/local/nginx/conf/myproject-nginx.conf????http????{include?mime.types;default_type?application/octet-stream;server_names_hash_bucket_size?3526;server_names_hash_max_size?4096;#?the?upstream?component?nginx?needs?to?connect?toupstream?django?{#?server?unix:///path/to/your/mysite/mysite.sock;?#?for?a?file?socketserver?127.0.0.1:8001;?#?for?a?web?port?socket?(we'll?use?this?first)}#?configuration?of?the?serverserver?{#?the?port?your?site?will?be?served?onlisten??????8080;?#?the?domain?name?it?will?serve?forserver_name?192.168.2.180;?#?substitute?your?machine's?IP?address?or?FQDNcharset?????utf-8;#?max?upload?sizeclient_max_body_size?75M;???#?adjust?to?taste#?Django?media#?location?/media??{#?????alias?/path/to/your/mysite/media;??#?your?Django?project's?media?files?-?amend?as?required??????#?}????location?/static?{alias?/myproject/static;?#?your?Django?project's?static?files?-?amend?as?required}???#?Finally,?send?all?non-media?requests?to?the?Django?server.location?/?{uwsgi_pass??django;include?????/myproject/uwsgi_params;?#?the?uwsgi_params?file?you?installed}??? }??? }??? events?{worker_connections??1024;??##?Default:?1024 }

? ? 6.2 復制NGINX配置文件下的uwsgi_params文件到django項目的主目錄下

????[root@localhost?myproject]#?cp?/usr/local/nginx/conf/uwsgi_params?/myproject

????6.3 啟動服務: ? ?

????[root@localhost?myproject]#?nginx?-tnginx:?the?configuration?file?/usr/local/nginx/conf/nginx.conf?syntax?is?oknginx:?configuration?file?/usr/local/nginx/conf/nginx.conf?test?is?successful[root@localhost?myproject]#?nginx?-c?/usr/local/nginx/conf/myproject-nginx.conf[root@localhost?myproject]#?netstat?-lnpt?|?grep?nginxtcp????????0??????0?0.0.0.0:8080????????????????0.0.0.0:*???????????????????LISTEN??????2537/nginx??????????tcp????????0??????0?0.0.0.0:80??????????????????0.0.0.0:*???????????????????LISTEN??????1463/nginx

????????此時訪問服務器IP地址:8080即可

-------------------------------------------------------------------------------------------

轉載于:https://blog.51cto.com/linqi/1979778

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Django之部署NGINX+uWSGI的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。