通過Nginx部署Django(基於ubuntu)
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比較常見的一種方式。
在這種方式中,我們的通常做法是,將nginx作為服務器最前端,它將接收WEB的所有請求,統一管理請求。nginx把所有靜態請求自己來處理(這是NGINX的強項)。然后,NGINX將所有非靜態請求通過uwsgi傳遞給Django,由Django來進行處理,從而完成一次WEB請求。
可見,uwsgi的作用就類似一個橋接器。起到橋梁的作用。
Linux的強項是用來做服務器,所以,下面的整個部署過程我們選擇在Ubuntu下完成。
一、安裝Nginx
Nginx (engine x) 是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP服務器。
Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特點是占有內存少,並發能力強,事實上nginx的並發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。
打開ubuntu控制台(ctrl+alt+t)利用Ubuntu的倉庫安裝。
linux@ubuntu:~$ sudo apt-get install nginx #安
啟動Nginx:
linux@ubuntu:~$ /etc/init.d/nginx start #啟動 linux@ubuntu:~$ /etc/init.d/nginx stop #關閉 linux@ubuntu:~$ /etc/init.d/nginx restart #重啟
修改Nginx默認端口號,打開/etc/nginx/nginx.conf 文件,修改端口號。
server { listen 8000; server_name 127.0.0.1 access_log /var/log/nginx/myblog_access.log; error_log /var/log/nginx/myblog_error.log; charset utf-8; client_max_body_size 75M; root /home/linux/Desktop/MyBlog; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9001; uwsgi_read_timeout 2; } location /static/ { expires 30d; autoindex on; add_header Cache-Control private; alias /home/linux/Desktop/MyBlog/static/; } }
通過上面命令重啟nginx。
二、安裝uwsgi
linux@ubuntu:~$ sudo pip install uwsgi
測試uwsgi 寫一個test.py文件
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return “HelloWorld”
linux@ubuntu:~$ uwsgi --http :8001 --wsgi-file test.py
運行上面命令,

在我們通過Django創建myweb項目時,在子目錄myweb下已經幫我們生成的 wsgi.py文件。所以,我們只需要再創建myblog_uwsgi.ini配置文件即可,當然,uwsgi支持多種類型的配置文件,如xml,ini等。此處,使用ini類型的配置。
[uwsgi] # Django-related settings
#socket 指定項目執行的端口號。 socket = 127.0.0.1:9001 # 項目絕對路徑 chdir = /home/linux/Desktop/MyBlog # Django的wsgi文件相對路徑 wsgi-file = MyBlog/wsgi.py # process-related settings # master master = True # 最大進程數 processes = 4 # 線程數 threads = 2 #設置此參數,有一個主進程 master=True #守護進程的方式運行,log日志存在此log文件里 deamonize=/var/log/uwsgi/djangoProject.log #主進程id寫入文件里 pidfile= /var/log/nginx/uwsgi.pid # ... with appropriate permissions - may be needed # chmod-socket = 664 #退出時,清理環境 vacuum = True reload-mercy = 10 max-requests = 5000 limit-as = 512 buffer-size = 30000
接下來,切換到myweb項目目錄下,通過uwsgi命令讀取myblog_uwsgi.ini文件啟動項目。
.......................... Python main interpreter initialized at 0x8b52dc0 your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 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的啟動信息,如果有錯,就要檢查配置文件的參數是否設置有誤。
listen 指定的是nginx代理uwsgi對外的端口號。
include uwsgi_params;
uwsgi_pass 127.0.0.1:9001;
include 必須指定為uwsgi_params;而uwsgi_pass指的本機IP的端口與myweb_uwsgi.ini配置文件中的必須一致。
然后訪問主機ip 加上設置的端口8000 127.0.0.1:8000 就可以了
