1. 程式人生 > 實用技巧 >Uwsgi+Nginx的 前後端 部署

Uwsgi+Nginx的 前後端 部署

1.setting.py 配置

# 1.修改配置
# 正式上線關閉除錯模式, 不會暴露伺服器資訊
DEBUG = False #Ture

# 2.允許的主機
ALLOWED_HOSTS = ['xxx.com','127.0.0.1']

# 3.前端修改介面地址
http://127.0.0.1:8000/   =>  http://xxx.com/

# 4.MySQL資料庫根據實際情況配置(注意地址)

# 5.快取配置, 改為預設, 最簡單形式(配置redis)

# 6.收集靜態檔案
# 靜態資源收集位置
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# 7.命令列執行 python manage.py collectstatic
View Code

2.安裝uwsgi 配置uWSGI

'''1. 安裝uwsgi'''
[root@linux-node1 /]# workon syl
[root@linux-node1 /]# pip3 install uwsgi # 安裝uwsgi
[root@linux-node1 /]# whereis uwsgi # 檢視uwsgi安裝路徑
uwsgi: /root/.virtualenvs/syl/bin/uwsgi

2.1 安裝uwsgi

'''1. 安裝uwsgi'''
[root@linux
-node1 /]# workon syl [root@linux-node1 /]# pip3 install uwsgi # 安裝uwsgi [root@linux-node1 /]# whereis uwsgi # 檢視uwsgi安裝路徑 uwsgi: /root/.virtualenvs/syl/bin/uwsgi

2.2 配置uwgsi.ini啟動檔案

[root@linux-node1 /]# vim uwsgi_conf/uwsgi.ini
[uwsgi]
# 使用Nginx連線時使用,Django程式所在伺服器地址和埠號
socket=127.0.0.1:8000
# 專案目錄絕對路徑
chdir=/teach/shiyanlou_project/syl # 專案中wsgi.py檔案的目錄,相對於專案目錄 wsgi-file=syl/wsgi.py # 程序數(機器核數的1倍) processes=4 # 執行緒數 threads=20 # uwsgi伺服器的角色 master=True # 存放程序編號的檔案 pidfile=uwsgi.pid # 日誌檔案 daemonize=uwsgi.log # 指定依賴的虛擬環境 virtualenv=/root/.virtualenvs/syl
View Code

2.3 使用uwsgi啟動django:一定要在這個專案目錄中

'''3. 使用uwsgi啟動django:一定要在這個專案目錄中'''
[root@linux-node1 /]# uwsgi --http 192.168.56.100:6666 --file syl/wsgi.py --static-map=/static=static
訪問專案:http://192.168.56.11

3.安裝配置nginx

3.1 安裝nginx

[root@linux-node1 /]# sudo apt update                  # 更新apt
[root@linux-node1 /]# sudo apt install nginx           # 安裝nginx
[root@linux-node1 /]# sudo systemctl status nginx      # 檢視nginx狀態

3.2 配置nginx+uwsgi啟動

# /etc/nginx/conf.d/django.conf
server {
    listen       8888;
    server_name  192.168.56.100;
    
        location /static {
            alias /teach/loonflow_project/syl/static;
        }
    
        location / {
              include uwsgi_params;
              uwsgi_pass 127.0.0.1:8000;
              uwsgi_ignore_client_abort on;
        }
}

#  前端
server {
        listen 9000;
        server_name 192.168.56.100;
        #access_log logs/access_example.log  main;
        root /P8/loonflow_project/loonflow/static/dist;
        location / {
                try_files $uri $uri/ @router;
        }
        location @router {
                rewrite ^.*$ /index.html last;
        }
}
View Code

3.3 /etc/nginx/nginx.confnginx主配置檔案解讀

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;      # 連結數量
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    
    access_log /var/log/nginx/access.log;         # 客戶瀏覽器訪問nginx服務記錄(客戶戶端訪問異常時可以檢視)
    error_log /var/log/nginx/error.log;           # nginx錯誤日誌(nginx啟動時報錯的日誌)
    include /etc/nginx/conf.d/*.conf;             # nginx擴充套件配置檔案件
    include /etc/nginx/sites-enabled/*;
}
View Code

4. uwsgi和nginx 啟動、關閉、檢視日誌

'''1.啟動並檢視nginx日誌'''
[root@linux-node1 demo2]# systemctl restart nginx # 開啟nginx
root@dev:uwsgi_conf# tail -f /var/log/nginx/access.log # 檢視nginx接入日誌
root@dev:uwsgi_conf# tail -f /var/log/nginx/error.log # 檢視nginx錯誤日誌​
'''2.啟動並檢視uwsgi日誌'''
root@dev:uwsgi_conf# cd /teach/shiyanlou_project/uwsgi_conf # 進入實驗樓目錄
[root@linux-node1 demo2]# uwsgi --ini uwsgi.ini # 啟動uwsgi的django專案
# http://192.168.56.11:8888/ 訪問專案
[root@linux-node1 demo2]# uwsgi --stop uwsgi.pid # 關閉uwsgi
[root@dev:uwsgi_conf# tail -f uwsgi.log # 檢視uwsgi日誌