部署先后端分离项目

路飞先后端项目部署

前言

  • 使用软件
    1. vue
      1. 部署前段
    2. uwsgi
      1. uWSGI是一个全功能的HTTP服务器,实现了WSGI协议、uwsgi协议、http协议等。它要作的就是把HTTP协议转化成语言支持的网络协议。好比把HTTP协议转化成WSGI协议,让Python能够直接使用。
    3. centos7
      1. 系统环境
    4. virtulenv
      1. 在虚拟环境中部署后端项目
    5. nginx
      1. 使用nginx作反向代理
    6. redis
      1. 存储数据
    7. mysql(mariadb)
      1. 存储数据
    8. supervisor
      1. Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它能够很方便的监听、启动、中止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它从新拉起,很方便的作到进程自动恢复的功能,再也不须要本身写shell脚原本控制。

项目部署

准备工做

1 将项目上传到服务器上

  1. 方法一:使用xftp工具,进项上传文件夹,将项目代码,传到linux服务器当中html

    1. 这个页面操做,实在不会百度
  2. 方式2: 使用scp从本地将文件上传到linux服务器中vue

    scp -r  本地文件夹  远程用户名@远程ip:远程文件夹/

2 将mysql数据迁移到服务器数据库

  • 服务器端安装mysql(mariadb)数据库连接:https://www.cnblogs.com/yuncong/p/10253215.htmlnode

  • 数据导入导出python

    在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用户设置远程连接的权限mysql

    grant all privileges on *.* to root@'%' identified by 'redhat';
    # 受权全部的权限,在全部库,全部表  对  root用户在全部的主机上, 权限密码为redhat,   注意是本身设置的密码
    # 刷新受权表
    flush privileges;
  • 注意2:linux的防火墙要给关闭,不然windows去连接linux的3306端口可能被拒绝linux

    centos7默认已经使用firewall做为防火墙了
    1.关闭防火墙
    systemctl status firewalld #查看防火墙状态
    systemctl stop firewalld    #关闭防火墙
    systemctl disable firewalld#关闭防火墙开机启动
    systemctl is-enabled firewalld.service#检查防火墙是否启动

1 配置node环境

  1. 下载node,因为此包含有node,因此不须要编译ios

    wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
  2. 解压node源码包nginx

    tar -zxvf node-v8.6.0-linux-x64.tar.gz
    
    # 查看
    ls
    # 进入
    cd node-v8.6.0-linux-x64/
  3. 将node命令加入PATH环境变量中,方便调用redis

    vim /etc/profile
    
    # 在底行添加代码
    PATH=$PATH:/opt/node-v8.6.0-linux-x64/bin
    
    # 保存退出
    
    # 读出该文件,使其生效
    source /etc/profile
    
    # 查看该变量看是否成功
    echo $PATH
    
    # 测试
    node -v
    npm -v
  4. 进入到vue项目中,打包node模块sql

    cd 07-luffy_project_01/
  5. 安装vue模块,默认安装该目录中的package.json模块.若是出错,请手动安装

    npm install  # 该命令执行后会生成node_modules文件

    此时注意,你本地写的vue代码,接口极可能链接的服务器地址有问题,注意Axios.POST提交的地址,必定得发送给django应用(若是用了nginx,就发送给nginx的入口端口)
    这里为了试验方便,将vue项目和django项目放在了一台服务器,经过nginx反向代理功能(8000端口),转发vue请求给django(9000)

  6. 准备编译打包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

    注意:换成本身的服务器地址,换成本身的服务器地址,换成本身的服务器地址

  7. 打包,生成一个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安装项目的依赖包

  1. 进入项目中,新建requirements.txt文件,写入项目相关的依赖包

touch requirements.txt
vim requirements.txt

  1. 指定python解释器版本

pipenv --python python3

  1. 开启虚拟环境,并换一个国外的源,下载速度更快

pipenv shell

  1. 安装依赖包

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的静态文件
  1. 设置django的静态文件目录,收集一下

    修改项目中settings.py,写下以下参数

    STATIC_ROOT= '/opt/static'   #该路径根据实际放置
  2. 使用命令收集django的静态文件

    python manage.py collectstatic
  3. 查看django的静态文件收集目录

    ls /opt/static
  4. 配置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;
    }
    
    }

    这步要注意本身的路径,和代理

    img

  5. 重启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
  • 任务管理命令以下:有两种,一个是交互式,一个是参数形式

    1. 参数形式

      supervisorctl -c /etc/supervisor.conf stop/start/restart   all
      supervisorctl -c /etc/supervisor.conf start crm_knight
    2. 交互式形式

      supervisorctl -c /etc/supervisor.conf
  • 注意,可能在前面配置文件的时候就会启动项目,形成报错,那么就能够经过如下的命令找出来并杀死进程,而后重启

    netstat -tunlp 查看端口号
    ps -ef| grep super  查看supervisor