Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比較常見的一種方式。
在這種方式中,我們的通常做法是,將nginx作為服務(wù)器最前端,它將接收WEB的所有請求,統(tǒng)一管理請求。nginx把所有靜態(tài)請求自己來處理(這是NGINX的強項)。然后,NGINX將所有非靜態(tài)請求通過uwsgi傳遞給Django,由Django來進行處理,從而完成一次WEB請求。
可見,uwsgi的作用就類似一個橋接器。起到橋梁的作用。
Linux的強項是用來做服務(wù)器,所以,下面的整個部署過程我們選擇在Ubuntu下完成。
一、安裝Nginx
Nginx是一款輕量級的Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,并在一個BSD-like 協(xié)議下發(fā)行。其特點是占有內(nèi)存少,并發(fā)能力強,事實上nginx的并發(fā)能力確實在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好。
Nginx同樣為當(dāng)前非常流行的web服務(wù)器。利用其部署Django,我們在此也做簡單的介紹。
Nginx官網(wǎng):http://nginx.org/
打開ubuntu控制臺(ctrl+alt+t)利用Ubuntu的倉庫安裝。
fnngj@ubuntu:~$ sudo apt-get install nginx #安裝
啟動Nginx:
fnngj@ubuntu:~$ /etc/init.d/nginx start #啟動fnngj@ubuntu:~$ /etc/init.d/nginx stop #關(guān)閉fnngj@ubuntu:~$ /etc/init.d/nginx restart #重啟
修改Nginx默認(rèn)端口號,打開/etc/nginx/nginx.conf 文件,修改端口號。
server { listen 8088; # 修改端口號 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; }
大概在文件36行的位置,將默認(rèn)的80端口號改成其它端口號,如 8088。因為默認(rèn)的80端口號很容易被其它應(yīng)用程序占用。
然后,通過上面命令重啟nginx。訪問:http://127.0.0.1:8088/
如果出現(xiàn)如上圖,說明Nginx啟動成功。
二、安裝uwsgi
通過pip安裝uwsgi。
root@ubuntu:/etc# python3 -m pip install uwsgi
測試uwsgi,創(chuàng)建test.py文件:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"]
通過uwsgi運行該文件。
fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --wsgi-file test.py
接下來配置Django與uwsgi連接。此處,假定的我的django項目位置為:/home/fnngj/pydj/myweb
常用選項:
http : 協(xié)議類型和端口號
processes : 開啟的進程數(shù)量
workers : 開啟的進程數(shù)量,等同于processes(官網(wǎng)的說法是spawn the specified number ofworkers / processes)
chdir : 指定運行目錄(chdir to specified directory before apps loading)
wsgi-file : 載入wsgi-file(load .wsgi file)
stats : 在指定的地址上,開啟狀態(tài)服務(wù)(enable the stats server on the specified address)
threads : 運行線程。由于GIL的存在,我覺得這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允許主進程存在(enable master process)
daemonize : 使進程在后臺運行,并將日志打到指定的日志文件或者udp服務(wù)器(daemonize uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。
pidfile : 指定pid文件的位置,記錄主進程的pid號。
vacuum : 當(dāng)服務(wù)器退出的時候自動清理環(huán)境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)
三、Nginx+uwsgi+Django
接下來,我們要將三者結(jié)合起來。首先羅列一下項目的所需要的文件:
myweb/├── manage.py├── myweb/│ ├── __init__.py│ ├── settings.py│ ├── urls.py│ └── wsgi.py└── myweb_uwsgi.ini
在我們通過Django創(chuàng)建myweb項目時,在子目錄myweb下已經(jīng)幫我們生成的 wsgi.py文件。所以,我們只需要再創(chuàng)建myweb_uwsgi.ini配置文件即可,當(dāng)然,uwsgi支持多種類型的配置文件,如xml,ini等。此處,使用ini類型的配置。
# myweb_uwsgi.ini file[uwsgi]# Django-related settingssocket = :8000# the base directory (full path)chdir = /home/fnngj/pydj/myweb# Django s wsgi filemodule = myweb.wsgi# process-related settings# mastermaster = true# maximum number of worker processesprocesses = 4# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum = true
這個配置,其實就相當(dāng)于在上一小節(jié)中通過wsgi命令,后面跟一堆參數(shù)的方式,給文件化了。
socket 指定項目執(zhí)行的端口號。
chdir 指定項目的目錄。
module myweb.wsgi ,可以這么來理解,對于myweb_uwsgi.ini文件來說,與它的平級的有一個myweb目錄,這個目錄下有一個wsgi.py文件。
其它幾個參數(shù),可以參考上一小節(jié)中參數(shù)的介紹。
接下來,切換到myweb項目目錄下,通過uwsgi命令讀取myweb_uwsgi.ini文件啟動項目。
fnngj@ubuntu:~$ cd /home/fnngj/pydj/myweb/fnngj@ubuntu:~/pydj/myweb$ uwsgi --ini myweb_uwsgi.ini [uWSGI] getting INI configuration from myweb_uwsgi.ini*** Starting uWSGI 2.0.12 (32bit) on [Sat Mar 12 13:05:06 2016] ***compiled with version: 4.8.4 on 26 January 2016 06:14:41os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2015nodename: ubuntumachine: i686clock source: unixdetected number of CPU cores: 2current working directory: /home/fnngj/pydj/mywebdetected binary path: /usr/local/bin/uwsgi!!! no internal routing support, rebuild with pcre support !!!chdir() to /home/fnngj/pydj/mywebyour processes number limit is 15962your memory page size is 4096 bytesdetected max file descriptor number: 1024lock engine: pthread robust mutexesthunder lock: disabled (you can enable it with --thunder-lock)uwsgi socket 0 bound to TCP address :8000 fd 3Python version: 3.4.3 (default, Oct 14 2015, 20:37:06) [GCC 4.8.4]*** Python threads support is disabled. You can enable it with --enable-threads ***Python main interpreter initialized at 0x8b52dc0your server socket listen backlog is limited to 100 connectionsyour mercy for graceful operations on workers is 60 secondsmapped 319920 bytes (312 KB) for 4 cores*** Operational MODE: preforking ***WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)*** uWSGI is running in multiple interpreter mode ***spawned uWSGI master process (pid: 7158)spawned uWSGI worker 1 (pid: 7160, cores: 1)spawned uWSGI worker 2 (pid: 7161, cores: 1)spawned uWSGI worker 3 (pid: 7162, cores: 1)spawned uWSGI worker 4 (pid: 7163, cores: 1)
注意查看uwsgi的啟動信息,如果有錯,就要檢查配置文件的參數(shù)是否設(shè)置有誤。
再接下來要做的就是修改nginx.conf配置文件。打開/etc/nginx/nginx.conf文件,添加如下內(nèi)容。
……server { listen 8099; server_name 127.0.0.1 charset UTF-8; access_log /var/log/nginx/myweb_access.log; error_log /var/log/nginx/myweb_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; uwsgi_read_timeout 2; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /home/fnngj/pydj/myweb/static/; } }……
listen 指定的是nginx代理uwsgi對外的端口號。
server_name 網(wǎng)上大多資料都是設(shè)置的一個網(wǎng)址(例,www.example.com),我這里如果設(shè)置成網(wǎng)址無法訪問,所以,指定的到了本機默認(rèn)ip。
在進行配置的時候,我有個問題一直想不通。nginx到底是如何uwsgi產(chǎn)生關(guān)聯(lián)?,F(xiàn)在看來大概最主要的就是這兩行配置。
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
include 必須指定為uwsgi_params;而uwsgi_pass指的本機IP的端口與myweb_uwsgi.ini配置文件中的必須一直。
現(xiàn)在重新啟動nginx,翻看上面重啟動nginx的命令。然后,訪問:http://127.0.0.1:8099/
通過這個IP和端口號的指向,請求應(yīng)該是先到nginx的。如果你在頁面上執(zhí)行一些請求,就會看到,這些請求最終會轉(zhuǎn)到uwsgi來處理。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答
圖片精選