close

【Linux】Ubuntu Django2.0 Nginx uWSGI 部署與錯誤處理

環境配置:

  • Ubuntu 18.04.1 LTS
  • Python 3.6
  • Django2.0
  • nginx 1.14.0
  • uwsgi  2.0.17.1

 

前言:

首先要了解部署的整體線路,並且一部份一部份的相連與測試

最後才能完整的將網站部署完成

每個環節請一定要進行測試,確保沒有問題再進行下一步

不然最後發生問題而重新排查花的時間可是成倍的翻哦 

最終整體線路:

the web client <-> the web server <-> the socket <-> uWSGI <-> Django

 

uwsgi部署

開啟虛擬環境,切換到專案目錄

由於我的專案使用Django2.0 、celery4.2 ,所以必須要使用python3環境安裝uwsgi以免錯誤

apt-get install build-essential python-dev

apt-get install python3.6-dev

pip3 install uwsgi

※python-dev 或python-devel 稱為是python 的開發包,其中包括了一些用C/Java/C# 等編寫的python 擴展在編譯的時候依賴的頭文件等信息

安裝完後先進行測試

線路:the web client <-> uWSGI <-> Python

在虛擬環境下Django專案目錄創建test.py檔案

輸入以下內容:   

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2

在虛擬環境下的終端機輸入以下指令:  

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

打開瀏覽器,在路徑上輸入:127.0.0.1:8000

這時候是uwsgi直接部署python文件

如果有成功看到hello world就代表uwsgi成功運作了

線路:the web client <-> uWSGI <-> Django

現在要返回一個Django站點而非一個python檔案

首先測試Django專案是否程正確運行 

python manage.py runserver 0.0.0.0:8000

確認可以正常訪問後,使用 uwsgi運行它

uwsgi --http :8000 --module mysite.wsgi

此時為 uwsgi直接部署Django專案

如果訪問結果正確,通常這裡是不會有css樣式的

 

配置nginx

由於ubuntu預設開機自動啟動apache2

所以需要先停止apache2才能安裝或更新ngnix

service apache2 stop

安裝nginx

sudo apt-get update

sudo apt-get install nginx

如果安裝有問題要重新安裝可以參考這篇

這邊我安裝了非常的久,怎麼都只跑出apache2 works! 

我認為是學習了錯誤的步驟,使用非官方源安裝nginx的緣故

除了把apache2完全移除,也重裝了nginx才終於正常

請使用下列命令啟動nginx: 

sudo /etc/init.d/nginx start # start nginx

停止與重啟語法如下:

sudo /etc/init.d/nginx stop # stop nginx

sudo /etc/init.d/nginx restart # restart nginx

線路:the web client <-> the web server

安裝好nginx並啟動後,可以在瀏覽器透過80端口訪問

出現 welcome to  nginx 頁面,代表這條線路是正確運作的

為Django站點配置nginx

接著將nginx與uwsgi連接在一起

首先在uwsgi的目錄下找到uwsgi_params文件(我的路徑為 /etc/nginx/ ) 

將該文件拷貝至Django專案目錄下

接著在Django目錄底下創建mysite_nginx.conf文件,並寫入以下內容:   

# mysite_nginx.conf
#改專案名稱、server_name與專案路徑
# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

接著將此檔連接至nginx中的sites-enabled目錄中

sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

部署靜態文件

在settings.py中添加以下這行代碼:   

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

然後運行:

python manage.py collectstatic

重啟 nginx:

sudo /etc/init.d/nginx restart

線路測試

添加一個 media.png的圖片到 /path/to/your/project/project/media

http://example.com:8000/media/media.png 如果正常運作,代表nginx正在正確的提供文件服務

線路:the web client <-> the web server <-> the socket <-> uWSGI <-> Python

nginx和uWSGI以及test.py

在虛擬環境Django目錄下運行,將透過uwsgi再傳給nginx 

uwsgi --socket :8001 --wsgi-file test.py

由於nginx對外使用8000端口,因此對此進行訪問

http://example.com:8000/   (127.0.0.1:8000)

如果能順利運轉,就接著使用Unix socket而不是端口

目前,我們使用了一個TCP端口socket,因為它簡單些,但事實上,使用Unix socket會比端口更好- 開銷更少

編輯以下文件內容,改為unix與專案路徑:   

# mysite_nginx.conf

    server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)

重啟 nginx:

sudo /etc/init.d/nginx restart

再次運行uwsgi,使用socket選項告訴uwsgi使用哪個文件

uwsgi --socket mysite.sock --wsgi-file test.py

如果無法正確訪問,可能出現如下訊息 :

connect () to unix:///path/to/your/mysite/mysite.sock failed ( 13: Permission denied )

則可能是權限問題,嘗試以下兩個方式:

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666

可能必須添加你的用戶到nginx的組(www-data),反之亦然,這樣,nginx可以正確地讀取或寫入你的socket。

即可成功訪問

 

線路:the web client <-> the web server <-> the socket <-> uWSGI <-> Django

使用 --socket 運行Django應用

uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket = 666

如果運行成功,代表可以正確部署Django了 

但只有這樣還不夠,我們可以將用在uWSGI上的相同的選項放到一個文件中,然後告訴uWSGI使用該文件運行。

配置uWSGI.ini文件:

在Django專案目錄創建一個uwsgi.ini檔案,並輸入以下內容:     

# mysite_uwsgi.ini file 
#請修改專案目錄路徑、虛擬機路徑、wsgi文件名稱、socket路徑
[uwsgi]

# Django-related settings 
# the base directory (full path) 
chdir            = /path/to/your/project
 # Django's wsgi file 
module           = project.wsgi
 # the virtualenv (full path) 
home             = /path/to/virtualenv

# process-related settings 
# master 
master           =  true 
# maximum number of worker processes 
processes        = 10
 # the socket (use the full path to be safe 
socket           = /path/to/your/project/mysite.sock
 # ... with appropriate permissions - may be needed 
chmod-socket = 666
# clear environment on exit 
vacuum           =  true

接著在虛擬環境下指令即可運行:  

uwsgi --ini mysite_uwsgi.ini 

即可成功運行

 

 

參考文件:

Setting up Django and your web server with uWSGI and nginx

使用uWSGI和nginx来设置Django和你的web服务器

django+nginx+uwsgi进行服务器部署

Ubuntu上通过nginx部署Django笔记

ubuntu14.04彻底删除nginx

arrow
arrow
    全站熱搜

    ivankao 發表在 痞客邦 留言(0) 人氣()