如何將Django項目部署到Linux雲服務器?

本文操作環境: Debian 9,lnmp1.6,Django2.1.3,Python3.6.0

主要參考了文檔 查看

1. 安裝 Python3.6.0

由於Django2.1僅支持Python3.5以上的版本,所以需要安裝Python3版本。如果直接安裝Python3版本,使用時需要改變全局默認Python版本,還需要重新配置環境變量,所以推薦一個叫pyenv的Python多版本管理工具,它構建出一個Python虛擬環境,可以在多個Python版本間隨意切換。

1.1 安裝pyenv(首先創建~/.pyenv目錄)

官方給出了安裝教程 https://github.com/pyenv/pyenv#installation

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
# 注意,文件名不一定叫.bash_profile,有可能是.bashrc

1.2 安裝完成之後下載Python3.6.0版本

使用命令:

pyenv install 3.6.0

這時pyenv會自動下載並將其放入版本集合裏,如果想全局使用Python3.6.0,只需要執行:

pyenv global 3.6.0

2. 安裝 Django 2.1.3

pip install Django==2.1.3

我根據自己的需要,還安裝了一些其他庫

pip install requests
pip install mysqlclient

3. 安裝 uWSGI

由於Django並不推薦在其自帶的開發環境下的WSGI模塊運行,所以還需要安裝uWSGI

參考:如何用 uWSGI 託管 Django

pip install uwsgi

完整的調用關係應該是這樣的:

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

這裏uWSGI大致起到一箇中間件的作用,它實現了Django的WSGI協議。客戶端請求由Nginx處理分發到uwsgi,uwsgi再將請求交給Django處理,Django處理完成之後將結果原路返回,最後由Nginx返回數據給客戶端。

4. 創建一個虛擬主機,修改網站配置文件

由於我的服務器上放了不止一個站點,所以需要創建虛擬主機,當然不創建也可以,無非就是訪問的時候主域名 + 端口號

我使用的是lnmp一鍵安裝腳本安裝的Nginx,所以創建虛擬主機完全命令化:

lnmp vhost add

接下來會提示你配置一些關鍵信息

如果想要支持https,選擇添加SSL安全認證時選用Let’s Encript

Add SSL Certificate (y/n) y
1: Use your own SSL Certificate and Key
2: Use Let's Encrypt to create SSL Certificate and Key

uWSGI 文檔提供了一個模板

# 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)
    # 大概就是一次轉發,與真正的 Django 應用通信
    # 二選一,我選用的unix套接字方式而不是端口
}

# 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 項目下的一些靜態資源,靜態資源的話就可以直接交給nginx發送
    
    # 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
        # 使用 manage.py 執行 collectstatic 命令可以整理出來
    }

    # 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的conf目錄下,我把這個文件複製到了我項目下
    }
}

如果配置了https,那麼這個文件下會看到有兩個server模塊,整體結構不用改變,參考上面文件 server 中的內容填到自己的配置文件中就好了。

5. 編寫uWSGI配置文件

這裏我給出一個示例

# book.ini
[uwsgi]

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

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe

# 這裏還是一樣,可以使用端口號後者socket,但是注意要與Nginx配置文件保持一致

#http     = :8000
socket          = /home/wwwroot/shuchong1/shuchong1.sock

# ... with appropriate permissions - may be needed
chmod-socket    = 666 # 給 socket 添加權限
# clear environment on exit
vacuum          = true
enable-threads = true # 允許多線程
touch-reload = /home/wwwroot/shuchong1 
# 熱重載配置,意思就是對應目錄下的文件有更改,不用手動重啓uWSGI

事實上,這個時候如果執行命令 uwsgi --ini book.ini 項目可以啓動了

6. 最後一步,讓uWSGI開機自啓

uWSGI有一個emperor模式,這種模式下會自動捕捉配置文件的變化,生成新的uWSGI實例

uWSGI can run in ‘emperor’ mode. In this mode it keeps an eye on a directory of uWSGI config files, and will spawn instances (‘vassals’) for each one it finds.

# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals

vassals目錄下可以放置多個配置文件,uWSGI啓動時將全部加載

ps:如果實在不清楚uid, gid的話寫root也不是不可以

[emperor] is the uwsgi binary in your system PATH ?
TIME STAMP - [emperor] curse the uwsgi instance cc_uwsgi.ini (pid: ####)
TIME STAMP - [emperor] removed uwsgi instance cc_uwsgi.ini

2019/10/30: 使用www遇到權限問題,建議root就完事了

以守護進程的方式執行

uwsgi --emperor /etc/uwsgi/vassals --uid root --gid root --daemonize /var/log/uwsgi-emperor.log

最後一步,編寫腳本,讓uWSGI開機自啓

官方推薦的做法是新建文件 /etc/rc.local,把上面這行命令放進去

不過好像不是很管用,開機時並不會自啓動,還要手動執行一遍

/etc/rc.local

所以最後我在~/.bashrc下添加了執行命令 /etc/rc.local,這樣就做到開機自啓了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章