Django學習筆記:第十一天 專案部署
【部署】
-- django中自帶開發者伺服器
-- runserver
-- 路由處理功能,具備動態資源處理
-- 開啟debug,具有靜態資源處理功能
-- 功能健壯,效能是比較低的,僅適用於開發
-- 部署不會使用單一伺服器
-- Apache
-- Nginx
-- HTTP伺服器
-- 處理靜態資源
-- 反向代理
-- Nginx + uWSGI HTTP伺服器
-- Nginx + gunicorn HTTP伺服器
-- 郵件伺服器
-- 流媒體伺服器
【Nginx】
Nginx 安裝
-- 下載Nginx公鑰:wget http://nginx.org/keys/nginx_signing.key
-- 新增公鑰,新增源,更新源,安裝,解除安裝,檢視程序
# 新增 Nginx 公鑰(處於公鑰所在目錄) sudo apt-key add nginx_signing.key # 開啟Linux原始檔,新增源 sudo vim /etc/apt/sources.list # 將下面兩句程式碼新增到 sources.list 尾部 deb http://nginx.org/packages/ubuntu/ [你的ubuntu版本名] nginx deb-src http://nginx.org/packages/ubuntu/ [你的ubuntu版本名] nginx# 源新增好後,更新源 sudo apt update # 安裝 Nginx sudo apt install nginx # 解除安裝 Nginx sudo apt remove nginx # 檢視 Nginx 程序狀態 ps -ef|grep nginx
Nginx 控制
報錯:nginx: [alert] could not open error log file...... 解決:命令前新增 sudo 許可權 # 啟動 Nginx nginx [-c configpath] # 資訊檢視 nginx -v # 顯示版本資訊 nginx -V # 顯示所有配置資訊# 控制 Nginx nginx -s stop 快速關閉 quit 優雅的關閉 reload 重新載入配置,原來的 # 通過系統管理 systemctl status nginx # 檢視nginx狀態 systemctl start nginx # 啟動nginx服務 systemctl stop nginx # 關閉nginx服務 systemctl enable nginx # 設定開機自啟 systemctl disable nginx # 禁止開機自啟
Nginx 配置(核心)
-- Nginx配置檔案是包含<指定指令控制>的模組。
-- 指令分為<簡單指令>和<塊指令>
-- 一個簡單指令由名稱和引數組成,以空格分隔,並以分號結尾
-- 塊指令和簡單指令有相同的結構,但不是以分號結束,而是以一個大括號包含的一堆附加指令結束
-- 如果一個大括號內可以有其他的指令,它就被稱為一個上下文,比如(events,http,server,location)
# 開啟Nginx配置檔案 cd /etx/nginx vim nginx.conf
nginx -t # 不執行,僅測試配置檔案 nginx -c configpath # 從指定路徑載入配置檔案 nginx -t -c configpath # 測試指定配置檔案,使用絕對路徑
Nginx 配置檔案結構
main # 全域性設定 events{ # 工作模式,連線配置 ... } http{ # http的配置 ... upstream xxx{ # 負載均衡配置 ... } server{ # 主機設定 ... location xxx{ # URL匹配 ... } } } user nginx; # worker程序執行的使用者和組 worker_processes 1; # 指定Nginx開啟的子程序數,多核CPU建議設定和CPU數量一樣的程序數 error_log xxx level; # 用來定義全域性錯誤日誌檔案,通常放在var中, # level有 debug,info,notice,warn,error,crit pid xxx; 指定程序id的儲存檔案位置
# events,指定工作模式和連線上限
# events,指定工作模式和連線上限 events{ use epoll; worker_connections 1024; } use 指定nginx工作模式 epoll 高效工作模式,linux kqueue 高效工作模式, bsd poll 標準模式 select 標準模式 worker_connections # 定義nginx每個程序的最大連線數 正向代理 連線數 * 程序數 反向代理 連線數 * 程序數 / 4 linux系統限制最多能同時開啟65535個檔案,預設上限就是65535, 可解除 ulimit -n 65535 # 增加連線數,需增加worker_processes,單臺裝置最高連線50000
# http,最核心的模組
# http,最核心的模組 # 主要負責http伺服器相關配置,包含server,upstream子模組 include mime.types; #設定檔案的mime型別 include xxxconfig; # 包含其它配置檔案,分開規劃解耦 default_type xxx; # 設定預設型別為二進位制流,檔案型別未知時就會使用預設 log_format # 設定日誌格式 sendfile # 設定高效檔案傳輸模式 keepalive_timeout # 設定客戶端連線活躍超時 gzip # gzip壓縮
# server,用來指定虛擬主機
# server,用來指定虛擬主機 listen 80; # 指定虛擬主機監聽的埠 server_name localhost; # 指定ip地址或域名,多個域名使用空格隔開 charset utf-8; # 指定網頁的預設編碼格式 error_page 500 502 /50x.html #指定錯誤頁面 access_log xxx main; # 指定虛擬主機的訪問日誌存放路徑 error_log xxx main; # 指定虛擬主機的錯誤日誌存放路徑 root xxx; # 指定這個虛擬主機的根目錄 index xxx; # 指定預設首頁
# location 核心中的核心,以後的主要配置都在這
# location 核心中的核心,以後的主要配置都在這 # 主要功能:定位url,解析url,支援正則匹配,還能支援條件,實現動靜分離 location [modifier] url{ ... } modifier 修飾符 = 使用精確匹配並且終止搜尋 ~ 區分大小寫的正則表示式 ~* 不區分大小寫的正則表示式 ^~ 最佳匹配,不是正則匹配,通常用來匹配目錄 常用指令 alias 別名,定義location的其他名字,在檔案系統中能夠找到, 如果location指定了正則表示式,alias將會引用正則表示式中的捕獲, alias替代lication中匹配的部分,沒有匹配的部分將會在檔案系統中搜索
例項配置檔案
# nginx.conf 配置檔案 user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; # mime:網路傳輸中允許的資料型別 default_type application/octet-stream; # 預設流媒體格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; # 訪問日誌 sendfile on; # 高效檔案傳送模式,對資料進行壓縮演算法 #tcp_nopush on; keepalive_timeout 65; # 連線最長時長 #gzip on; include /etc/nginx/conf.d/*.conf; # 內含 server }
# *.conf => server server { listen 80; server_name localhost; # 伺服器域名 #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { # 可指定主頁 root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
【uwsgi】
# 安裝 uwsgi pip install uwsgi #工程目錄下建立uwsgi.ini 配置檔案 #書寫配置資訊 #使用uwsgi伺服器 - 啟動 uwsgi --ini uwsgi.ini # 絕對路徑 - 停止 uwsgi --stop uwsgi.pid
uwsgi.ini 配置檔案
[uwsgi] # 使用nginx連線時 使用 socket=0.0.0.0:8888 # 直接作為web伺服器使用 # http=127.0.0.1:8010 # http=0.0.0.0:8888 # 配置工程目錄 chdir=/home/dc/GP1/day7/GPAXF # 配置專案的wsgi目錄。相對於工程目錄,程式入口 wsgi-file=GPAXF/wsgi.py #配置程序,執行緒資訊 #程序數 processes=4 #每個程序的執行緒數 threads=2 #是否開啟多執行緒 enable-threads=True #是否開啟主從結構 master=True #程序id儲存檔案,相對路徑 pidfile=uwsgi.pid daemonize=uwsgi.log
uwsgi作為web伺服器
nginx + uwsgi 部署
# 修改uwsgi.ini
# 修改config.conf
# 重啟 nginx 和uwsgi
【部署 => Ali雲伺服器】
# 匯出專案依賴(處於專案目錄) pip freeze > requirements.txt
# 雲伺服器系統安裝
# 通過公網ip連線伺服器
-- 連線工具
-- Xshell
-- ssh root@[公網ip]
-- 需先在雲伺服器管理上配置安全組(設定哪些埠允許被訪問)
Xshell 連線和設定伺服器
-- 新建連線
-- 安裝環境
# pip升級 pip install --upgrade pip pip3 install --upgrade pip # 安裝pip apt install python-pip apt install python3-pip # python環境 python python3 apt update apt install python-dev python3-dev python-pip python3-pip # 安裝虛擬環境 pip install virtualenvwrapper pip uninstall virtualenv # 解除安裝 # 配置虛擬環境 mkdir .virtualenvs # 刪除 rm -rf .virtualenvs/ find / -name virtualenvwrapper.sh # 得到路徑 vim .bashrc # 配置變數 source .bashrc # 啟用環境變數 # 建立虛擬環境 mkvirtualenv 環境名 -p /usr/bin/python3 deactivate # 退出當前環境 # 安裝 mysql apt install mysql-serve # 安裝redis,https://redis.io wgte http://download.redis.io/releases/redis-6.0.6.tar.gz # 下載redis tar -zxvf redis-6.0.6.tar.gz # 解壓 make # 進入解壓目錄執行make命令,構建編譯redis make test # 測試 apt install tcl # 測試發生錯誤,安裝tcl ./install_server.sh # 安裝redis,解壓目錄 => utils => install_server.sh ps -ef|grep redis redis-cli # 測試redis連線,PONG => 連線成功
專案打包傳入伺服器
-- 專案部署在伺服器的 var 目錄中
cd /var/ mkdir 專案目錄名 cd 專案目錄名 apt install lrzsz # 安裝進度顯示 將打包的專案拖入Xshell會話視窗,上傳 tar -zxvf 專案壓縮包 # 解壓專案
-- 安裝 nginx ,修改配置檔案
wget http://nginx.org/keys/nginx_signing.key # 下載nginx證書 apt-key add nginx_signing.key vim /etc/apt/sources.list # 編輯源資訊 apt install nginx # 安裝nginx nginx # 啟動nginx
vim config.conf # 進入專案目錄,編輯配置檔案 nginx -s quite nginx -t -c /.../config.conf # 退出,測試 nginx -c /.../config.conf # 開啟nginx
-- 進入虛擬環境,安裝依賴
workon 虛擬環境名 pip install -r requirements.txt pip freeze vim uwsgi.ini # 修改 chdir 專案路徑 uwsgi --ini uwsgi.ini # 啟動 uwsgi
-- 建立資料庫,匯入資料
# settings.py => DATABASES,資料庫名 mysql -uroot -p密碼 # 進入資料庫 mysql> create database 資料庫名 charset=utf8; # 建立資料庫 python manage.py migrate # 執行遷移 mysql-workbench # 連線遠端資料庫,資料庫操作,插入資料
-- 郵件傳送,25埠非安全埠,不允許使用
-- 使用ssl加密埠465,https://docs.djangoproject.com/en/3.0/topics/email/
-- 簡單壓力測試
ab -n 100 https://www.baidu.com/