Ubuntu上通過nginx部署Django筆記


Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比較常見的一種方式。今天在Ubuntu上使用Nginx部署Django服務,雖然不是第一次搞這個了,但是發現還是跳進了好多坑,google了好久才搞定。想想還是把這個過程記錄下來,免得下次再來踩同樣的坑。

安裝Nginx

apt-get install nginx

ubantu安裝完Nginx后,文件結構大致為:
  所有的配置文件都在 /etc/nginx下;
  啟動程序文件在 /usr/sbin/nginx下;
  日志文件在 /var/log/nginx/下,分別是access.log和error.log;
  並且在 /etc/init.d下創建了啟動腳本nginx。

sudo /etc/init.d/nginx start    # 啟動
sudo /etc/init.d/nginx stop     # 停止
sudo /etc/init.d/nginx restart  # 重啟

安裝uwsgi

apt-get install python-dev
pip install uwsgi

至於為什么要使用uwsgi,可以參見這邊博客:快速部署Python應用:Nginx+uWSGI配置詳解(1)
這樣大體的流程是:nginx作為服務器最前端,負責接收client的所有請求,統一管理。靜態請求由Nginx自己處理。非靜態請求通過uwsgi傳遞給Django,由Django來進行處理,從而完成一次WEB請求。
通信原理是:
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django

測試uwsgi

在Django項目下新建test.py文件,

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

然后執行shell命令:

uwsgi --http :8001 --plugin python --wsgi-file test.py

加上--plugin python是告訴uWSGI在使用python插件,不然很有可能會出現類似這樣的錯誤:

uwsgi: unrecognized option '--wsgi-file'
getopt_long() error

執行成功在瀏覽器中打開:http://localhost:8001顯示Hello World說明uwsgi正常運行。

測試Django

首先得保證Django項目沒有問題

python manage.py runserver 0.0.0.0:8001

訪問http://localhost:8001,項目運行正常。
然后鏈接Django和uwsgi,實現簡單的web服務器,到Django項目目錄下執行shell:

uwsgi --http :8001 --plugin python --module blog.wsgi

blog為你的項目名。訪問http://localhost:8001,項目正常。注意這時項目的靜態文件是不會被加載的,需要用nginx做靜態文件代理。

配置uwsgi

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

[uwsgi]
# Django-related settings

socket = :8001

# the base directory (full path)
chdir           = /home/ubuntu/blog

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

daemonize      = /var/log/web_blog.log

在shell中執行:

sudo uwsgi --ini uwsgi.ini 

ps:如果實在不想配置nginx的話,單uwsgi就已經能完成部署了(把socket換成http),你可以把Django中的靜態文件放到雲平台中如七牛等等,這樣你的Web也能被正常訪問。

配置nginx

nginx默認會讀取/etc/nginx/sites-enabled/default文件中的配置,修改其配置如下:

server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name 127.0.0.1; # 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 /home/ubuntu/blog/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/ubuntu/blog/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        include     uwsgi_params; # the uwsgi_params file you installed
        uwsgi_pass 127.0.0.1:8001;
    }
}

收集Django靜態文件

把Django自帶的靜態文件收集到同一個static中,不然訪問Django的admin頁面會找不到靜態文件。在django的setting文件中,添加下面一行內容:

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

然后到項目目錄下執行:

python manage.py collectstatic

修改配置文件

DEBUG = False
ALLOWED_HOSTS = ['*']

運行

一切配置好后直接重啟nginx即可。更加詳細的說明請參見官方文檔

可能遇到的問題

如果監聽80端口,部署后訪問localhost自動跳轉到nginx默認的歡迎界面
uwsgi: option ‘--http‘ is ambiguous


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM