django-vue項目部署


上線部署准備工作

  • 安裝mysql

  • 安裝redis

安裝: apt-get install redis-server
從進程中查看是否啟動: ps -aux|grep redis 端口 6379
  • 安裝nginx

安裝: sudo apt install nginx

systemctl status nginx 查看nginx的狀態
systemctl start/stop/enable/disable nginx 啟動/關閉/設置開機啟動/禁止開機啟動

或者是如下命令:
service nginx status/stop/restart/start

注意:nginx的配置文件中加載自定義的nginx的配置文件

vim /etc/nginx/nginx.conf
在server中加入以下配置:
include /home/app/conf/*.conf;
  • 安裝uwsgi

pip install uwsgi 安裝到虛擬環境下

vim /etc/nginx/nginx.conf
在server中加入以下配置:
include /home/app/conf/*.conf;

注意:將window上項目文件移動到與服務器

scp -r D:\/file\/wordspace\/axf root@47.112.15.210(雲服務器ip):/home/code

scp -r D:\/file\/wordspace\/axf-vue\/dist root@47.112.15.210:/home/code

 

一.前端vue

1.ajax請求地址修改

const ajax = axios.create({
 // baseURL: 'http://127.0.0.1:8000/' # 本地使用
 // baseURL: 'http://47.112.15.210:8000/'
 baseURL: 'http://47.112.15.210:8080/' 改為反向代理,前端訪問端口和ajax請求端口改為同一端口
})

2.樣式問題

  • style樣式中加入 scoped 避免樣式污染 <style scoped> <style>

  • bulid下的utils.js文件, 加入publicPath: '../../'

 

3.vue打包

打包后生成dist目錄

npm run build

注意: 這個時候直接打開dist/下的index.html,會發現文件可以打開,但是所有的js,css,img等路徑有問題是指向根目錄的。 此時需要修改config/index.js里的assetsPublicPath的字段,初始項目是/他是指向項目根目錄,這時改為./即可。

./ 當前目錄

../ 父級目錄

/ 根目錄

 

二.使用nginx掛載整個項目

1.配置 nginx

新建文件 axfuwsgi.conf

upstream backend {   # 反向代理多端口配置
      server 172.17.18.103:8001;   # 雲服務器內網ip
      server 172.17.18.103:8002;
      server 172.17.18.103:8003;
}

server {
      listen 8080;                # 監聽8080端口
      server_name 47.112.15.210;  # 雲服務器公網ip

      root /home/code/django/dist; # root參數表示前端dist文件路徑
      index index.html;            # index表示訪問首頁地址頁面,貌似只能訪問首頁,可注釋掉
​     try_files $uri $uri/ /index.html; 將默認的index注釋掉,換成了try_files,它會去掃描內部目錄然后再進行內部重定向。
     expires 7d;   expires 是nginx控制緩存的一種方式,7d=7天 

      location /api/ {   # 訪問8080端口前綴為api時,請求跳轉到 proxy_pass 指定的內網ip繼續訪問
          proxy_pass http://backend;  # proxy_pass 表示反向代理地址
      }
}

2.配置 uwsgi

新建文件 axfuwsgi.ini

[uwsgi]
master=true
pythonpath = /home/env/axfenv/bin/python3
http = 172.17.18.103:8001       # 雲服務器內網ip
logto = /home/logs/axfuwsgi.log # 設置日志文件地址
chdir = /home/code/django/axf  
module = axf.wsgi

注意: 當在瀏覽器中訪問http://47.112.15.210:8080地址時,瀏覽器中將訪問/app/src/dist/index.html頁面,並且將頁面中所有訪問以/api/開頭的路由地址,反向解析到backend中定義的地址與端口上。

特別注意: server中定義的監聽端口為8080,因此在vue中配置訪問的axios的請求前綴baseURL也將定義為http://47.112.15.210:8080

 

三.django后端

1.settings 文件配置

  • 數據庫名設置為對應雲服務器數據庫, 首先要在雲服務器創建數據庫

  • DEBUG = False # 上線時改為False,隱藏報錯時出現路由

  • ALLOWED_HOSTS = ['*'] # 允許哪些ip訪問django程序, * 代表所有

 

使用ngnix和uwsgi掛載項目

啟動ini文件命令

/home/env/axfenv/bin/uwsgi --ini /home/conf/axfuwsgi.ini &

查看日志:

tail -f /home/logs/axfuwsgi.log   動態查看

注意事項:

1.django mysql配置

  • 密碼修改為雲服務器mysql密碼

  • 數據庫名修改為雲服務器數據庫

2.nginx的配置文件中加載自定義的nginx的配置文件

vim /etc/nginx/nginx.conf
在server中加入以下配置:
include /home/app/conf/*.conf;

3.每次修改完 nginx.conf 相關文件,需重啟nginx

service nginx status/stop/restart/start

4.阿里雲服務器 防火牆端口需開放

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM