这里使用centos7.6 网络自行配置,并配上DNS,这里不做二进制部署

1 在线上服务器安装虚拟开发环境

1.1 安装Python

1.1.1 安装编译相关工具

  1. yum -y groupinstall "Development tools"
  2. yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
  3. yum install libffi-devel -y

1.1.2 下载安装包解压

  1. mkdir /usr/local/python3 #创建编译安装目录
  2. wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
  3. tar -xvJf Python-3.7.0.tar.xz

1.1.3 编译安装python

  1. cd Python-3.7.0
  2. ./configure --prefix=/usr/local/python3
  3. make && make install

1.1.4 创建软连接

  1. ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
  2. ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

1.1.5 验证是否成功

  1. python3 -V
  2. pip3 -V

1.2 安装虚拟环境

  1. yum install python-virtualenv
  2. # 或者pip3 install virtualenv

1.2.1 创建python虚拟环境

使用virtualenv命令创建python虚拟环境:virtualenv [虚拟环境名称]。

  1. virtualenv env1

执行后,在本地会生成一个与虚拟环境同名的文件夹。 如果你的系统里安装有不同版本的python,可以使用–python参数指定虚拟环境的python版本:

  1. virtualenv --python=/usr/local/python3/bin/python3.7 env1

1.2.2 启动虚拟环境

进入虚拟环境目录

  1. cd env1
  2. . bin/activate
  3. # 退出虚拟环境 deactivate

1.2.3 上传项目到服务器

  1. scp -r project root@192.168.13.2:/data/www

2 安装其他

2.1 安装nginx

  1. yum install -y nginx
  2. systemctl enable nginx
  3. systemctl start nginx
  4. systemctl status nginx
  5. sudo firewall-cmd --permanent --zone=public --add-service=http
  6. sudo firewall-cmd --permanent --zone=public --add-service=https
  7. sudo firewall-cmd --reload

2.2 安装mysql并启动

  1. yum install mysql-server -y
  2. systemctl start mysqld
  3. systemctl enable mysqld
  4. systemctl daemon-reload
  5. mysql -u root -p
  6. SET PASSWORD = password('root'); 设置当前用户密码
  7. flush privileges;
  8. # 开启远程连接
  9. GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
  10. # 创建数据库
  11. create database test;
  12. use test;

2.3 在虚拟开发环境中安装django、pymysql、pillow

根据requirement.txt来安装

  1. cd /data/www/project
  2. pip3 install -r requirement.txt

2.4 安装redis

  1. yum install -y redis
  2. systemctl enable redis
  3. systemctl start redis
  4. systemctl status redis

2.5 安装uwsgi

  1. pip3 install uwsgi
  2. sudo ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
  1. 上传项目
  2. 在项目中根目录下创建uconfig.ini文件,代码如下
    1. [uwsgi]
    2. # 外部访问地址,可以指定多种协议,用socket
    3. socket = 127.0.0.1:9090 # uwsgi的监听端口
    4. # 只想项目根目录
    5. chdir = /data/www/project
    6. wsgi-file = project/wsgi.py
    7. module = project.wsgi
    8. plugins = python
    9. # 虚拟开发环境位置
    10. master = true
    11. # 处理器数
    12. processes = 2
    13. # 线程数
    14. threads = 2

    3 启动

    3.1 进入虚拟环境,前台启动uwsgi查看运行状态

    1. uwsgi uconfig.ini
    2. # 没有报错则正常,有报错解决报错
    3. uwsgi --reload uconfig.ini # 重启
    4. uwsgi --stop uconfig.ini # 关闭
    5. uwsgi -d --ini uconfig.ini # 后台启动

    3.2 配置nginx,并启动

    3.2.1 配置nginx

    server {
     listen 80;
     root         /data/www/project;
     location / {
         # 转发端口必须和uconfig.ini中socket端口一致
         uwsgi_pass 127.0.0.1:8000;
         include uwsgi_params;
         uwsgi_param UWSGI_SCRIPT project.wsgi;
         # 项目的根目录
         uwsgi_param UWSGI_CHDIR /data/www/project;
     }
     # 静态资源所在位置
     location /static {
         alias /data/www/project/static/;
     }
    }
    

    3.2.2 重启nginx

    nginx -t
    systemctl restart nginx
    

    3.3 迁移数据库并访问项目

    python manage.py migrate