• python项目上线


    一 购买服务器

    阿里云服务器:https://www.aliyun.com/product/ecs

    在控制台重置实例密码,后面远程链接用。
    在这里插入图片描述

    在这里插入图片描述

    二 使用MobaXterm连接服务器

    在这里插入图片描述
    在这里插入图片描述

    三 上线图

    在这里插入图片描述

    四 安装git

    yum install git -y
    
    • 1

    五 安装mysql

    1)前往用户根目录
    # cd ~
    
    2)下载mysql57
    # wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
    
    3)安装mysql57
    # yum -y install mysql57-community-release-el7-10.noarch.rpm
    # yum install mysql-community-server --nogpgcheck
    
    4)启动mysql57并查看启动状态
    # systemctl start mysqld.service
    # systemctl status mysqld.service
    
    5)查看默认密码并登录
    #  grep "password" /var/log/mysqld.log
    # mysql -uroot -p
    
    6)修改密码
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    六 安装redis

    1)前往用户根目录
    # cd ~
    
    2)下载redis-5.0.5(源码包)
    # wget http://download.redis.io/releases/redis-5.0.5.tar.gz
    
    
    3)解压安装包
    # tar -xf redis-5.0.5.tar.gz
    
    4)进入目标文件
    # cd redis-5.0.5
    
    5)编译环境
    # make
    
    6)复制环境到指定路径完成安装
    # cp -r /root/redis-5.0.5 /usr/local/redis
    
    7)配置redis可以后台启动:修改下方内容
    # vim /usr/local/redis/redis.conf
    
    daemonize yes
    
    8)完成配置修改
    # esc
    # wq
    
    9)建立软连接
    # ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server
    # ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli
    
    10)后台运行redis
    # cd /usr/local/redis
    # redis-server ./redis.conf &
    
    查看进程是否在运行:ps aux | grep redis
    ctrl + c
    
    11)测试redis环境
    # redis-cli
    
    12)关闭redis服务
    客户端连进去,敲  shutdown
    pkill -f redis -9
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    七 安装python

    所有linxu和mac,都自带python2,阿里云的centos默认装了python3.6。

    源码安装python,依赖一些第三方zlib* libffi-devel
    # yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel  -y
    
    1 前往用户根目录
    # cd ~
    
    2 下载 或 上传 Python3.8.6  服务器终端
    wget https://registry.npmmirror.com/-/binary/python/3.8.6/Python-3.8.6.tgz
    
    3  解压安装包
    # tar -xf Python-3.8.6.tgz
    
    4 进入目标文件
    # cd Python-3.8.6
    
    5 配置安装路径:/usr/local/python3
    把python3.8.6 编译安装到/usr/local/python38路径下
    # ./configure --prefix=/usr/local/python38
    
    6 编译并安装,如果报错,说明缺依赖
    # yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel  -y
    
    # make &&  make install
    
    #7  建立软连接:/usr/local/python38路径不在环境变量,终端命令 python3,pip3
    ln -s /usr/local/python38/bin/python3 /usr/bin/python3.8
    ln -s /usr/local/python38/bin/pip3 /usr/bin/pip3.8
    
    # 机器上有多个python和pip命令,对应关系如下
    python       2.x      pip 
    python3      3.6      pip3
    python3.8    3.8      pip3.8
    
    #8  删除安装包与文件:
    >: rm -rf Python-3.8.8
    >: rm -rf Python-3.8.8.tar.xz
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    八 安装uwsgi

    使用uwsgi运行django,不再使用测试阶段的wsgiref来运行django了
    
    uwsgi是符合wsgi协议的web服务器,使用c写的性能高,上线要使用uwsgi
    
    安装步骤
    1)在真实环境下安装
    # pip3.8 install uwsgi
    安装到了python38的安装路径的bin路径下了
    2)建立软连接
    # ln -s /usr/local/python38/bin/uwsgi /usr/bin/uwsgi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    九 安装虚拟环境

    1)安装依赖
    # pip3.8 install virtualenv
    # python3.8 -m pip install --upgrade pip
    # python3.8 -m pip install --upgrade setuptools
    # pip3.8 install pbr
    # pip3.8 install virtualenvwrapper
    
    2)建立虚拟环境软连接
    # ln -s /usr/local/python38/bin/virtualenv /usr/bin/virtualenv
    
    3)配置虚拟环境:填入下方内容
     ~/ 表示用户家路径:root用户,就是在/root/.bash_profile
    # vim ~/.bash_profile
    
    VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.8
    source /usr/local/python38/bin/virtualenvwrapper.sh
    
    4)退出编辑状态
    # esc
    
    5)保存修改并退出
    # :wq
    
    6)更新配置文件内容
    # source ~/.bash_profile
    
    7)虚拟环境默认根目录:~/.virtualenvs
    
    
    8)  mkvirtualenv -p python3.8 venv
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    十 安装nginx

    1)前往用户根目录
    # cd ~
    
    2)下载nginx1.13.7
    # wget http://nginx.org/download/nginx-1.13.7.tar.gz
    
    3)解压安装包
    # tar -xf nginx-1.13.7.tar.gz
    
    4)进入目标文件
    # cd nginx-1.13.7
    
    5)配置安装路径:/usr/local/nginx
    # ./configure --prefix=/usr/local/nginx
    
    6)编译并安装
    # make && make install
    
    7)建立软连接:终端命令 nginx
    # ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
    
    8)删除安装包与文件:
    # cd ~
    # rm -rf nginx-1.13.7
    # rm -rf nginx-1.13.7.tar.xz
    
    9)测试Nginx环境,服务器运行nginx,本地访问服务器ip
    # 启动 :nginx 
    # 停止: nginx -s stop
    >: 服务器绑定的域名 或 ip:80
    
    # netstat -nlp | grep 80
    # ps aux | grep 进程名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    十一 前端部署

    修改前端向后端发ajax请求的地址:项目的地址。
    编译vue项目成html,css,js。

    1) npm run bulid
    
    2) 项目根路径下生成dist文件夹(编译过后的文件)
    
    3) 使用SFTP连接服务器,将dict压缩包拖拽到服务端
    
    # 或者yum install -y unzip
    # yum install lrzsz -y
    直接把dist.zip 拖到 云服务器上去
    或者在云服务器敲 rz  选择上传
    
    4)解压文件
    # unzip dist.zip
    
    5)移动并重命名
    mv ~/dist /home/html
    
    6)去向Nginx配置目录,备份配置,完全更新配置:填入下方内容
    
    /usr/local/nginx/conf/nginx.conf 是nginx的配置文件
    制定nginx启动监听哪个端口
    做请求转发,做负载均衡
    做静态文件代理
    
    # cd /usr/local/nginx/conf
    
    做备份
    # mv nginx.conf nginx.conf.bak   
    
    # vim nginx.conf
    
    加入下边配置
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        server {
            listen 80;
            server_name  127.0.0.1;
            charset utf-8;
            location / {
                root /home/html;
                index index.html;
            }
        }
    } 
    
    7)重启nginx
    # nginx -s reload
    
    或者
    # nginx -s stop
    # nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    在这里插入图片描述

    十二 后端部署

    12.1 git拉取代码,安装依赖

    1)确认好wsgi.py 中得配置文件是不是用的prod.py
    
    2)导出项目所有的依赖,当前虚拟环境的依赖,就是当前项目的依赖
    pip freeze > requirements.txt
    
    3)在/home/路径下拉取项目
    git clone
    
    4)切换到venv虚拟环境
    workon venv
    
    5)安装依赖
    pip install -r requirements.txt
    
    如果安装mysqlclient报错,需要安装下边依赖
    yum install mysql-devel -y
    yum install python-devel -y
    rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    12.2 配置数据库

    创建数据库用户,和数据库,再迁移表

    1)管理员连接数据库
    mysql -uroot -p
    
    2)创建数据库
    create database db_name default charset=utf8;
    
    3)设置权限账号密码:账号密码要与项目中配置的一致
    grant all privileges on luffy.* to '用户名'@'%' identified by '密码';
    grant all privileges on luffy.* to '用户名'@'localhost' identified by '密码';
    flush privileges;
    
    4)退出mysql
    quit;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    12.3 迁移数据库

    1)cd到项目目录下
    
    2)执行数据库迁移命令
    python manage_prod.py makemigrations
    python manage_prod.py migrate
    
    • 1
    • 2
    • 3
    • 4
    • 5

    12.4 使用uwsgi启动djagno

    1)使用uwsgi跑之前先执行
    python manage_pro.py runserver 0.0.0.0:8080
    
    2)项目路径下,进行uwsgi服务配置
    
    # uwsgi的配置文件
    vim ./api.xml
    
    
       127.0.0.1:8888
       /home/project_api/
       project.wsgi
       4
       uwsgi.log
    
    
    3)启动uwsgi
    uwsgi -x ./api.xml
    
    4)查看uwsgi进程
    ps aux |grep uwsgi
    
    5)配置nginx,把8080的动态请求转发给8888端口
    vim /usr/local/nginx/conf/nginx.conf
    
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        server {
            listen 80;
            server_name  127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
            charset utf-8;
            location / {
                root /home/html; # html访问路径
                index index.html; # html文件名称
                try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
            }
        }
        # 新增的server
        server {
            listen 8080;
            server_name  127.0.0.1;
            charset utf-8;
            location / {
               include uwsgi_params;
               uwsgi_pass 127.0.0.1:8888;
               uwsgi_param UWSGI_SCRIPT prject.wsgi;
               uwsgi_param UWSGI_CHDIR /home/prject_api/;
            }
        }
    }
    
    6)重启nginx
    nginx -s reload
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    十三 后台静态文件代理

    uwsgi 只转发动态请求,静态资源它不管

    1)编辑线上配置文件
    vim /home/project_api/project/settings/prod.py
    
    2)修改static配置,新增STATIC_ROOT、STATICFILES_DIRS
    STATIC_URL = '/static/'
    STATIC_ROOT = '/home/project_api/project/static'  
    STATICFILES_DIRS = (os.path.join(BASE_DIR, "../static"),)
    
    3)退出保存
    
    4)完成静态文件迁移
    python /home/project_api/project/manage_prod.py collectstatic
    
    5)修改nginx配置
    vim /usr/local/nginx/conf/nginx.conf
    
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        server {
            listen 80;
            server_name  127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
            charset utf-8;
            location / {
                root /home/html; # html访问路径
                index index.html; # html文件名称
                try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
            }
        }
        # 新增的server
        server {
            listen 8080;
            server_name  127.0.0.1;
            charset utf-8;
            location / {
               include uwsgi_params;
               uwsgi_pass 127.0.0.1:8888;
               uwsgi_param UWSGI_SCRIPT project.wsgi;
               uwsgi_param UWSGI_CHDIR /home/project_api/;
            }
            location /static {
                alias /home/project_api/project/static;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    mysql中的date、datetime、timestamp你还不知道怎么使用吗
    工具栏和菜单栏的关系是什么?
    Vim实用技巧_3.可视模式和命令模式
    Python安装教程(Windows10系统)
    Linux centOS yum install MySQL5.7
    Apache Tomcat如何高并发处理请求
    [ 常用工具篇 ] 解决 kali 拉取docker容器慢的问题 -- kali修改 Docker 镜像服务器
    乐趣国学—品读《弟子规》中的“余力学文”之道
    Go channel被关闭时的广播机制,以及遍历未关闭channel时会导致死锁阻塞问题
    跨域访问时SameSite设置级别导致的SetCookie失败的问题分析及解决
  • 原文地址:https://blog.csdn.net/weixin_68531269/article/details/128003647