
1.單機啓動django項目,性能低,默認使用wsgiref模塊,性能低的wsgi協議 python3 manager.py runserver 0.0.0.0:8000 > wsgiref模塊中 2.高併發啓動django,django是沒有這個功能的,而uWSGI模塊,遵循uwsgi協議,支持多進程處理django請求 uwsgi 經過他,啓動你的django,而再也不是python3 manager.py runserver 0.0.0.0:8000 3.公司中通常用 nginx + uwsgi + django + virtualenv + supervisord(進程管理工具) 搭建筆記: 1.肯定依賴組件是否安裝 yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel nginx 正向代理,反向代理的概念 用戶阿段,去訪問mycrm.com:80 ,他想直接從80端口,找到hello視圖,也就是mycrm.com:80/hello 實現手段就是,阿段去訪問 mycrm.com:80 這個nginx服務,而且讓nginx,把hello這個請求,丟給後端的 uwsgi+django程序處理 1.基礎環境準備好 yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel 2.準備好python3環境 3.準備好virtualenv 4.安裝uWSGI 1.激活虛擬環境 source /opt/all_venv/venv2/bin/activate 2.安裝uWSGI (venv2) [root@s13linux ~ 05:18:21]$pip3 install uwsgi 3.檢查uwsgi版本 (venv) [root@slave 192.168.11.64 /opt]$uwsgi --version 2.0.17.1 #檢查uwsgi python版本 uwsgi --python-version 4.運行一個簡單的uwsgi服務器 1.建立一個test.py文件,寫入內容 def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 2.而後用uwsgi命令啓動 uwsgi --http :8000 --wsgi-file test.py 參數解釋 http :8000: 使用http協議,端口8000 wsgi-file test.py: 加載指定的文件,test.py 5.用uwsgi運行你的django項目(測試使用) 1.準備好mysite,本身寫好MTV視圖函數 /hello 先確保你在項目文件夾下,例如/opt/mysite/底下 uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1 參數解析 --http 啓動在8088端口,--module 指定項目文件夾路徑 --py-autoreload是熱加載程序 6.配置nginx反向代理uwsgi+django!!!!(此步重要!!!) 1.首先kill殺掉nginx進程 2.配置nginx.conf,經過此步才能生效!! 填入重要兩個參數,根據本身目錄結構配置,uwsgi_pass經過這個參數,nginx才能轉發請求給後端0.0.0.0:9000的應用 include /opt/nginx112/conf/uwsgi_params; uwsgi_pass 0.0.0.0:9000; --------------------------分割線-------------------------------------------------------- server { listen 80; server_name mycrm.com; location / { include /opt/nginx112/conf/uwsgi_params; uwsgi_pass 0.0.0.0:9000; root html; index index.html index.htm; #deny 10.0.0.1; } 配置nginx.conf以後,啓動nginx服務,等待配置啓動uwsgi+django
uwsgi --uwsgi 0.0.0.0:9000 --chdir=/opt/mysit --home=opt/all_venv/venv2/ --module=mysite.wsgi # 這條命令也是在項目目錄下 如 /home/perfey 7.配置supervisor進程管理工具 1.經過python2的包管理工具easy_install安裝 yum install python-setuptools easy_install supervisor 2.經過命令生成supervisor的配支文件 echo_supervisord_conf > /etc/supervisord.conf 3.寫入/etc/supervisord.conf配置信息(參數根據本身環境填寫) [program:my_crm] command=/opt/all_venv/venv2/bin/uwsgi(uwsgi的絕對路徑) --uwsgi 0.0.0.0:9000(端口號應與nginx配置中的端口號一致) --chdir=/opt/s13crm(項目目錄) --home=/opt/all_venv/venv2/(虛擬環境目錄) --module=s13crm.wsgi(項目中和項目同名文件夾下的wsgi.py文件,使用.鏈接文件夾名和文件,注意不是 /) directory=/opt/s13crm (項目目錄) startsecs=0 stopwaitsecs=0 autostart=true autorestart=true 8.啓動supervi服務,(同時啓動uwsgi+django服務) 最後啓動supervisor,完成uWSGI啓動django,nginx反向代理 supervisord -c /etc/supervisord.conf #啓動supervisor supervisorctl -c /etc/supervisord.conf restart my #重啓my項目 supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all] 9.此時訪問網站mycrm.com ,查看是否能夠經過80端口,訪問到django應用,完成項目發佈。 因爲nginx的高併發性能,配合uwsgi的多進程性能,能夠達到一個線上的django應用發佈!!! 內容博客地址: https://www.cnblogs.com/pyyu/p/9481344.html
你們都學過了django,用django寫了各類功能,寫了bbs項目,寫了路飛學城。html
我們都知道django是一個web框架,方便咱們快速開發web程序,http請求的動態數據就是由web框架來提供處理的。python
前面超哥也對nginx簡單的介紹了,本文將nginx、WSGI、uwsgi、uWSGI、django這幾個關係梳理一下。linux
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服務器nginx
傳統的c/s架構,請求的過程是 客戶端 > 服務器 服務器 > 客戶端 服務器就是:1.接收請求 2.處理請求 3.返回響應
web框架層程序員
HTTP的動態數據交給web框架,例如django遵循MTV模式處理請求。
HTTp協議使用url定位資源,urls.py將路由請求交給views視圖處理,而後返回一個結果,完成一次請求。
web框架使用者只須要處理業務的邏輯便可。
若是將一次通訊轉化爲「對話」的過程web
Nginx:hello wsgi,我剛收到一個請求,你準備下而後讓django來處理吧sql
WSGI:好的nginx,我立刻設置環境變量,而後把請求交給djangodjango
Django:謝謝WSGI,我處理完請求立刻給你響應結果flask
WSGI:好的,我在等着後端
Django:搞定啦,麻煩wsgi吧響應結果傳遞給nginx
WSGI:太棒了,nginx,響應結果請收好,已經按照要求傳遞給你了
nginx:好滴。我把響應交給用戶。合做愉快
Django Nginx+uwsgi 安裝配置
在前面的章節中咱們使用 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協議,端口8000wsgi-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
指定配置文件啓動命令
uwsgi --ini /etc/uwsgi_nginx.ini
配置nginx結合uWSGI
配置nginx.conf
worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 65;
#nginx反向代理uwsgi server { listen 80; server_name 192.168.11.64; location / { include /opt/nginx1-12/conf/uwsgi_params; uwsgi_pass 0.0.0.0:8000; root html; index index.html index.htm; }
#nginx處理靜態頁面資源
location /static{
alias /opt/nginx1-12/static;
}
#nginx處理媒體資源
location /media{
alias /opt/nginx1-12/media;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; } } }
配置完啓動nginx
supervisor
supervisor 是基於 python 的任務管理工具,用來自動運行各類後臺任務,固然你也能直接利用 nohup 命令使任務自動後臺運行,但若是要重啓任務,每次都本身手動 kill 掉任務進程,這樣很繁瑣,並且一旦程序錯誤致使進程退出的話,系統也沒法自動重載任務。
這裏超哥要配置基於virtualenv的supervisor
因爲supervisor在python3下沒法使用,所以只能用python2去下載!!!!!!
#注意此時已經退出虛擬環境了!!!!!
yum install python-setuptools
easy_install supervisor
經過命令生成supervisor的配支文件
echo_supervisord_conf > /etc/supervisord.conf
而後再/etc/supervisord.conf末尾添加上以下代碼!!!!!!
[program:my] #command=/opt/venv/bin/uwsgi --ini /etc/uwsgi_nginx.ini #這裏是結合virtualenv的命令 和supervisor的精髓!!!!
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#--home指的是虛擬環境目錄 --module找到 mysite/wsgi.py
directory=/opt/mysite startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
最後啓動supervisor,完成uWSGI啓動django,nginx反向代理
supervisord -c /etc/supervisord.conf #啓動supervisor
supervisorctl -c /etxc/supervisord.conf restart my #重啓my項目
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
從新加載supervisor
1、添加好配置文件後 2、更新新的配置到supervisord supervisorctl update 3、從新啓動配置中的全部程序 supervisorctl reload 4、啓動某個進程(program_name=你配置中寫的程序名稱) supervisorctl start program_name 5、查看正在守候的進程 supervisorctl 6、中止某一進程 (program_name=你配置中寫的程序名稱) pervisorctl stop program_name 7、重啓某一進程 (program_name=你配置中寫的程序名稱) supervisorctl restart program_name 8、中止所有進程 supervisorctl stop all 注意:顯示用stop中止掉的進程,用reload或者update都不會自動重啓。
django的靜態文件與nginx配置
mysite/settings.py
STATIC_ROOT='/opt/nginx1-12/static' STATIC_URL = '/static/' STATICFILES_DIRS=[ os.path.join(BASE_DIR,"static"), ]
上述的參數STATIC_ROOT用在哪?
經過python3 manage.py collectstatic 收集全部你使用的靜態文件保存到STATIC_ROOT!
STATIC_ROOT 文件夾 是用來將全部STATICFILES_DIRS中全部文件夾中的文件,以及各app中static中的文件都複製過來
# 把這些文件放到一塊兒是爲了用nginx等部署的時候更方便