nginx部署django

使用 uWSGI 和 Nginx 的原因

Django 是一個(gè) Web 框架,框架的作用在于處理 request 和 reponse,其他的不是框架所關(guān)心的內(nèi)容。所以怎么部署 Django 不是 Django 所需要關(guān)心的。

Django 所提供的是一個(gè)開發(fā)服務(wù)器,這個(gè)開發(fā)服務(wù)器,沒(méi)有經(jīng)過(guò)安全測(cè)試,而且使用的是 Python 自帶的 simple HTTPServer 創(chuàng)建的,在安全性和效率上都是不行的。

所以不能用 Django 的 Web 服務(wù)器直接部署Web服務(wù),所以就有了 django + uWSGI + Nginx 的 Web 服務(wù)器搭建方案。

uWSGI 和 Nginx 介紹

  • WSGI
  • uWSGI
  • uwsgi
  • Nginx

WSGI 是一種協(xié)議,不是任何包不是任何服務(wù)器,就和 TCP 協(xié)議一樣。它定義了 Web 服務(wù)器和 Web 應(yīng)用程序之前如何通信的規(guī)范。
注:至于為什么和 Python 扯在一起?因?yàn)檫@個(gè)協(xié)議是由 Python 在 2003 年提出的。(參考:PEP-333 和 PEP-3333 )

uWSGI 是一個(gè)全功能的 HTTP 服務(wù)器,他要做的就是把 HTTP 協(xié)議轉(zhuǎn)化成語(yǔ)言支持的網(wǎng)絡(luò)協(xié)議。比如把 HTTP 協(xié)議轉(zhuǎn)化成 WSGI 協(xié)議,讓 Python 可以直接使用。

uwsgi 是一種 uWSGI 的內(nèi)部協(xié)議,使用二進(jìn)制方式和其他應(yīng)用程序進(jìn)行通信。

Nginx 是一個(gè) Web 服務(wù)器其中的 HTTP 服務(wù)器功能和 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 服務(wù)。uWSGI 實(shí)際上可以完成 Nginx 的功能,但 Nginx 更強(qiáng)大,能直接在 Nginx 層面就完成很多事情,比如靜態(tài)文件、反向代理、轉(zhuǎn)發(fā)等需求。

uWSGI 安裝及配置

1. 安裝 uWSGI

$ sudo pip install uwsgi

2. 測(cè)試 uWSGI
創(chuàng)建一個(gè) 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

啟動(dòng) uWSGI 測(cè)試服務(wù)

uwsgi --http :8000 --wsgi-file test.py

瀏覽器訪問(wèn) 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 文件說(shuō)明
[uwsgi]
socket = 127.0.0.1:8000  
chdir=/root/test/mysite  # 工程的絕對(duì)路徑
module=mysite.wsgi  # wsgi.py在自己工程中的相對(duì)路徑,”.”指代一層目錄
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日志的存儲(chǔ)路徑

注:我的 django 項(xiàng)目路徑是/root/test/mysite

Nginx 安裝及配置

1. 安裝 Nginx

$ sudo apt-get install nginx

2. 配置 Nginx
定位 Nginx 配置文件路徑

$ sudo nginx -t

會(huì)列出以下信息

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

即 Nginx 默認(rèn)配置文件的路徑是/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 字段說(shuō)明
server {
    listen 80;  # 代表服務(wù)器開放80端口
    server_name localhost;  # 服務(wù)器地址
    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;  # 自己定義的項(xiàng)目引用靜態(tài)文件
    }

    location / {
        include     /etc/nginx/conf/uwsgi_params;
        uwsgi_pass  127.0.0.1:8000;  # uWSGI綁定的監(jiān)聽地址,這里使用了8000端口
    }
}

注:我的 django 項(xiàng)目路徑是/root/test/mysite

啟動(dòng) uWSGI 與 Nginx

1. 啟動(dòng) uWSGI

sudo uwsgi --ini /root/test/mysite/uwsgi.ini

2. 啟動(dòng) Nginx

nginx -c /etc/nginx/nginx.conf

注:-c 表示加載配置文件啟動(dòng)

3. 你成功了嗎
在瀏覽器輸入 localhost 是不是可以正常訪問(wèn)你的項(xiàng)目服務(wù)了。

作者:iamsea
鏈接:http://www.itdecent.cn/p/1951239aba5e
來(lái)源:簡(jiǎn)書
簡(jiǎn)書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容