部署前後端分離專案
路飛前後端專案部署
前言
- 使用軟體
- vue
- 部署前段
- uwsgi
- uWSGI是一個全功能的HTTP伺服器,實現了WSGI協議、uwsgi協議、http協議等。它要做的就是把HTTP協議轉化成語言支援的網路協議。比如把HTTP協議轉化成WSGI協議,讓Python可以直接使用。
- centos7
- 系統環境
- virtulenv
- 在虛擬環境中部署後端專案
- nginx
- 使用nginx做反向代理
- redis
- 儲存資料
- mysql(mariadb)
- 儲存資料
- supervisor
- Linux/Unix系統下的一個程序管理工具,不支援Windows系統。它可以很方便的監聽、啟動、停止、重啟一個或多個程序。用Supervisor管理的程序,當一個程序意外被殺死,supervisort監聽到程序死後,會自動將它重新拉起,很方便的做到程序自動恢復的功能,不再需要自己寫shell指令碼來控制。
- vue
專案部署
準備工作
1 將專案上傳到伺服器上
方法一:使用xftp工具,進項上傳資料夾,將專案程式碼,傳到linux伺服器當中
- 這個頁面操作,實在不會百度
方式2: 使用scp從本地將檔案上傳到linux伺服器中
scp -r 本地資料夾 遠端使用者名稱@遠端ip:遠端資料夾/
2 將mysql資料遷移到伺服器資料庫
伺服器端安裝mysql(mariadb)資料庫連結:https://www.cnblogs.com/yuncong/p/10253215.html
資料匯入匯出
在linux服務端,mysql,匯入knight的資料 1.mysql資料的匯出,與匯入 這個命令是在linux/windows中敲的 mysqldump -u root -p --all-databases > knight.dump 2.上傳這個資料檔案到linux資料庫中 3.在linux的mysql,匯入這個資料檔案 mysql -uroot -p < /opt/knight.dump
注意:linux的資料庫,需要對root使用者設定遠端連結的許可權
grant all privileges on *.* to [email protected]'%' identified by 'redhat'; # 授權所有的許可權,在所有庫,所有表 對 root使用者在所有的主機上, 許可權密碼為redhat, 注意是自己設定的密碼 # 重新整理授權表 flush privileges;
注意2:linux的防火牆要給關閉,否則windows去連結linux的3306埠可能被拒絕
centos7預設已經使用firewall作為防火牆了 1.關閉防火牆 systemctl status firewalld #檢視防火牆狀態 systemctl stop firewalld #關閉防火牆 systemctl disable firewalld#關閉防火牆開機啟動 systemctl is-enabled firewalld.service#檢查防火牆是否啟動
1 配置node環境
下載node,由於此包含有node,所以不需要編譯
wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
解壓node原始碼包
tar -zxvf node-v8.6.0-linux-x64.tar.gz # 檢視 ls # 進入 cd node-v8.6.0-linux-x64/
將node命令加入PATH環境變數中,方便呼叫
vim /etc/profile # 在底行新增程式碼 PATH=$PATH:/opt/node-v8.6.0-linux-x64/bin # 儲存退出 # 讀出該檔案,使其生效 source /etc/profile # 檢視該變數看是否成功 echo $PATH # 測試 node -v npm -v
進入到vue專案中,打包node模組
cd 07-luffy_project_01/
安裝vue模組,預設安裝該目錄中的package.json模組.如果出錯,請手動安裝
npm install # 該命令執行後會生成node_modules檔案
此時注意,你本地寫的vue程式碼,介面很可能連線的伺服器地址有問題,注意Axios.POST提交的地址,一定得傳送給django應用(如果用了nginx,就傳送給nginx的入口埠)
這裡為了試驗方便,將vue專案和django專案放在了一臺伺服器,通過nginx反向代理功能(8000埠),轉發vue請求給django(9000)準備編譯打包vue專案,使用sed命令替換配置檔案中所有的地址,給為伺服器地址
sed -i 's/127.0.0.1/192.168.11.99/g' /opt/opt/07-luffy_project_01/src/restful/api.js # 將該路徑下的所有127.0.0.1換成192.168.11.99
注意:換成自己的伺服器地址,換成自己的伺服器地址,換成自己的伺服器地址
打包,生成一個dist靜態檔案
npm run build # 檢查該檔案 ls dist/ # 結果 index.html static
配置完成
2 後端配置
1 使用virtulenv管理django專案(2選一即可)
mkvirtualenv drf
# 進入該虛擬環境
workon drf
# 停止該虛擬環境
deactivate
如果未安裝,檢視該連結:https://www.cnblogs.com/yuncong/p/10251899.html
解決專案模組依賴
該專案所需檔案
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
新建檔案requirements.txt
touch requirements.txt # 編輯該檔案 vim requirements.txt # 插入上訴所需檔案
生成專案依賴模組
pip freeze > requirements.txt
2 使用pipenv安裝專案的依賴包
- 進入專案中,新建requirements.txt檔案,寫入專案相關的依賴包
touch requirements.txt
vim requirements.txt
- 指定python直譯器版本
pipenv --python python3
- 開啟虛擬環境,並換一個國外的源,下載速度更快
pipenv shell
- 安裝依賴包
pipenv install
注意目錄中初始化虛擬環境之前,就要有requirements.txt這個檔案,他會將裡面所有的依賴包載入到Pipfile中,然後根據這個檔案下載包
3 啟動redis服務端
新建一個redis-6379.conf,配置檔案如下
port 6379 daemonize yes pidfile /data/6379/redis.pid loglevel notice logfile "/data/6379/redis.log" # #這個檔案要自己在相應的目錄下建立 dir /data/6379 protected-mode yes
啟動redis服務端
redis-server redis-6379.conf # 這裡是相對路徑,如果不在,起使用絕對路徑
啟動時報錯,如果沒有/data/6379/redis.log,這個檔案需要自己建立
檢視程序是否啟動
ps -ef|grep redis
4 配置uwsgi
在該django虛擬環境中安裝uwsgi
pip3 install uwsgi
使用uwsgi.ini配置檔案去啟動專案,這個檔案自己去建立即可,放哪都可以,但是放在專案中最好
touch /opt/luffy_boy/uwsgi.ini
配置資訊如下
vim uwsgi.ini # 插入資訊 [uwsgi] # Django-related settings # the base directory (full path) #寫上專案的絕對路徑 chdir = /opt/vue_drf/luffy_boy # Django's wsgi file #填寫找到django的wsgi檔案,填寫相對路徑,以chdir引數為相對路徑 module = luffy_boy.wsgi # the virtualenv (full path) #填寫虛擬環境的絕對路徑 home = /root/Envs/drf # process-related settings # master #啟動uwsgi主程序 master = true # maximum number of worker processes processes = 5 #如果你使用了nginx,做反向代理,必須填寫socket連結,而不是http引數 # the socket (use the full path to be safe socket = 0.0.0.0:9000 #如果你不用nginx,直接使用uwsgi,執行一個http服務端,就用這個http引數 #http = 0.0.0.0:9000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
注意: 檢視虛擬環境的命令
cdvirtualenv 導航到當前啟用的虛擬環境的目錄中,比如說這樣您就能夠瀏覽它的 site-packages pwd # 檢視 就能看到當前虛擬環境的路徑
啟動django專案
uwsgi --ini uwsgi.ini # 這裡用的是相對路徑,找不到使用絕對路徑
5 配置nginx,使用反向代理
- 作用: 使用ngixn處理django的靜態檔案
設定django的靜態檔案目錄,收集一下
修改專案中settings.py,寫下如下引數
STATIC_ROOT= '/opt/static' #該路徑根據實際放置
使用命令收集django的靜態檔案
python manage.py collectstatic
檢視django的靜態檔案收集目錄
ls /opt/static
配置nginx,進行反向代理,找到uwsgi專案,且配置nginx處理uwsgi的靜態檔案
編輯nginx.conf
server { listen 80; #域名 server_name hedouyu.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root /opt/vue_luffy/07-luffy_project_01/dist; 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 html; } } server { listen 8000; server_name 192.168.11.96; location / { include /opt/nginx1-12/conf/uwsgi_params; uwsgi_pass 0.0.0.0:9000; #root /opt/huya; #index index.html; } location /static { alias /opt/vue_luffy/static; } }
這步要注意自己的路徑,和代理
重啟nginx
./nginx/sbin/nginx -s reload
此時專案就已經完全可以運行了
6 配置supervisor程序管理工具,管理uwsgi
使用supervisor命令,生配置檔案
echo_supervisord_conf > /etc/supervisor.conf
在這個配置檔案中加入我們想要管理的任務
vim /etc/supervisor.conf # 在底部寫入如下配置 #定義一個任務,名字自定義 #commnad=引數,定義我們啟動專案的命令 [program:my_luffy] # uwsgi的絕對路徑和 uwsgi.ini的絕對路徑 command=/root/.local/share/virtualenvs/luffy_boy-8uvrb15Z/bin/uwsgi /opt/luffy_boy/uwsgi.ini stopasgroup=true ;預設為false,程序被殺死時,是否向這個程序組傳送stop訊號,包括子程序 killasgroup=true ;預設為false,向程序組傳送kill訊號,包括子程序
通過配置檔案啟動supervisor服務
supervisord -c /etc/supervisor.conf
啟動了supervisor服務端後,管理任務
supervisorctl -c /etc/supervisor.conf
任務管理命令如下:有兩種,一個是互動式,一個是引數形式
引數形式
supervisorctl -c /etc/supervisor.conf stop/start/restart all supervisorctl -c /etc/supervisor.conf start crm_knight
互動式形式
supervisorctl -c /etc/supervisor.conf
注意,可能在前面配置檔案的時候就會啟動專案,造成報錯,那麼就可以通過以下的命令找出來並殺死程序,然後重啟
netstat -tunlp 檢視埠號 ps -ef| grep super 檢視supervisor