如何使用nginx & uwsgi 部署 django -2 (uwsgi + django + nginx)

在上一篇我們已經順利運用uwsgi和django
接著將繼續示範&紀錄本人進一步結合nginx的步驟

*******************************************************************
安裝並啟動nginx
$ sudo apt-get install nginx sudo /etc/init.d/nginx start
接著瀏覽localhost(127.0.0.1)
如果正確顯示Welcome to nginx!”,則表示以下環節運行無誤
the web client <-> the web server

將/etc/nginx/目錄的uwsgi_params文件複製到專案資料夾,或者自行建立檔並將以下內容複製
uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;
uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;
uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

接著在專案資料夾中建立文件mysite_nginx.conf,並填入下列內容
# mysite_nginx.conf    #藍色字為專案名稱或路徑自行更改為自己的設定
# 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 127.0.0.1 .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從文件系統中拉起media和static服務,並同時相對應django的request

接著在/etc/nginx/site-enabled目錄裡建立此文件的連結,使nginx能夠使用它
$ sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
#藍色字為檔案路徑請自行更改


接著要重啟nginx,但在重啟前先進行django的部署
在專案的settings.py中增加此行
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, "static/")  
#藍色字為資料夾名稱,可自行設定,但不可與STATIC_DIR相同

接著在專案目錄下運行:
$ python manage.py collectstatic
最後終於可以重啟nginx,看我們的設定是否生效
$ sudo /etc/init.d/nginx restart 為了測試是否成功,我們在目錄/path/to/your/project/project/media下添加文件meida.png,然後訪問http://127.0.0.1:8000/media/media.png ,如果正確顯示圖片則表示我們nginx設定成功


終於我們現在要開始將django, nginx, uwsgi三個一起運用了
一開始我們先用test.py進行小小的測試
$ uwsgi --socket :8001 --wsgi-file test.py# 參數含義 socket :8001  使用uwsgi協議, 端口為8001
在此同時nginx被設定為使用此端口與uwsgi溝通,與外界則使用8000端口進行溝通
所以我們瀏覽127.0.0.1:8000,如果跟之前一樣正確顯示Hello World,則表示以下環節正確運行
the web client <-> the web server <-> the socket <-> uWSGI <-> Python

用UNIX socket取代TCP port
編輯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 更新設定

接著我們運行django專案進行測試
$ uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664

如果發現沒有運行成功
那我們查看一下nginx error log(/var/log/nginx/error.log)
如果你看到類似下列的log紀錄:
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission denied)
那麼可能代表你需要管理socket的權限以便nginx使用
我們試著將指令改為:
$ uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666
or:
$ uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
如果運行成功那就代表我們已經成功將django, nginx, uwsgi三個統合運用在一起了!

如果覺得每次都要輸入那麼參數設定很麻煩,那麼在這裡補充一個比較方便的方法
我在使用.ini檔來簡化那些指令
在專案資料夾目錄下建立文件mysite_uwsgi.ini,填入並修改下面内容:

# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings # the base directory (full path)專案目錄
chdir           = /path/to/your/project
# Django's wsgi file (django的wsgi檔案)
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檔案位置)
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 664  #或者666
# clear environment on exit
vacuum          = true

現在只要執行下列命令,就能輕鬆運行django專案了:
$ uwsgi --ini mysite_uwsgi.ini參數含義  ini:使用特定.ini檔案

補充:
設定開機自啟
編輯文件/etc/rc.local,在"exit 0"前添加下列內容:
/usr/local/bin/uwsgi --ini  /path/to/your/project/mysite_uwsgi.ini

差不多主要流程就是這樣  過程中我也是有遇到不少問題啦  統一打在下一篇整理ˊˋ

留言

這個網誌中的熱門文章