專案部署nginx+uwsgi+django
阿新 • • 發佈:2018-11-14
1.概念
- Django專案(web應用)
- web服務
- nginx:高併發處理的好
- Apache:穩定
LAMP:Linux Apache MySQL Python/Php/Per1
LNMP:Linux Nginx MySQL Python/Php/Per1 - uwsgi
是web伺服器與web框架之間一種簡單而通用的介面
2.單專案部署(nginx+uwsgi+Django)
- 確保django專案能夠執行
- 安裝nginx
- 安裝
sudo apt-get install nginx - 啟動
sudo /etc/init.d/nginx restart - 驗證
開啟瀏覽器 -- 127.0.0.1:80
Welcome to Nginx
- 安裝
- 安裝uwsgi(用pip3去安裝)
- 安裝
sudo pip3 install uwsgi - 驗證
uwsgi --http :9998 --chdir/home/tarena/myproject/friutday_1/ --model
friutday.wsgi
- 安裝
- 部署
- 配置uwsgi(配置檔案)
- mkdir uwsgi
- cd uwsgi
- vi fruitday_uwsgi.ini
[uwsgi]
#和nginx通訊的埠
socket=127.0.0.1:8001
#專案路徑
chdir=/home/tarena/myproject/friutdat_1
#wsgi.py路徑
wsgi-file=friutday/wsgi.py
#程序數
processes=4
#執行緒數
thread=2
#本專案佔用uwsgi埠
stats=127.0.0.1:8081
- 配置nginx(配置檔案)
- sudo -i
- cd /etc/nginx/sites-enabled
- vi project_nginx.conf
server{ # 監聽本專案埠,瀏覽器輸入的埠 listen 8201; server_name frituday.com; charset utf-8; client_max_body_size 75M; # 收集專案靜態檔案路徑 location /static{ alias /home/tarena/myproject/friutday_1/static; } # 和uwsgi通訊埠和專案通訊檔案uwsgi_params location /{ include uwsgi_params; uwsgi_pass 127.0.0.1:8001; } }
- 重啟nginx服務
sudo /etc/init.d/nginx restart - 拷貝uwsgi_params檔案到專案目錄
cp uwsgi_params /home/tarena/myproject/friutday_1/
- 收集靜態檔案
- 在settings.py檔案中新增路徑(STATIC_ROOT)
STATIC_ROOT = '/home/tarena/myproject/friutday_1/static/' - 收集靜態檔案
python3 manage.py collectstatic
- 在settings.py檔案中新增路徑(STATIC_ROOT)
- uwsgi啟動專案
cd /home/tarena/uwsgi
uwsgi --ini fruitday_uwsgi.ini
- 配置uwsgi(配置檔案)
3.多專案部署
- uwsgi
每個專案需要單獨建立uwsgi配置檔案,選用不同埠 - nginx
配置檔案,1個就可以,新增server{} - 部署個人部落格專案
- 瀏覽器訪問的埠:8202
/etc/nigin/sites-enabled/project_nigix.conf
#listen 8202 - uwsgi 和 nigix 通訊埠:8002
cd uwsgi
vi blog_uwsgi.ini
#socket=127.0.0.1:8002 - uwsgi啟動個人部落格專案佔用埠:8082
vi blog_uwsgi.ini
stats=127.0.0.1:8082
- 瀏覽器訪問的埠:8202