環境:
centos 7.0 :
[root@judge soft]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
[root@judge soft]# uname -r
3.10.0-123.9.3.el7.x86_64
nginx 1.8.0 編譯安裝
[root@judge ~]# cd /usr/local/nginx/sbin/
[root@judge sbin]# ls
nginx
[root@judge sbin]# ./nginx -v
nginx version: nginx/1.8.0
uwsgi: uwsgi --version
uwsgi -version 可看到更多的內容
[root@judge soft]# uwsgi --version
2.0.11.2
python 2.7.5
django :
>>> import django
>>> django.__version__
'1.8.2'
原理: client 發送http請求----經過nginx------uwsgi----django----Nosql mysql 可用兩張圖來說明
基於軟件的發展:
在過去Python的Web服務器的解決方案基本上只有mod_wsgi。其中最流行的(或理解為時尚)的方法是最近Gunicorn
如今nginx和uwsgi結合,在基於 Python的Web應用程序上你能獲得在線程(或非線程)之上更好的性能體驗。暫且先這么認為吧。
WSGI:
wsgi:web server gateway interface 。It is a specification for web servers and application servers to communicate with web applications (though it can also be used for more than that) WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx)與應用服務器(如uWSGI服務器)通信的一種規范。
uWSGI
uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI服務器進行交換。
要注意 WSGI / uwsgi / uWSGI 這三個概念的區分。
- WSGI,是一種通信協議。
- uwsgi同WSGI一樣是一種通信協議。
- 而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。
uwsgi協議是一個uWSGI服務器自有的協議,它用於定義傳輸信息的類型(type of information),每一個uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣東西。
為什么有了uWSGI為什么還需要nginx?因為nginx具備優秀的靜態內容處理能力,然后將動態內容轉發給uWSGI服務器,這樣可以達到很好的客戶端響應。
驗證uwsgi
django-admin.py startproject aaa_pj
cd /opt/aaa_pj/aaa_pj/
vi test.py
#!/usr/bin/env python
#coding:utf-8
def application(env,start_response):
start_response('200 OK',[('Content-Type','text/html')])
return "Hello World
uwsgi --http :8001 --wsgi-file /opt/aaa_pj/aaa_pj/test.py 打開瀏覽器 ip:8001 出現hello world 則說明uwsgi安裝沒有問題
python /opt/aaa_pj/manage.py runserver 0.0.0.0:82 驗證django
連接django和uwsgi
編寫django_wsgi.py文件,將其放在與文件manage.py同一個目錄下。
#!/usr/bin/env python
#coding:utf-8
import os
import sys
#將系統的編碼設置成UTF8
reload(sys)
sys.setdefaultencoding('utf8')
os.environ.setdefault("DJANGO_SETTINGS_MODULE","aaa_pj.settings")
uwsgi --http :8002 --chdir /opt/aaa_pj/ --module django_wsgi
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
uwsgi --http :8002 --chdir /opt/aaa_pj/ --module django_wsgi
但是到這樣一步,網頁始終無法打開,
暫且先不管
設置uwsgi與Nginx連接
nginx配置:
uWSGI 配置
前面我們是直接使用命令行來啟動 uWSGI,在實際部署環境中,我們常用的是配置文件的方式,而非命令行的方式。
我的 Django 程序目錄:/opt/aaa_pj/
這里讓 Nginx 采用 8088 端口與 uWSGI 通訊,請確保此端口沒有被其它程序采用。
uWSGI 支持多種配置文件格式,比如 xml,ini,json 等等都可以。
1. xml 配置
請確定你在上一節中的django_wsgi.py文件已經存在了。新建一個XML文件:aaa_pj.xml,將它放在 /opt/aaa_pj 目錄下
<uwsgi>
<socket>127.0.0.1:8088</socket>
<listen>80</listen>
<master>true</master>
<pythonpath>/opt/aaa_pj</pythonpath>
<processes>1</processes>
<logdate>true</logdate>
<daemonize>/var/log/uwsgi.log</daemonize>
<plugins>python</plugins>
</uwsgi>
執行命令:uwsgi -x /opt/aaa_pj/aaa_pj.xml
or /usr/
local
/bin/uwsgi -x /opt/aaa_pj/aaa_pj.xml
結果報錯了:
[root@judge aaa_pj]# uwsgi -x /opt/aaa_pj/aaa_pj.xml
[uWSGI] parsing config file /opt/aaa_pj/aaa_pj.xml
open("./python_plugin.so"): No such file or directory [core/utils.c line 3675]
!!! UNABLE to load uWSGI plugin: ./python_plugin.so: cannot open shared object file: No such file or directory !!!
根據提示 我將xml文件里的 plugin那行刪除,結果再執行就沒有報錯了
[root@judge aaa_pj]# vi aaa_pj.xml
[root@judge aaa_pj]# uwsgi -x /opt/aaa_pj/aaa_pj.xml
[uWSGI] parsing config file /opt/aaa_pj/aaa_pj.xml
此時我重啟nginx 又報錯了
[root@judge sbin]# ./nginx
nginx: [emerg] unknown log format "/opt/logs/access.log" in /usr/local/nginx/conf/nginx.conf:43
根據提示 不能用=號
server {
listen 80;
server_name localhost;
access_log /opt/logs/access.log;
error_log /opt/logs/error.log;
location / {
uwsgi_pass 127.0.0.1:8088;
include /usr/local/nginx/conf/uwsgi_params;
}
access_log off;
}
再啟動nginx就沒有報錯了。
由於前面的錯誤,網頁沒有成功打開。因此還需要仔細找找原因。
加載指定的xml配置文件。當使用命令行參數時,可以使用簡化命令“-x”。當然也可以不簡寫:
甚至如果在命令行的最后一個參數以“.xml”結尾,那么就隱含將加載該xml文件作為配置。
有時候因各種環境問題,-x --xml 命令識別不了,可以使用下面的 ini 配置方式:
2. ini 配置
然后執行命令:
vhost
=
false
plugins
=
python
socket
=
127.0
.
0.1
:
8088
master
=
true
enable
-
threads
=
true
workers
=
1
wsgi
-
file
=
/opt
/opt
_pj
/opt
_pj
/
wsgi.py
chdir
=/opt
/opt
_pj
/
然后執行命令:
uwsgi --ini /root/nowamagic_venv/nowamagic_pj.ini&