歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 通過Nginx部署Django(基於ubuntu)

通過Nginx部署Django(基於ubuntu)

日期:2017/3/3 11:49:37   编辑:Linux技術


通過Nginx部署Django(基於ubuntu)

2016-03-12 14:21 by 蟲師, 1715 閱讀, 0 評論, 收藏, 編輯
  Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比較常見的一種方式。
  在這種方式中,我們的通常做法是,將nginx作為服務器最前端,它將接收WEB的所有請求,統一管理請求。nginx把所有靜態請求自己來處理(這是NGINX的強項)。然後,NGINX將所有非靜態請求通過uwsgi傳遞給Django,由Django來進行處理,從而完成一次WEB請求。
  可見,uwsgi的作用就類似一個橋接器。起到橋梁的作用。
  Linux的強項是用來做服務器,所以,下面的整個部署過程我們選擇在Ubuntu下完成。

一、安裝Nginx

  Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特點是占有內存少,並發能力強,事實上nginx的並發能力確實在同類型的網頁服務器中表現較好。
  Nginx同樣為當前非常流行的web服務器。利用其部署Django,我們在此也做簡單的介紹。
  Nginx官網: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  #關閉
fnngj@ubuntu:~$ /etc/init.d/nginx restart  #重啟

  修改Nginx默認端口號,打開/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行的位置,將默認的80端口號改成其它端口號,如 8088。因為默認的80端口號很容易被其它應用程序占用。
 然後,通過上面命令重啟nginx。訪問:http://127.0.0.1:8088/
  

  如果出現如上圖,說明Nginx啟動成功。

二、安裝uwsgi

通過pip安裝uwsgi。
root@ubuntu:/etc# python3 -m pip install uwsgi

測試uwsgi,創建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
fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --chdir /home/fnngj/pydj/myweb/ --wsgi-file myweb/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

常用選項:
http : 協議類型和端口號
processes : 開啟的進程數量
workers : 開啟的進程數量,等同於processes(官網的說法是spawn the specified number ofworkers / processes)
chdir : 指定運行目錄(chdir to specified directory before apps loading)
wsgi-file : 載入wsgi-file(load .wsgi file)
stats : 在指定的地址上,開啟狀態服務(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服務器(daemonize uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。
pidfile : 指定pid文件的位置,記錄主進程的pid號。
vacuum : 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)

三、Nginx+uwsgi+Django

  接下來,我們要將三者結合起來。首先羅列一下項目的所需要的文件:
myweb/
├── manage.py
├── myweb/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── myweb_uwsgi.ini
  在我們通過Django創建myweb項目時,在子目錄myweb下已經幫我們生成的 wsgi.py文件。所以,我們只需要再創建myweb_uwsgi.ini配置文件即可,當然,uwsgi支持多種類型的配置文件,如xml,ini等。此處,使用ini類型的配置。

# myweb_uwsgi.ini file
[uwsgi]

# Django-related settings

socket = :8000

# the base directory (full path)
chdir           = /home/fnngj/pydj/myweb

# Django s wsgi file
module          = myweb.wsgi

# process-related settings
# master
master          = true

# maximum number of worker processes
processes       = 4

# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true


  這個配置,其實就相當於在上一小節中通過wsgi命令,後面跟一堆參數的方式,給文件化了。
  socket 指定項目執行的端口號。
  chdir 指定項目的目錄。
  module myweb.wsgi ,可以這麼來理解,對於myweb_uwsgi.ini文件來說,與它的平級的有一個myweb目錄,這個目錄下有一個wsgi.py文件。
  其它幾個參數,可以參考上一小節中參數的介紹。
  接下來,切換到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:41
os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2015
nodename: ubuntu
machine: i686
clock source: unix
detected number of CPU cores: 2
current working directory: /home/fnngj/pydj/myweb
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/fnngj/pydj/myweb
your processes number limit is 15962
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
Python 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 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的啟動信息,如果有錯,就要檢查配置文件的參數是否設置有誤。
  再接下來要做的就是修改nginx.conf配置文件。打開/etc/nginx/nginx.conf文件,添加如下內容。

……
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 網上大多資料都是設置的一個網址(例,www.example.com),我這裡如果設置成網址無法訪問,所以,指定的到了本機默認ip。
  在進行配置的時候,我有個問題一直想不通。nginx到底是如何uwsgi產生關聯。現在看來大概最主要的就是這兩行配置。
  include uwsgi_params;
  uwsgi_pass 127.0.0.1:8000;
  include 必須指定為uwsgi_params;而uwsgi_pass指的本機IP的端口與myweb_uwsgi.ini配置文件中的必須一直。
  現在重新啟動nginx,翻看上面重啟動nginx的命令。然後,訪問:http://127.0.0.1:8099/
  通過這個IP和端口號的指向,請求應該是先到nginx的。如果你在頁面上執行一些請求,就會看到,這些請求最終會轉到uwsgi來處理。
  

=============
ps: 這個過程本應不算復雜,之前花兩天時間沒搞定,索性放到了一邊,這次又花了兩天時間才算搞定。網上搜到的文章比較亂,有些太簡單的看不懂,有些又太啰嗦的不知道核心的幾步是什麼,希望本文能幫到你。
Copyright © Linux教程網 All Rights Reserved