uWSGI + Nginx + Django 部署

1. uWSGI 服務器

Django 默認使用 WSGI(Python Web Server Gateway )
做爲 Web 服務器,通常僅用來做爲測試使用,實際生產環境而是使用 uWSGI 和 Nginx 做爲服務器。javascript

uWSGI 代碼徹底用C編寫,效率高、性能穩定,可是處理 靜態文件能力較弱,所以實際生產環境中,通常採用 uWSGI + Nginx 二者搭配使用:php

  • uWSGI:處理動態請求(css、js、圖片文件等)
  • Nginx:處理靜態文件請求(提交表單、mysql、動態渲染 html)

安裝:css

pip3 install uWSGI

settings.py 配置html

設置 ALLOWED_HOSTS前端

ALLOWED_HOSTS = [
    # 加上本機的IP地址
    '192.168.xx.xxx',
    '127.0.0.1', 
    'localhost'
]

也能夠這樣設置:java

ALLOWED_HOSTS = ['*']

1.1 經過參數啓動 uWSGI

# 請更換成你服務器 ip 和端口,其中 Celery_Test/wsgi.py 爲Django 項目中自帶的 wsgi web 服務
hj@hj:~/桌面/Celery_Test$ uwsgi --http 192.168.21.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static

出現以下效果即是運行成功了:python

查看啓動端口:mysql

hj@hj:~/桌面/Celery_Test/script$ ps -ef|grep uwsgi

hj       17176  5231  0 15:37 pts/1    00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static
hj       17177 17176  0 15:37 pts/1    00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static
hj       17206  6554  0 15:38 pts/2    00:00:00 grep --color=auto uwsgi

訪問 192.168.21.128:8080linux

1.2 經過配置文件啓動 uWSGI

在項目根目錄建立一個名爲 uwsgi.ini 的配置文件,配置以下:nginx

[uwsgi]
# 項目目錄
chdir=/home/hj/桌面/Celery_Test/
# 指定項目的application
module=Celery_Test.wsgi:application
# 指定sock的文件路徑
socket=/home/hj/桌面/Celery_Test/script/uwsgi.sock
# 進程個數
workers=5
pidfile=/home/hj/桌面/Celery_Test/script/uwsgi.pid
# 指定IP端口
http=192.168.21.128:8080
# 指定靜態文件
static-map=/static=/home/hj/桌面/Celery_Test/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=/home/hj/桌面/Celery_Test/script/uwsgi.log

配置好以後,運行 uwsgi --ini uwsgi.ini 啓動 uwsgi,出現以下信息即表示啓動成功:

hj@hj:~/桌面/Celery_Test$ uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
[uwsgi-static] added mapping for /static => /home/hj/桌面/Celery_Test/static

查看運行狀況:

ps ajx|grep uwsgi

效果以下圖:

經常使用命令:

uwsgi --ini uwsgi.ini       # 啓動
    # 啓動時會生成兩個文件,分別爲:
    # PID文件 標識這個程序所處的狀態
    # SOCK文件  用來和其餘程序通訊的
uwsgi --stop uwsgi.pid      # 中止
uwsgi --reload uwsgi.ini    # 重置

Tips

中止時出現 signal_pidfile()/kill(): No such process [core/uwsgi.c line 1693]

緣由:當前端口進程與 uwsgi.pid 不一致,查看當前端口實際進程 ID,並修改 uwsgi.pid

# 根據端口,查看進程
hj@hj:~/桌面/Celery_Test$ sudo netstat -nap | grep 8080
tcp        0      0 192.168.21.128:8080     0.0.0.0:*               LISTEN      6943/uwsgi       


# 修改 uwsgi.pid 的值爲 6943,並再從新中止
hj@hj:~/桌面/Celery_Test$ uwsgi --stop script/uwsgi.pid

# 查看發現已經成功中止
hj@hj:~/桌面/Celery_Test$ ps ajx|grep uwsgi
 5231 14550 14549  5231 pts/1    14549 S+    1000   0:00 grep --color=auto uwsgi

Linux 中怎麼查看端口和進程號

# 加上 sudo
# 根據進程 pid 查看端口
lsof -i | grep pid

# 根據端口查看進程
lsof -i:port

# 根據進程 pid 查看端口
netstat -nap | grep pid

# 根據端口查看進程號
netstat -nap | grep port

2. Nginx 服務器

咱們知道 uWSGI 處理靜態文件請求能力比較弱,所以通常實際生產環境中以 動靜分離 的方式處理動靜請求,即 uWSGI + Nginx。

Nginx 做用還包括負載均衡、反向代理等。

2.1 Ubuntu 上安裝 Nginx

Nginx 的軟件包在 Ubuntu 默認軟件倉庫中可用。 安裝很是簡單,只需鍵入如下命令:

sudo apt update
udo apt install nginx

查看服務器版本信息:

sudo nginx -v

nginx version: nginx/1.14.0 (Ubuntu)

查看服務器狀態:

# 兩個均可以
sudo systemctl status nginx
ps -ef | grep nignx

配置防火牆

打開 80 和 443 端口容許經過防火牆:

hj@hj:~$ sudo ufw allow 'Nginx Full'
防火牆規則已更新
規則已更新(v6)

檢查是否更改:

hj@hj:~$ sudo ufw status
狀態: 激活

至                          動做          來自
-                          --          --
22                         ALLOW       Anywhere                  
4200                       ALLOW       Anywhere                  
Nginx Full                 ALLOW       Anywhere                  
22 (v6)                    ALLOW       Anywhere (v6)             
4200 (v6)                  ALLOW       Anywhere (v6)             
Nginx Full (v6)            ALLOW       Anywhere (v6)

測試安裝

訪問:http://192.168.21.128/

使用 systemctl 管理 Nginx 服務

您能夠像任何其餘 systemd 單位同樣管理 Nginx 服務:

# 中止Nginx服務
sudo systemctl stop nginx

# 再次啓動
sudo systemctl start nginx

# 從新啓動Nginx服務:
sudo systemctl restart nginx

# 在進行一些配置更改後從新加載 Nginx 服務:
$sudo systemctl reload nginx

# 若是你想禁用Nginx服務在啓動時啓動:
$sudo systemctl disable nginx

# 並從新啓用它:
$sudo systemctl enable nginx

參考連接:如何在Ubuntu 18.04上安裝Nginx

2.2 CentOS 上安裝

以CentOS6.x 系統爲例

  1. 更換源:
# 備份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

# 更換成國內源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

# 生成緩存
yum makecache
  1. 安裝 Nginx:
yum -y install nginx

2.3 與 uWSGI 結合使用部署 Django

爲 Nginx 添加配置文件,Ngnix 默認配置文件加載是在 /etc/nginx/conf.d/ 目錄下,新建一個配置文件(名字隨意),編輯以下:

# 新建到配置文件 conf.d
vim /etc/nginx/conf.d/

# 編輯配置文件
server {            # 開始配置了
        listen 80;   # 監聽端口
        server_name 10.129.xxx.183 ;  # 你訪問的路徑前面的 url名稱
        access_log  /var/log/nginx/access.log  main;  # Nginx日誌配置
        charset  utf-8; # Nginx編碼
        gzip on;  # 啓用壓縮,這個的做用就是給用戶一個網頁,好比3M壓縮後1M這樣傳輸速度就會提升不少
        gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;  # 支持壓縮的類型

        error_page  404           /404.html;  # 錯誤頁面
        error_page   500 502 503 504  /50x.html;  # 錯誤頁面

        # 指定項目路徑 uwsgi
        location / {        # 相似於 Django的 url(r'^admin/', admin.site.urls),
            include uwsgi_params;  # 導入一個Nginx模塊他是用來和uWSGI進行通信的
            uwsgi_connect_timeout 30;  # 設置鏈接uWSGI超時時間
            uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock;  # 指定uwsgi的sock文件全部動態請求就會直接丟給他
        }

        # 指定靜態文件路徑
        location /static/ {
            alias  /opt/project_teacher/teacher/static/;
            index  index.html index.htm;
        }

        }

參考:

server {            
        listen 80;   
        server_name 192.168.xx.128 ;  
        access_log  /var/log/nginx/access.log  main;  
        charset  utf-8; 
        gzip on;  
        gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;  
        error_page  404           /404.html;
        error_page   500 502 503 504  /50x.html;  

       
        location / {        
            include uwsgi_params;  
            uwsgi_connect_timeout 30;  
            uwsgi_pass unix:/home/hj/桌面/Celery_Test/script/uwsgi.sock;  
        }

       
        location /static/ {
            alias  /home/hj/桌面/Celery_Test/static/;
        index index.html index.htm

啓動 Nginx 服務 /etc/init.d/nginx start,訪問:http://192.168.21.128:8080/app/index/,效果以下圖:

經常使用命令:

/etc/init.d/nginx start     # 啓動
/etc/init.d/nginx stop      # 關閉

#  Nginx配置是重啓才生效,若修改配置不知道是否對不對,能夠用來測試
/etc/init.d/nginx configtest

# 生產環境直接 reload就好了,不要 stop start 或者 restart
/etc/init.d/nginx reload

配置 Django 靜態文件

admin 所需的靜態文件都在 Django 的安裝內,咱們沒有配置指向 Django 的配置文件。

解決辦法:

  1. 設置 settings.py ,將全部靜態文件收集到 static_all 中:
# settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static_all")

# 收集成功提示(僅供參考)
120 static files copied to '/home/hj/桌面/Celery_Test/static_all'.
  1. 收集靜態文件到 static_all 這個目錄中(可不建立 static_all):
python3 manage.py collectstatic --noinput
  1. 修改 /etc/nginx/conf.d/test.ini 中靜態文件路徑:
alias  /home/hj/桌面/Celery_Test/static_all;

指定其餘地方放置靜態文件

# 新建目錄
sudo mkdir -vp /var/www/test2/static/

# 賦予權限
sudo chmod 777 /var/www/test2/static/

# 修改項目中 settings.py,指定靜態文件路徑
STATIC_ROOT = '/var/www/test2/static/'
STATIC_URL = '/static/'

# 收集靜態文件到 /var/www/test2/static/ 中
python3 manage.py collectstatic

# 輸入 yes,開始收集,從新加載 Nginx 服務
相關文章
相關標籤/搜索