使用 uWSGI 和 Nginx 的原因
Django 是一個 Web 框架辐脖,框架的作用在于處理 request 和 reponse膀哲,其他的不是框架所關心的內(nèi)容。所以怎么部署 Django 不是 Django 所需要關心的份名。
Django 所提供的是一個開發(fā)服務器菠赚,這個開發(fā)服務器,沒有經(jīng)過安全測試抗愁,而且使用的是 Python 自帶的 simple HTTPServer 創(chuàng)建的馁蒂,在安全性和效率上都是不行的。
所以不能用 Django 的 Web 服務器直接部署Web服務蜘腌,所以就有了 django + uWSGI + Nginx 的 Web 服務器搭建方案沫屡。
uWSGI 和 Nginx 介紹
- WSGI
- uWSGI
- uwsgi
- Nginx
WSGI 是一種協(xié)議,不是任何包不是任何服務器撮珠,就和 TCP 協(xié)議一樣沮脖。它定義了 Web 服務器和 Web 應用程序之前如何通信的規(guī)范。
注:至于為什么和 Python 扯在一起?因為這個協(xié)議是由 Python 在 2003 年提出的勺届。(參考:PEP-333 和 PEP-3333 )
uWSGI 是一個全功能的 HTTP 服務器驶俊,他要做的就是把 HTTP 協(xié)議轉化成語言支持的網(wǎng)絡協(xié)議。比如把 HTTP 協(xié)議轉化成 WSGI 協(xié)議免姿,讓 Python 可以直接使用饼酿。
uwsgi 是一種 uWSGI 的內(nèi)部協(xié)議,使用二進制方式和其他應用程序進行通信胚膊。
Nginx 是一個 Web 服務器其中的 HTTP 服務器功能和 uWSGI 功能很類似嗜湃,但是 Nginx 還可以用作更多用途,比如最常用的反向代理功能澜掩。
之間的聯(lián)系:
瀏覽器 <==HTTP協(xié)議==>
Nginx <==uwsgi協(xié)議==>
uWSGI <==uwsgi協(xié)議==>
Python WSGI Module <==WSGI協(xié)議==>
Python Application
注:Nginx 主要優(yōu)化的是連接數(shù)和靜態(tài)文件购披,uWSGI 主要優(yōu)化的是 wsgi 服務。uWSGI 實際上可以完成 Nginx 的功能肩榕,但 Nginx 更強大刚陡,能直接在 Nginx 層面就完成很多事情,比如靜態(tài)文件株汉、反向代理筐乳、轉發(fā)等需求。
uWSGI 安裝及配置
1. 安裝 uWSGI
$ sudo pip install uwsgi
2. 測試 uWSGI
創(chuàng)建一個 test.py
文件
# test.py 文件內(nèi)容
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
啟動 uWSGI 測試服務
uwsgi --http :8000 --wsgi-file test.py
瀏覽器訪問 localhost:8000乔妈,顯示 hello world 表示成功蝙云。
3. 配置 uWSGI
在工程目錄下創(chuàng)建uWSGI的配置文件/root/test/mysite/uwsgi.ini
# uwsgi.ini 文件內(nèi)容
[uwsgi]
socket = 127.0.0.1:8000
chdir=/root/test/mysite
module=mysite.wsgi
master = true
workers=2
vacuum=true
thunder-lock=true
enable-threads=true
harakiri=30
post-buffering=4096
daemonize =/root/test/mysite/mysite/uwsgi.log
# uwsgi.ini 文件說明
[uwsgi]
socket = 127.0.0.1:8000
chdir=/root/test/mysite # 工程的絕對路徑
module=mysite.wsgi # wsgi.py在自己工程中的相對路徑,”.”指代一層目錄
master = true
workers=2
vacuum=true
thunder-lock=true
enable-threads=true
harakiri=30
post-buffering=4096
daemonize =/root/test/mysite/mysite/uwsgi.log # uWSGI日志的存儲路徑
注:我的 django 項目路徑是/root/test/mysite
Nginx 安裝及配置
1. 安裝 Nginx
$ sudo apt-get install nginx
2. 配置 Nginx
定位 Nginx 配置文件路徑
$ sudo nginx -t
會列出以下信息
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
即 Nginx 默認配置文件的路徑是/etc/nginx/conf/nginx.conf
打開配置文件路召,并在 http {} 內(nèi)添加 server 字段:
server {
listen 80;
server_name localhost;
charset utf-8;
access_log /root/test/mysite/nginx_access.log;
error_log /root/test/mysite/nginx_error.log;
client_max_body_size 75M;
location /static {
alias /root/test/mysite/mysite/static;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
# server 字段說明
server {
listen 80; # 代表服務器開放80端口
server_name localhost; # 服務器地址
charset utf-8;
access_log /root/test/mysite/nginx_access.log;
error_log /root/test/mysite/nginx_error.log;
client_max_body_size 75M;
location /static {
alias /root/test/mysite/mysite/static; # 自己定義的項目引用靜態(tài)文件
}
location / {
include /etc/nginx/conf/uwsgi_params;
uwsgi_pass 127.0.0.1:8000; # uWSGI綁定的監(jiān)聽地址勃刨,這里使用了8000端口
}
}
注:我的 django 項目路徑是/root/test/mysite
啟動 uWSGI 與 Nginx
1. 啟動 uWSGI
sudo uwsgi --ini /root/test/mysite/uwsgi.ini
2. 啟動 Nginx
nginx -c /etc/nginx/nginx.conf
注:-c 表示加載配置文件啟動
3. 你成功了嗎
在瀏覽器輸入 localhost 是不是可以正常訪問你的項目服務了。