nginx+vue+uwsgi+django的前后端分离项目部署

时间:2021-07-01 06:26:21

Vue+Django前后端分离项目部署,nginx默认端口80,数据提交监听端口9000,反向代理(uwsgi配置)端口9999  

1.下载项目文件(统一在/opt/luffyproject目录)

  (1)前端vue--------------wget https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip

  (2)后端django---------wget https://files.cnblogs.com/files/pyyu/luffy_boy.zip

  (3)解压-----------------unzip解压(如需实际操作,项目文件可以直接联系博主)

2.vue框架依赖的nodejs

  (1)下载nodejs--------wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz

  (2)解压-----------------tar -zxvf node-v8.6.0-linux-x64.tar.gz

  (3)进入源码包:bin文件夹存在,说明已经编译好,查看是否可用以下命令:

      ------node -v

      ------npm -v

  (4)添加nodejs源码包bin目录到环境变量:

      ------vim /etc/profile

      ------PATH='/opt/tengine231/sbin:/opt/python3.6/bin:/usr/local/sbin:

            /usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:

            /opt/luffyproject/node-v8.6.0-linux-x64/bin'

3.配置前端vue的静态文件请求路径

  (1)前端项目/top/luffyproject/07-luffy_project_01/src/restful/api.js

  (2)修改api.js中的请求ip和端口----sed -i 's/127.0.0.1:8000/192.168.160.134:9000/g' api.js

  (3)注意在项目部署的服务器ip变化时需要更改配置,同时重新执行npm install和 npm run build进行编译生成dist静态文件

4.编译前端vue代码

  (1)npm install----网速不好可以先修改npm下载源:npm config set registry https://registry.npm.taobao.org

  (2)npm run build--/top/luffyproject/07-luffy_project_01/中种生成一个dist静态文件夹,作为nginx.conf中的server{}匹配的root根目录

  (3)注意:如果使用虚拟机,ip可能会自动改变,需重新编译

5.配置nginx.conf

  (1)前端vue静态文件返回配置

location / {

  root /opt/luffyproject/07-luffy_project_01/dist;

  index index.html index.htm;

  #确保刷新不出现404

  try_files $uri $uri/ /index.html;

}

  (2)后端django服务反向代理配置

server {

  listen 9000;

  server_name localhost;

  location / {

    include uwsgi_params;

    uwsgi_pass 0.0.0.0:9999;

  }

} 

  (3)启动nginx------nginx

6.前端vue静态页面已经部署完成

7.django项目的虚拟环境配置

  创建虚拟环境-------mkvirtualenv luffy_env4

  已创建有直接开启------workon luffy_env4

8.配置uwgis

  (1)进入后端django项目---------cd /opt/luffyproject/luffy_boy

  (2)启动测试django项目:

      启动django项目---------python3 manage.py runserver 0.0.0.0:9003

      报错根据提示在该虚拟环境内处理下载需要的包

         pip3 install django==2.0

           ........     

           pip3 install -r requirements.txt(批量下载)     

  (3)下载uwsgi模块 ---------pip3 install uwsgi

  (4)手动创建uwsgi.ini文件,写入配置:

[uwsgi]

# Django-related settings

# the base directory (full path)

chdir = /opt/luffyproject/luffy_boy

# Django's wsgi file

module = luffy_boy.wsgi

# the virtualenv (full path)

home = /root/Envs/luffy_env4

# process-related settings

# master

master = true

# maximum number of worker processes

processes = 2

# the socket (use the full path to be safe

#以socket请求与nginx结合

socket = 0.0.0.0:9999

#以http请求可以直接在刘昂器发起访问,测试用

#http =0.0.0.0:9999

# clear environment on exit

vacuum = true

#设置守护进程,后台运行,也可指定文件,输出日志

daemonize=yes

  (5)启动uwsgi--------uwsgi --ini uwsgi.ini

  (6)uwsgi以http形式进行测试访问:

      浏览器直接访问ip端口

      部分功能无法实现,缺少redis数据库

  (7)物理环境安装redis数据库  

       yum install redis

      systemctl start redis

      systemctl status redis

      redis-cli--> ping----------启动redis客户端ping是否得到PONG

  (8)修改uwsgi.ini以socket形式结合nginx

  (9)退出虚拟环境--------deactivate

9.重启nginx--------nginx -s reload