上线11
内容回顾
文件存储
之前上传到服务器,放到media文件夹下
使用文件服务器托管文件
1.第三方托管
腾讯云
阿里云
七牛云
2.基于开源文件服务器自己搭建
fastdsf 》中小文件
minio
go-fastdsf :是一个基于http协议的分布式文件系统,它基于大道至简的设计理念,一切从简单设计,使得它的运维及扩展变得更加简单,它具有高性能,高可用,无中心,面维护等优点
搜索功能
前端header.vue 搜索框
搜接口:结果:实战课,轻课,免费》分布式全文检索引擎
前端搜索结果页面
支付宝支付
商户:我们系统》申请商户号,需要营业执照
沙箱环境:测试商户号
买家:测试买家
支付流程:用户在我们系统中点击立即购买》我们后端生成支付链接》返回给前端》打开这个支付地址(支付宝付款页面)》用户扫码输入账户密码付款》支付宝收到用户付款》用户付到支付宝商户上》支付宝会有前端回调地址(get)》调到我们配置的前端回调地址》我们做了个付款成功的页面》后台写了个支付宝回调的地址(post,支付宝用,验证了签名,防止破解)
使用第三方sdk
封装了包
| -__init__ | |
| -settings.py | |
| -pay.py | |
| -pem | |
| -支付宝公钥 | |
| -我们私钥 |
订单板块结构表
order
orderDetail
下单接口
前端传入的数据
| {courses:[1,],total_amount:99,subject:订单标题} |
后端
1取出课程列表,根据课程列表拿到course对象,统计里面的金额与前端对比是否一致
2.生成订单 uuid
3.拿到购买用户request.user中取
4.生成支付链接 放到context中
5.把订单存起来,存到order订单表与orderDetail订单详情表中
6.取出context中的支付链接把支付链接返回前端,前端跳转到支付宝支付页面
前端支付
open函数打开支付链接地址
付款成功回到支付成功页面
两个回调
get回调地址用于给前端查询是否真正付款成功
post回调地址用来给支付宝,当用户支付成功回调的地址,更新用户订单状态
内容详细
上线架构图
服务器,公网ip地址
云服务器:阿里云,腾讯云
上线架构图

阿里云购买
购买阿里云服务器
ssh客户端
xshell
finalshellhttp://www.hostbuf.com/t/988.html
git Bash命令
配置一下服务器,装一些常用的依赖
| #更新yum工具 | |
| yum update -y |
| # 装了一堆开发用的工具,git | |
| yum -y groupinstall "Development tools" |
| # 安装一些可能会用到的工具 | |
| yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel -y |
云服务器安装mysql
前往用户根目录
| # 回到家目录/root | |
| cd ~ |
下载mysql57
这个版本不需要只需跟着步骤来就行了
| wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm |
安装
| yum -y install mysql57-community-release-el7-10.noarch.rpm |
| # 真正安装上面选择完毕后这个直接执行 | |
| yum install mysql-community-server --nogpgcheck -y |
启动mysql57并查看启动状态
启动mysql
| systemctl start mysqld.service |
查看mysql启动状态
| systemctl status mysqld.service |

查看默认密码并登录
| grep "password" /var/log/mysqld.log | |
| # 密码 qTPzt<srl5** | |

登录
| mysql -uroot -p | |
| # 密码 qTPzt<srl5** | |

修改密码
| ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lqz12345?'; |
云服务器安装redis(源码安装)
前往用户跟目录
| cd ~ |
下载redis-5.0.5
| wget http://download.redis.io/releases/redis-5.0.5.tar.gz |
解压安装包
| tar -xf redis-5.0.5.tar.gz |
进入目标文件
| cd redis-5.0.5 |
编译环境
| make | |
| # 在src路径下会有可执行文件:redis-server redis-cli |
复制环境到指定路径完成安装
| cp -r ~/redis-5.0.5 /usr/local/redis |
配置redis可以后台启动:修改下方内容
| vim /usr/local/redis/redis.conf | |
| daemonize yes |
完成配置
| esc | |
| :wq | |
| 回车 |
建立软连接(因为/usr/local/redis/src)没有加到环境变量
| # /usr/bin/ 在环境变量中,所以以后直接敲redis-server就能找到了 | |
| ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server | |
| ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli |
后台运行redis
| cd /usr/local/redis | |
| redis-server ./redis.conf & |
查看是否正常运行
| ps aux |grep redis |
测试redis环境
| redis-cli |

关闭redis服务
| pkill -f redis -9 |
云服务器安装python3.8(源码安装)
linux mac 系统服务是用python写的python2写所以系统中默认带了python2
阿里云的centos自动装了python3.6
云服务器上有python2和python3.6
自己装python3.8
centos快速安装 yum install python 可以快速安装,但是不能指定版本,不能指定安装目录
源码安装 下载指定版本的源码,编译安装
所以linux和mac都自带python2:系统服务,用的python2写的
阿里云的centos默认安装了python3.6
python2 python3.6 python3.8
源码安装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 |
前往用户根目录
| cd ~ |
下载或上传python3.8.6 到服务器终端
| wget https://registry.npmmirror.com/-/binary/python/3.8.6/Python-3.8.6.tgz |
解压安装包
| tar -xf Python-3.8.6.tgz |
进入目标文件
| cd Python-3.8.6 |
配置安装路径:/usr/local/python3
把python3.8.6 编译安装到/usr/local/python38路径下
| ./configure --prefix=/usr/local/python38 |
编译安装如果报错,说明却依赖
| make && make install |
| yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel -y |
建立软连接/usr/local/python38路径不在环境变量,终端命令 python3,pip3
把/usr/local/python38/bin加入到环境变量
| 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 | |
| python3 3.6 pip3 | |
| python3.8 3.8 pip3.8 |
删除安装包与文件
| rm -rf Python-3.8.8 | |
| rm -rf Python-3.8.8.tar.xz |
安装uwsgi
django flask项目上线,需要使用uwsgi部署,性能高,符合wsgi协议的web服务器
使用uwsgi运行django,不在使用测试阶段的wsgiref来运行dajngo了
uwsgi是符合wsgi协议的web服务器,使用c写的性能高,上线要使用uwsgi
安装步骤
在真实环境安装
| pip3.8 install uwsgi |
安装到了python38的安装路径的bin路径下了
建立软链接
| ln -s /usr/local/python38/bin/uwsgi /usr/bin/uwsgi |

安装虚拟环境
pip换源
| 在root文件夹.pip文件夹pip.conf文件内填写 | |
| # 创建文件 | |
| mkdir .pip | |
| cd .pip | |
| vim pip.conf | |
| 下入下面代码 | |
| esc | |
| :wq | |
| 回车 | |
| 退出保存 | |
| [global] | |
| index-url = https://mirrors.aliyun.com/pypi/simple | |
| [install] | |
| trusted-host = mirrors.aliyun.com |
安装前先更新一下pip 与setuptools pbr 减少安装模块错误
| # 升级pip版本 | |
| python3.8 -m pip install --upgrade pip | |
| #升级setuptools | |
| python3.8 -m pip install --upgrade setuptools | |
| pip3.8 install pbr |
安装依赖
| pip3.8 install virtualenv | |
| pip3.8 install virtualenvwrapper |
建立虚拟环境软连接
| ln -s /usr/local/python38/bin/virtualenv /usr/bin/virtualenv |
配置虚拟环境:填入下方内容
~/ 表示用户家路径:root用户,就是
在/root/.bash_profile
| vim ~/.bash_profile |
退出编辑并保存
| esc | |
| :wq |
更新配置文件内容
| source ~/.bash_profile |

虚拟环境默认环境目录~/.virtualenvs
创建虚拟环境
| mkvirtualenv -p python3.8 luffy |

| # 退出虚拟环境 | |
| deactivate | |
| # 查看虚拟环境 | |
| workon | |
| # 进入虚拟环境 | |
| workon 虚拟环境名 |

按章nginx(源码安装)
nginx:软件网关软件
Nginx(enigne x)是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·塞索耶夫为俄罗斯访问量第二的Rambler.ru站点开发的服务器
运行在服务器上,监听某个端口,可以向这个服务器发生http请求
转发http请求,代理静态文件,负载均衡
安装Nginx
前往用户根目录
| cd ~ |
下载nginx1.13.7
| wget http://nginx.org/download/nginx-1.13.7.tar.gz |
解压安装包
| tar -xf nginx-1.13.7.tar.gz |
进入目标文件
| cd nginx-1.13.7 |
配置安装路径 /usr/local/nginx
| ./configure --prefix=/usr/local/nginx |
编译并安装
| make && sudo make install |
建立软连接:终端命令 nginx
| ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx |
删除安装包与文件
| cd ~ | |
| rm -rf nginx-1.13.7 | |
| rm -rf nginx-1.13.7.tar.xz | |
测试Nginx环境,服务器运行Nginx,本地访问服务器ip
| nginx | |
| 服务器绑定的域名 或 ip:80 | |

无法访问时记得查看服务器防护墙是否在开启着,如果在开启着需要关闭才能访问
| 1、命令行界面输入命令 | |
| systemctl status firewalld.service | |
| 并按下回车键。 | |
| 2、然后在下方可度以查看得到“active(running)”,此时说明防火墙已经被打开了。 | |
| 3、在命令行中输入 | |
| systemctl stop firewalld.service | |
| 命令,进行关闭防火墙。 | |
| 4、然后再使用命令 | |
| systemctl status firewalld.service | |
| 在下方出现disavtive(dead),这权样就说明防火墙已经关闭。 | |
| 5、再在命令行中输入命令 | |
| systemctl disable firewalld.service | |
| 命令,即可永久关闭防火墙。 | |
路飞前端部署
把vue项目编译成存粹的静态文件,把ajax远程链接的地址改成服务器的地址]
settings.js
| export default { | |
| BASE_URL:'http://127.0.0.1:80/api/v1' | |
| } |

编译前端
| npm run build |

编译后打包成zip压缩包,传到云服务上去
在云服务器上安装上传下载的软件
| yum install -y lrzsz | |
| rz 选择上传就行 |
解决zip
| yum install unzip -y | |
| unzip dist.zip |
修改Nginx配置文件,实现代理路飞前端
来到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; # 改为自己的域名,没域名修改为127.0.0.1:80 | |
| charset utf-8; | |
| location / { | |
| root /home/html; # html访问路径 | |
| index index.html; # html文件名称 | |
| try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题 | |
| } | |
| } | |
| } |

把dist下所有内容 cp 到/home/html 注意路径
| cp -r dist /home/html |
重启nginx
| ginx -s reload |

访问自己云服务器ip就能看到前端页面了
| http://自己的云服务器ip/ |
路飞后端部署
提交本地代码
把配置文件修改
| Debug模式 | |
| ALLOWED_HOSTS | |
| 点后端地址 | |
| 数据库地址 | |
| redis地址 |
上线配置文件
prov.py
| """ | |
| Django settings for luffy_api project. | |
| Generated by 'django-admin startproject' using Django 3.2.2. | |
| For more information on this file, see | |
| https://docs.djangoproject.com/en/3.2/topics/settings/ | |
| For the full list of settings and their values, see | |
| https://docs.djangoproject.com/en/3.2/ref/settings/ | |
| """ | |
| from pathlib import Path | |
| # Build paths inside the project like this: BASE_DIR / 'subdir'. | |
| BASE_DIR = Path(__file__).resolve().parent.parent | |
| import sys, os | |
| sys.path.insert(0, str(BASE_DIR)) | |
| sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) | |
| # Quick-start development settings - unsuitable for production | |
| # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ | |
| # SECURITY WARNING: keep the secret key used in production secret! | |
| SECRET_KEY = 'django-insecure-t9pg0*4&7qu29p^yth8u@w8puvp6u6475p=*bt8*6q-gabz530' | |
| # SECURITY WARNING: don't run with debug turned on in production! | |
| # DEBUG = True | |
| DEBUG = False | |
| ALLOWED_HOSTS = ['*'] | |
| # Application definition | |
| INSTALLED_APPS = [ | |
| 'simpleui.apps.SimpleApp', | |
| 'django.contrib.admin', | |
| 'django.contrib.auth', | |
| 'django.contrib.contenttypes', | |
| 'django.contrib.sessions', | |
| 'django.contrib.messages', | |
| 'django.contrib.staticfiles', | |
| 'home', | |
| 'user', | |
| 'rest_framework', | |
| 'rest_framework_jwt', | |
| 'corsheaders', | |
| 'course', | |
| 'django_filters', | |
| 'order', | |
| ] | |
| MIDDLEWARE = [ | |
| 'django.middleware.security.SecurityMiddleware', | |
| 'django.contrib.sessions.middleware.SessionMiddleware', | |
| 'django.middleware.common.CommonMiddleware', | |
| 'django.middleware.csrf.CsrfViewMiddleware', | |
| 'django.contrib.auth.middleware.AuthenticationMiddleware', | |
| 'django.contrib.messages.middleware.MessageMiddleware', | |
| 'django.middleware.clickjacking.XFrameOptionsMiddleware', | |
| 'utils.common_middleware.ResponseMiddlerware', | |
| 'corsheaders.middleware.CorsMiddleware' | |
| ] | |
| ROOT_URLCONF = 'luffy_api.urls' | |
| TEMPLATES = [ | |
| { | |
| 'BACKEND': 'django.template.backends.django.DjangoTemplates', | |
| 'DIRS': [], | |
| 'APP_DIRS': True, | |
| 'OPTIONS': { | |
| 'context_processors': [ | |
| 'django.template.context_processors.debug', | |
| 'django.template.context_processors.request', | |
| 'django.contrib.auth.context_processors.auth', | |
| 'django.contrib.messages.context_processors.messages', | |
| ], | |
| }, | |
| }, | |
| ] | |
| WSGI_APPLICATION = 'luffy_api.wsgi.application' | |
| # Database | |
| # https://docs.djangoproject.com/en/3.2/ref/settings/#databases | |
| name = os.environ.get('luffy_name', 'luffy') | |
| password = os.environ.get('luffy_password', 'Luffy123?') | |
| DATABASES = { | |
| # 'default': { | |
| # 'ENGINE': 'django.db.backends.sqlite3', | |
| # 'NAME': BASE_DIR / 'db.sqlite3', | |
| # }, | |
| 'default': { | |
| 'ENGINE': 'django.db.backends.mysql', | |
| 'NAME': 'luffy01', | |
| 'HOST': 'localhost', | |
| 'PORT': 3306, | |
| 'USER': name, | |
| 'PASSWORD': password, | |
| 'CHARSET': 'utf8mb4' | |
| } | |
| } | |
| # Password validation | |
| # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators | |
| AUTH_PASSWORD_VALIDATORS = [ | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | |
| }, | |
| ] | |
| # Internationalization | |
| # https://docs.djangoproject.com/en/3.2/topics/i18n/ | |
| # LANGUAGE_CODE = 'en-us' | |
| LANGUAGE_CODE = 'zh-hans' | |
| # TIME_ZONE = 'UTC' | |
| TIME_ZONE = 'Asia/Shanghai' | |
| USE_I18N = True | |
| USE_L10N = True | |
| # USE_TZ = True | |
| USE_TZ = False | |
| # Static files (CSS, JavaScript, Images) | |
| # https://docs.djangoproject.com/en/3.2/howto/static-files/ | |
| STATIC_URL = '/static/' | |
| # Default primary key field type | |
| # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field | |
| DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' | |
| standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ | |
| '[%(levelname)s][%(message)s]' # 其中name为getlogger指定的名字 | |
| simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' | |
| LOGGING = { | |
| 'version': 1, | |
| 'disable_existing_loggers': False, | |
| 'formatters': { | |
| 'standard': { | |
| 'format': standard_format | |
| }, | |
| 'simple': { | |
| 'format': simple_format | |
| }, | |
| }, | |
| 'filters': {}, # 过滤日志 | |
| 'handlers': { | |
| # 打印到终端的日志 | |
| 'console': { | |
| 'level': 'ERROR', | |
| 'class': 'logging.StreamHandler', # 打印到屏幕 | |
| 'formatter': 'simple' | |
| }, | |
| # 打印到文件的日志,收集info及以上的日志 | |
| 'default': { | |
| 'level': 'ERROR', | |
| 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 | |
| 'formatter': 'standard', | |
| 'filename': os.path.join(os.path.dirname(BASE_DIR), 'log', 'log.log'), # 日志文件 | |
| 'maxBytes': 1024 * 1024 * 5, # 日志大小 5M | |
| 'backupCount': 5, | |
| 'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了 | |
| }, | |
| }, | |
| 'loggers': { | |
| # logging.getLogger(__name__)拿到的logger配置 | |
| 'django': { | |
| 'handlers': ['default', 'console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 | |
| 'propagate': True, # 向上(更高level的logger)传递 | |
| # 'level': 'DEBUG', # 如果debug等级日志不打印不记录,那么把这个加上都,文件记录日志的就能生效了,但是控制台打印的还是不能生效 | |
| # 正常项目不会用这么低,因为django用的debug日志太多了,对于我们来说也没什么用所有就info就行了 | |
| }, # 当键不存在的情况下 (key设为空字符串)默认都会使用该k:v配置 | |
| # '购物车记录': { | |
| # 'handlers': ['default','console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 | |
| # 'level': 'WARNING', | |
| # 'propagate': True, # 向上(更高level的logger)传递 | |
| # }, # 当键不存在的情况下 (key设为空字符串)默认都会使用该k:v配置 | |
| }, | |
| } | |
| REST_FRAMEWORK = { | |
| 'EXCEPTION_HANDLER': 'utils.common_exceptions.exception', | |
| } | |
| AUTH_USER_MODEL = 'user.User' | |
| MEDIA_URL = '/media/' | |
| MEDIA_ROOT = os.path.join(BASE_DIR, 'media') | |
| DB_PREFX = 'luffy' | |
| TENG_CLOUD = { | |
| 'SECRET_ID': os.environ.get('SECRET_ID'), | |
| 'SECRET_KEY': os.environ.get('SECRET_KEY'), | |
| # 短信应用id | |
| 'SMSSDK_APPID': os.environ.get('SMSSDK_APPID'), | |
| # 签名名称 | |
| 'SIGN_NAME': os.environ.get('SIGN_NAME'), | |
| # 模板id | |
| 'TEMPLATE_ID': os.environ.get('TEMPLATE_ID'), | |
| } | |
| CORS_ORIGIN_ALLOW_ALL = True | |
| CORS_ALLOW_METHODS = ( | |
| 'DELETE', | |
| 'GET', | |
| 'OPTIONS', | |
| 'PATCH', | |
| 'POST', | |
| 'PUT', | |
| 'VIEW', | |
| ) | |
| CORS_ALLOW_HEADERS = ( | |
| 'XMLHttpRequest', | |
| 'X_FILENAME', | |
| 'accept-encoding', | |
| 'authorization', | |
| 'content-type', | |
| 'dnt', | |
| 'origin', | |
| 'user-agent', | |
| 'x-csrftoken', | |
| 'x-requested-with', | |
| 'Pragma', | |
| 'token' | |
| ) | |
| CACHES = { | |
| "default": { | |
| "BACKEND": "django_redis.cache.RedisCache", | |
| "LOCATION": "redis://127.0.0.1:6379", | |
| "OPTIONS": { | |
| "CLIENT_CLASS": "django_redis.client.DefaultClient", | |
| "CONNECTION_POOL_KWARGS": {"max_connections": 100} | |
| # "PASSWORD": "123", | |
| } | |
| } | |
| } | |
| BASE_URL = 'http://10.0.0.200:8080' | |
| # 后台基URL | |
| BACKEND_URL = 'http://10.0.0.200:8080' | |
| # 前台基URL | |
| LUFFY_URL = 'http://10.0.0.200:80' | |
| # 支付宝同步异步回调接口配置 | |
| # 后台异步回调接口 | |
| NOTIFY_URL = BACKEND_URL + "/api/v1/order/success/" # 如果这个接口不对,支付宝永远掉不回来,订单状态永远不会改 | |
| # 前台同步回调接口,没有 / 结尾 | |
| RETURN_URL = LUFFY_URL + "/pay/success" | |
| import datetime | |
| JWT_AUTH = { | |
| 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7), | |
| } | |
新建一个manage_pro.py在线上使用这个来迁移数据库
wsgi.py配置修改
| os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffy_api.settings.prov') |
生成项目依赖
| pip freeze >requirements.txt |
提交到git上
| git add . | |
| git commit -m '路飞项目v1上线版' | |
| git push origin master |

部署到服务器上
| mkdir /home/project | |
| cd /home/project | |
| git clone https://gitee.com/xuwuw/luffy_api.git |

配置luffy数据库
管理员链接数据库
| mysql -uroot -p |
创建数据库
| create database luffy default charset=utf8; |
设置权限账户密码:账户密码要与项目配置的一致
| grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?'; | |
| grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?'; | |
| # 刷新权限 | |
| flush privileges; |
退出msyql
| quit |

检查用户
| mysql -uroot -p | |
| Luffy123? | |
| show databases; |

安装项目依赖,迁移数据库
在远端,安装项目依赖
进入到虚拟环境,进入到项目路径下,只需
| workon luffy |
如果mysqlclient装不上,先把mysqlclient注释
| pip install -r requirements.txt |

| yum install mysql-devel -y | |
| yum install python-devel -y | |
| rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 | |
| pip install mysqlclient |
重点 要在虚拟环境中页安装uwsgi
| pip install uwsgi |
迁移的记录文件,要不要提交到git上
官方建议提交
我们自己不推荐:如果多人开发,提交会造成冲突,所有不提交
| 需要修prov.py中的数据库名改为由luffy01修改为luffy |
| python manage_pro.py makemigrations | |
| python manage_pro.py migrate |
迁移成功

录入数据,由于是测试,把本地的sql导出,导入到远端

使用uwsgi启动django
运行django(老司机经验,现有wsgiref运行起来,再用uwsgi跑)
原来测试阶段,使用wsgiref运行
跑起来,访问测试一下,可能访问不到
原因是:安全组没有开启 8888和8000端口
最后期:只留22,3306,6379,80,8080
上线使用uwsgi
| # 测试使用 | |
| # 监听本机所有ip | |
| python manage_pro.py runserver 0.0.0.0:8000 |

使用uwsgi步骤
写一个wusgi的配置文件(ini,[xml]) 我们使用xml
| vim luffyapi.xml | |
| # 写入文件时把注释去掉防止出错 |
| <uwsgi> | |
| <socket>127.0.0.1:8888</socket> <!-- 内部端口,自定义 --> | |
| <chdir>/home/project/luffy_api/</chdir> <!-- 项目路径 --> | |
| <module>luffy_api.wsgi</module> <!-- luffyapi为wsgi.py所在目录名--> | |
| <processes>4</processes> <!-- 进程数 --> | |
| <daemonize>uwsgi.log</daemonize> <!-- 日志文件 --> | |
| </uwsgi> |

使用uwsgi启动django
| uwsgi -x ./luffyapi.xml | |
| ps aux |grep uwsgi | |
| # 看到有四个进程说明没问题 |

现在访问8888没有响应

uWSGI:配置文件是socket,说明它只能监听uwsgi协议,浏览器只能发出http协议,他不能响应
只能http请求发送到nginx上,使用nginx把http请求,转发到uwsgi,nginx支持把http协议转成uwsgi
修改nginx配置文件,完成对http请求的转发
| 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 luffy_api.wsgi; | |
| uwsgi_param UWSGI_CHDIR /home/project/luffy_api/; | |
| } | |
| } |
重启nginx
| nginx -s reload |

uwsgi上线成功

路飞后台管理样式处理
访问后台管理http://服务器ip地址:8080/admin

发现没有样式
uwsgi为了提高性能,只负责处理动态请求,静态资源不管
wgisref 既能访问静态资源,又能访问动态资源 测试阶段
上线后,uwsgi为了提高性能,只处理动态请求【动静分离】
静态资源,需要使用nginx代理
使用步骤
第一步:收集静态资源
simpleui的静态资源
drf的静态资源
前后端项目混合,项目的静态资源
media文件夹资源
第二步 创建文件夹
| mkdir /home/project/luffy_api/luffy_api/static |
第三步修改配置文件prov.py
| STATIC_URL = '/static/' | |
| STATIC_ROOT = '/home/project/luffy_api/luffy_api/static' |
执行这个命令,把静态文件搜集到STATIC_ROOT对应的文件夹下
| python manage_pro.py collectstatic |

修改nginx配置文件
| 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 { | |
| listen 8000; | |
| server_name 127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80 | |
| charset utf-8; | |
| location / { | |
| include uwsgi_params; | |
| uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi里配置的一样 | |
| uwsgi_param UWSGI_SCRIPT luffyapi.wsgi; #wsgi.py所在的目录名+.wsgi | |
| uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 项目路径 | |
| } | |
| # 新增的配置静态文件 | |
| location /static { | |
| alias /home/project/luffy_api/luffy_api/static; | |
| } | |
| location /static { | |
| alias /home/project/luffy_api/luffy_api/media; | |
| } | |
| } | |
| } |
重启nginx
| nginx -s reload |
访问80端口是前端http://服务器ip地址/
访问 8080后端 http://服务器ip地址:8080/admin

以后使用
| python manag_pro.py runserver 0.0.0.0:8888 | |
| # 就运行不了了 |
补充
域名转发
购买域名》备案》域名解析配置
以后访问域名即可

浙公网安备 33010602011771号