Python 關於在ubuntu部署Django項目

來源:互聯網
上載者:User

標籤:cloc   com   call   mod   tin   site   mount   auto   esc   

  Django的部署可以有很多方式,採用nginx+uwsgi的方式是其中比較常見的一種方式。

  在這種方式中,我們的通常做法是,將nginx作為伺服器最前端,它將接收WEB的所有請求,統一管理請求。nginx把所有靜態請求自己來處理(這是NGINX的強項)。然後,NGINX將所有非靜態請求通過uwsgi傳遞給Django,由Django來進行處理,從而完成一次WEB請求。

  可見,uwsgi的作用就類似一個橋接器。起到橋樑的作用。

  Linux的強項是用來做伺服器,所以,下面的整個部署過程我們選擇在Ubuntu下完成。

 

一、安裝Nginx

  Nginx是一款輕量級的Web 伺服器/反向 Proxy伺服器及電子郵件(IMAP/POP3)Proxy 伺服器,並在一個BSD-like 協議下發行。其特點是佔有記憶體少,並發能力強,事實上nginx的並發能力確實在同類型的網頁伺服器中表現較好。

   Nginx同樣為當前非常流行的web伺服器。利用其部署Django,我們在此也做簡單的介紹。

[email protected]:/$ sudo apt-get install nginx  # 安裝nginx

 

  啟動nginx:

[email protected]:~$ /etc/init.d/nginx start [email protected]:~$ /etc/init.d/nginx stop[email protected]:~$ /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
[email protected]:~$ pip3 install uwsgi

測試uwsgi,建立test.py檔案:

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

通過uwsgi運行該檔案

[email protected]:~$ uwsgi --http:8001 --wsgi-file test.py

 

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

 

uwsgi支援通過設定檔的方式啟動,可以接受更多的參數,高度可定製。我們在Django項目的同名目錄下建立ini檔案default_app_uwsgi.ini

# Django-related settingssocket = :8001# the base directory (full path)chdir           = /usr/app/DataAnalysis# Django s wsgi filemodule          = /DataAnalysis.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

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

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

 

[email protected]:/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:22os: Linux-4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48 UTC 2017nodename: WUZLX004machine: x86_64clock source: unixdetected number of CPU cores: 8current working directory: /usr/app/DataAnalysis/DataAnalysisdetected binary path: /home/wuzlxadmin/.local/bin/uwsgi!!! no internal routing support, rebuild with pcre support !!!chdir() to /usr/app/DataAnalysisyour processes number limit is 15706your 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 :8001 fd 3Python 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 0x1d6b0d0your server socket listen backlog is limited to 100 connectionsyour mercy for graceful operations on workers is 60 secondsmapped 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項目

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.