Django 2 生產環境部署 uwsgi nginx

前言

Django在開發過程當中能夠使用python3 manage.py runserver啓動web服務,可是這個服務只是用來開發調試使用的。 正常的部署狀況須要使用 uwsgi + nginx 進行服務部署。node

配置項目的settings

  • 修改Debug狀態爲 False
  • 容許接收全部hosts的方法
DEBUG = False

ALLOWED_HOSTS = ['*']
複製代碼

接下來配置uwsgipython

部署 uwsgi

安裝 uwsgi

pip3 install uwsgi
複製代碼

執行以下:nginx

[root@server01 ~]# pip3 install uwsgi
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting uwsgi
  Downloading https://mirrors.aliyun.com/pypi/packages/e7/1e/3dcca007f974fe4eb369bf1b8629d5e342bb3055e2001b2e5340aaefae7a/uwsgi-2.0.18.tar.gz (801kB)
     |████████████████████████████████| 808kB 5.4MB/s 
Installing collected packages: uwsgi
  Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.18
WARNING: You are using pip version 19.2.1, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@server01 ~]# 
複製代碼

下載完畢後,還須要配置一下環境變量。web

配置uwsgi執行文件的軟連接至/usr/bin

  • 搜索二進制可執行文件的所在路徑
# 搜索文件所在路徑
[root@server01 performance]# find / -name "*uwsgi*" -ls | grep python3 | grep bin
405620 14852 -rwxr-xr-x   1 root     root     15205552 Oct 14 01:15 /usr/local/python3/bin/uwsgi
# 查看可執行文件
[root@server01 performance]# ls -ll /usr/local/python3/bin/uwsgi
-rwxr-xr-x 1 root root 15205552 Oct 14 01:15 /usr/local/python3/bin/uwsgi
[root@server01 performance]# 
複製代碼
  • 設置軟連接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
複製代碼
  • 執行命令測試
[root@server01 performance]# uwsgi 
*** Starting uWSGI 2.0.18 (64bit) on [Mon Oct 14 17:44:34 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 13 October 2019 17:14:55
os: Linux-3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018
nodename: server01
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /work/performance
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7914
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
The -s/--socket option is missing and stdin is not a socket.
[root@server01 performance]# 
複製代碼

測試uwsgi啓動django服務

命令說明:

cd /your/django/path # 進入項目目錄
uwsgi --http ip:port  --file  project/wsgi.py  --static-map=/static=static
複製代碼

參數說明:django

  • --http 這個就和runserver同樣指定IP 端口
  • --file 指定項目的wsgi.py
  • --static 指定靜態文件

執行以下:

[root@server01 performance]# uwsgi --http 127.0.0.1:8001 --file performance/wsgi.py --static-map=/static=static
[uwsgi-static] added mapping for /static => static
*** Starting uWSGI 2.0.18 (64bit) on [Mon Oct 14 17:50:59 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 13 October 2019 17:14:55
os: Linux-3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018
...
複製代碼

使用測試訪問 http://127.0.0.1:8001/vim

能夠看到服務正常訪問,可是這只是測試的啓動方式。下面來看看怎麼使用 uwsgi 的配置文件方式來啓動。bash

使用uwsgi配置文件啓動django項目

  • 在django項目下,建立uwsgi.ini配置文件:
[root@server01 performance]# > uwsgi.ini
複製代碼
  • 編輯vim uwsgi.ini文件
# uwsig使用配置文件啓動
[uwsgi]
# 項目目錄
chdir=/work/performance
# 指定項目的application
module=performance.wsgi:application
# 指定sock的文件路徑 
socket=/work/performance/uwsgi.sock
# 進程個數 
workers=5
pidfile=/work/performance/uwsgi.pid
# 指定IP端口 
http=127.0.0.1:8001
# 指定靜態文件
static-map=/static=/work/performance/static
# 啓動uwsgi的用戶名和用戶組
uid=root
gid=root
# 啓用主進程
master=true
# 自動移除unix Socket和pid文件當服務中止的時候
vacuum=true
# 序列化接受的內容,若是可能的話
thunder-lock=true
# 啓用線程
enable-threads=true
# 設置自中斷時間
harakiri=30
# 設置緩衝
post-buffering=4096
# 設置日誌目錄
daemonize=/work/performance/logs/uwsgi.log
複製代碼
  • 啓動項目uwsgi --ini uwsgi.ini
[root@server01 performance]# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
[uwsgi-static] added mapping for /static => /work/performance/static
[root@server01 performance]# 
複製代碼
  • uwsgi 中止命令
[root@server01 performance]# uwsgi --stop uwsgi.pid 
[root@server01 performance]# 
[root@server01 performance]# ps -ef | grep uwsgi
root      1073 31902  0 19:58 pts/8    00:00:00 grep --color=auto uwsgi
[root@server01 performance]# 
複製代碼
  • 測試訪問頁面

成功訪問服務了。app

  • 查看uwsgi日誌
[root@server01 performance]# cd logs/
[root@server01 logs]# ls
celeryd.log  uwsgi.log
[root@server01 logs]# tail -f uwsgi.log 
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x24b5a90 pid: 29508 (default app)
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 29508)
spawned uWSGI worker 1 (pid: 29512, cores: 1)
spawned uWSGI worker 2 (pid: 29513, cores: 1)
spawned uWSGI worker 3 (pid: 29514, cores: 1)
spawned uWSGI worker 4 (pid: 29515, cores: 1)
spawned uWSGI worker 5 (pid: 29516, cores: 1)
spawned uWSGI http 1 (pid: 29517)
[pid: 29513|app: 0|req: 1/1] 127.0.0.1 () {40 vars in 818 bytes} [Mon Oct 14 19:11:26 2019] GET / => generated 8398 bytes in 519 msecs (HTTP/1.1 200) 6 headers in 437 bytes (1 switches on core 0)
next_url = machine_unit:machine_unit_list
[pid: 29512|app: 0|req: 1/2] 127.0.0.1 () {48 vars in 963 bytes} [Mon Oct 14 19:11:28 2019] POST / => generated 0 bytes in 671 msecs (HTTP/1.1 302) 
複製代碼

到了這裏已經部署好了 uwsgi + django 的服務了,可是uwsgi處理動態請求能力高,處理靜態文件的請求就比較差了,下一步使用nginx結合使用,靜態文件由nginx進行處理。socket

配置nginx

  • 編輯vim nginx.conf文件,增長以下配置:
server {
        listen       80;
        server_name  localhost;

        ....

        # 配置動態請求使用uwsgi
        location / {
            include uwsgi_params; # 導入nginx與uwsgi通信的模塊
            uwsgi_connect_timeout 30; # 設置鏈接uWSGI超時時間
            uwsgi_pass unix:/work/performance/uwsgi.sock; # 指定uwsgi的sock文件: 全部動態請求直接轉發
        }

        # 配置靜態文件路徑
        location /static/ {
            alias /work/performance/static/;
        }


    ...
    }
複製代碼
  • 重加載nginx服務
# 檢查nginx配置是否正確
[root@server01 conf]# nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
# 重載nginx配置
[root@server01 conf]# nginx -s reload
複製代碼
  • 測試訪問

相關文章
相關標籤/搜索