Linux - nginx+uWSGI+django+virtualenv+supervisor發布web服務器
目錄
- Linux - nginx+uWSGI+django+virtualenv+supervisor發布web服務器
- crm django項目部署流程
- 使用supervisro啟動uwsgi,退出虛擬環境, 使用物理環境的python2去安裝
- 前後端分離項目部署流程
- 原理圖
- crm django項目部署流程
Linux - nginx+uWSGI+django+virtualenv+supervisor發布web服務器
crm django項目部署流程
項目部署,環境準備
1.python3虛擬環境準備
mkvirtualenv -p python3 my_django
2.安裝uwsgi
pip3 install uwsgi
安裝 django
pip3 install django==1.11.18
pip3 install -i https://pypi.douban.com/simple pymysql
pip3 install -i https://pypi.douban.com/simple django-multiselectfield
3.通過uwsgi啟動django項目
1.進入項目的第一層 cd Ace_crm 2.執行命令啟動django uwsgi --http :8000 --module Ace_crm.wsgi
4.熱加載uwsgi 命令如下
uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1
5.通過配置文件,啟動uwsgi
這個文件叫做 uwsgi.ini
#手動創建這個配置文件,寫入如下信息 [uwsgi] # Django-related settings # the base directory (full path) #這裏寫入項目的絕對路徑 chdir = /opt/crm/Ace_crm # Django's wsgi file #寫入django的第二層文件夾,和wsgi.py文件 module = Ace_crm.wsgi # the virtualenv (full path) #虛擬環境的絕對路徑 home = /root/Envs/my_django # process-related settings # master master = true # maximum number of worker processes processes = 5 #如果你沒用nginx,想直接訪問django後臺,可以使用http協議 #http = 0.0.0.0:8000 #如果你用了nginx進行反向代理,請使用socket協議,註釋掉http協議 #如果你用了nginx進行反向代理,請使用socket協議,註釋掉http協議 #如果你用了nginx進行反向代理,請使用socket協議,註釋掉http協議 socket = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true 啟動 uwsgi --ini uwsgi.ini
6.nginx結合uwsgi 啟動
1.修改nginx配置 nginx.conf如下
#是nginx進行反向代理
location / {
include uwsgi_params;
uwsgi_pass 0.0.0.0:8000;
}
#http://192.168.11.250/static/admin/css/base.css
#當請求url,從static路徑開始時,我們就讓他去找某一個文件夾
#解決靜態文件的配置方式
location /static {
alias /opt/aceCrmStatic;
}
2.修改django的settings.py,收集靜態資源
1.修改settings.py如下
STATIC_ROOT="/opt/aceCrmStatic"
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
3.使用命令收集django的靜態文件
python3 manage.py collectstatic
?
4.此時靜態頁面配置完畢
使用supervisro啟動uwsgi,退出虛擬環境, 使用物理環境的python2去安裝
1.安裝supervisor
yum install python-setuptools
easy_install supervisor
2。安裝完畢後,生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
3.在配置文件中,添加任務,管理uwsgi
vim /etc/supervisord.conf #在最底行,寫入配置如下
[program:s17uwsgi]
command=/root/Envs/my_django/bin/uwsgi --ini /opt/crm/Ace_crm/uwsgi.ini
stopasgroup=true
killasgroup=true
[program:s17nginx]
command=/opt/tnginx220/sbin/nginx
stopasgroup=true
killasgroup=true
4.啟動supervisor服務
supervisord -c /etc/supervisord.conf
5.通過命令管理任務,管理uwsgui
supervisorctl -c /etc/supervisord.co
6。學習管理supervisor的命令
start 任務名
stop 任務名
stop all
start all
status 任務名
nginx + uwsgi + crm + mariadb +supervisor
前後端分離項目部署流程
一.準備代碼
二. 從後端搞起
解決環境依賴
把可以正常運行路飛哪個機器地下的python包,全部導出來,就可以
pip3 freeze > requirements.txt把這個文件,傳輸給linux系統 linux再通過命令安裝 pip3 install -r requirements.txt 通過模塊依賴文件,直接安裝,著是從講師機器上下載的 手動創建依賴文件,然後寫入
(s17luffy) [root@master luffy_boy]# cat requirements.txt
certifi==2018.11.29
chardet==3.0.4
crypto==1.4.1
Django==2.1.4
django-redis==4.10.0
django-rest-framework==0.1.0
djangorestframework==3.9.0
idna==2.8
Naked==0.1.31
pycrypto==2.6.1
pytz==2018.7
PyYAML==3.13
redis==3.0.1
requests==2.21.0
shellescape==3.4.1
urllib3==1.24.1
uWSGI==2.0.17.1安裝uwsgi啟動後端
pip3 install uwsgi
啟動方式1,用參數啟動uwsgi --socket :8000 --module luffy_boy.wsgi 啟動方式2:用配置文件啟動 touch uwsgi.ini 寫入配置: [uwsgi] # Django-related settings # the base directory (full path) chdir = /opt/luffy_boy # Django's wsgi file module = luffy_boy.wsgi # the virtualenv (full path) home = /root/Envs/vue # process-related settings # master master = true # maximum number of worker processes processes = 1 # the socket (use the full path to be safe socket = 0.0.0.0:6666 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
二. 開始搞前段(vue)
1. 準備node環境 ,下載node環境包
wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
2. 解壓縮node包
tar -zxvf node-v8.6.0-linux-x64.tar.gz
3. 添加node到環境變量
PAHT="node環境"
vim /etc/pro..
source /etc/pro..
4. 開始編譯打包前端vue文件
1.修改vue提交的請求地址,修改文件是
/opt/s17luffy/07-luffy_project_01/src/restful/api.js
2.更改接口內的地址
原本是 127.0.0.1:8000/
改成服務器的ip地址+端口
sed -i "s/127.0.0.1/192.168.11.175/g" /opt/07-luffy_project_01/src/restful/api.js
此時進入vue代碼的第一層文件夾
cd /opt/s17luffy/07-luffy_project_01
3.安裝node模塊,這是找到package.json,安裝它的內容
npm install
4.編譯打包vue代碼,這一步會生成dist靜態文件夾,用於nginx展示,路飛的頁面都在這了
npm run build
5. 配置nginx去找到這個路飛學城頁面
修改nginx.conf(也可以利用include /opt/tnginx220/conf/conf.d/*.conf; 去conf.d配置) 參數如下
虛擬主機1,用於找到vue頁面
server {
listen 80;
server_name s17dnf.com;
location / {
root /opt/07-luffy_project_01/dist;
index index.html;
}
}
虛擬主機2,用於反向代理,找到django
server{
listen 8000;
server_name 192.168.11.250;
location / {
include uwsgi_params;
uwsgi_pass 0.0.0.0:6666;
}
}
6. 重啟 nginx
7. 啟動redis數據庫,支撐購物車數據
redis-server redis.conf
三. 確保前端vue正常,後端django正常,nginx啟動正常,redis啟動正常,在windows中訪問頁面
原理圖
Linux - nginx+uWSGI+django+virtualenv+supervisor發布web服務器