1. 程式人生 > >Python 關於在ubuntu部署Django項目

Python 關於在ubuntu部署Django項目

with 服務器 pytho bsd 選擇 time like Go tcp

  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,我們在此也做簡單的介紹。

wuzlxadmin@WUZLX004:/$ sudo apt-get install nginx  # 安裝nginx

  啟動nginx:

wuzlxadmin@WUZLX004:~$ /etc/init.d/nginx start 

wuzlxadmin@WUZLX004:~$ /etc/init.d/nginx stop

wuzlxadmin@WUZLX004:~$ /etc/init.d/nginx restart 

  配置nginx

nginx默認會讀取/etc/nginx/sites-enabled/default

文件中的配置:

server {
        listen 8080 default_server;
        listen [::]:8080 default_server;
        ...........

輸入你的ubuntu ip即可訪問, 正常是127.0.0.1:80

技術分享圖片

如果出現上圖,則說明Nginx已經安裝配置成功。

二、安裝uwsgi

wuzlxadmin@WUZLX004:~$ pip3 install uwsgi

測試uwsgi,創建test.py文件:

def application(env, start_response):
    start_response(
200 OK, [(Content-Type,text/html)]) return [b"Hello World"]

通過uwsgi運行該文件

wuzlxadmin@WUZLX004:~$ uwsgi --http:8001 --wsgi-file test.py

技術分享圖片

接下來配置Django與uswgi連接。我的Django項目位置為:

技術分享圖片

uwsgi支持通過配置文件的方式啟動,可以接受更多的參數,高度可定制。我們在Django項目的同名目錄下新建ini文件default_app_uwsgi.ini

# Django-related settings

socket = :8001

# the base directory (full path)
chdir           = /usr/app/DataAnalysis

# Django s wsgi file
module          = /DataAnalysis.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

其中chdir是django項目的根目錄、module是相對路徑指向項目同名目錄下的wsgi.py文件

接下來,切換到Django項目目錄下,通過uwsgi命令讀取default_app_uwsgi.ini文件啟動項目。

wuzlxadmin@WUZLX004:/usr/app/DataAnalysis/DataAnalysis$ uwsgi --ini default_app_uwsgi.ini
[uWSGI] getting INI configuration from default_app_uwsgi.ini
*** Starting uWSGI 2.0.17 (64bit) on [Tue Jun  5 13:42:55 2018] ***
compiled with version: 5.4.0 20160609 on 24 May 2018 07:57:22
os: Linux-4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48 UTC 2017
nodename: WUZLX004
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /usr/app/DataAnalysis/DataAnalysis
detected binary path: /home/wuzlxadmin/.local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /usr/app/DataAnalysis
your processes number limit is 15706
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 :8001 fd 3
Python version: 3.5.2 (default, Nov 23 2017, 16:37:01)  [GCC 5.4.0 20160609]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1d6b0d0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 364520 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
ImportError: No module named /DataAnalysis
unable to load app 0 (mountpoint=‘‘) (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 29756)
spawned uWSGI worker 1 (pid: 29757, cores: 1)
spawned uWSGI worker 2 (pid: 29758, cores: 1)
spawned uWSGI worker 3 (pid: 29759, cores: 1)
spawned uWSGI worker 4 (pid: 29760, cores: 1)

再接下來要做的就是修改nginx.conf配置文件。打開/etc/nginx/nginx.conf文件,添加如下內容。

server {
    listen         8081; 
    server_name    10.202.143.240 
    charset UTF-8;
    access_log      /var/log/nginx/DataAnalysis_access.log;
    error_log       /var/log/nginx/DataAnalysis_error.log;

    client_max_body_size 75M;

    location / { 
        include uwsgi_params;
        uwsgi_pass 10.202.143.240:8001;
        uwsgi_read_timeout 2;
    }   
    location /static {
        expires 30d;
        autoindex on; 
        add_header Cache-Control private;
        alias /usr/app/DataAnalysis/static/;
     }
 }

listen 指定的是nginx代理uwsgi對外的端口號。

uwsgi_pass 10.202.143.240:8001;指的本機IP的端口號與ini文件配置中的必須一致。

重啟nginx。

PS:

操作/etc/nginx/nginx.conf文件的時候,將下面這一句的註釋去掉。

server_names_hash_bucket_size 64;

另外註意一下 新加的server的位置問題:

http{
    server{}      
}

測試nginx的configuration是否ok

nginx -t -c /etc/nginx/nginx.conf

技術分享圖片

Python 關於在ubuntu部署Django項目