本文將nginx、WSGI、uwsgi、uWSGI、django這幾個關係梳理一下。
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服務器html
傳統的c/s架構,請求的過程是 客戶端 > 服務器 服務器 > 客戶端 服務器就是:1.接收請求 2.處理請求 3.返回響應
web框架層python
HTTP的動態數據交給web框架,例如django遵循MTV模式處理請求。
HTTp協議使用url定位資源,urls.py將路由請求交給views視圖處理,而後返回一個結果,完成一次請求。
web框架使用者只須要處理業務的邏輯便可。
若是將一次通訊轉化爲「對話」的過程nginx
Nginx:hello wsgi,我剛收到一個請求,你準備下而後讓django來處理吧程序員
WSGI:好的nginx,我立刻設置環境變量,而後把請求交給djangoweb
Django:謝謝WSGI,我處理完請求立刻給你響應結果sql
WSGI:好的,我在等着django
Django:搞定啦,麻煩wsgi吧響應結果傳遞給nginxflask
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/a-dyw/p/9356042.html
virtualenv
請確保你的虛擬環境正常工做
https://www.cnblogs.com/a-dyw/p/9356042.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配置文件
![](http://static.javashuo.com/static/loading.gif)
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等部署的時候更方便
參考文檔:http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
uwsgi熱加載:https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/Management.html