From 8c703af2db6c3c74d2c5ee2e215917fc9cb9f48c Mon Sep 17 00:00:00 2001 From: yuzhanglong Date: Fri, 5 Jun 2020 01:37:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E7=BD=B2=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E6=8E=A8=E8=8D=90=E4=BD=BF=E7=94=A8docker?= =?UTF-8?q?=E6=9D=A5=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 7 + README.md | 3 + ...73\347\273\223\347\254\224\350\256\260.md" | 151 +++++++++++++----- gunicorn.py | 1 - 4 files changed, 123 insertions(+), 39 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..aa55245 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.8 +COPY . . +WORKDIR . +RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple \ + && pip install gunicorn -i https://pypi.tuna.tsinghua.edu.cn/simple +CMD ["gunicorn", "-c", "gunicorn.py", "manage:app"] +EXPOSE 5001 \ No newline at end of file diff --git a/README.md b/README.md index 59c9af6..d390e02 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ # EasyQuestionnaire-backend +# 旧的部署方法不推荐了!!!因为略显麻烦,且一不小心容易出错,强烈建议使用docker,轻松、快速、幸福地部署本项目,满满的幸福感~,请查看项目目录下的flask部署总结笔记查看具体方法~ + +#### 最近在折腾别的项目,然后发现本项目的代码有些烂,期末考试考完之后(大概6月20日)准备重构本项目问卷提交、数据分析的业务逻辑 #### 此项目是问卷调查平台的后端项目 基于Python的flask框架 diff --git "a/flask\351\203\250\347\275\262\346\200\273\347\273\223\347\254\224\350\256\260.md" "b/flask\351\203\250\347\275\262\346\200\273\347\273\223\347\254\224\350\256\260.md" index a066776..e2ddafc 100644 --- "a/flask\351\203\250\347\275\262\346\200\273\347\273\223\347\254\224\350\256\260.md" +++ "b/flask\351\203\250\347\275\262\346\200\273\347\273\223\347\254\224\350\256\260.md" @@ -1,71 +1,90 @@ +# flask部署总结笔记 +## 旧的部署方法不推荐了!!!因为略显麻烦,且一不小心容易出错,强烈建议使用docker,轻松、快速、幸福地部署本项目,满满的幸福感~ +## PS.学习docker可以前往docker官网,或者【菜鸟教程】网站,附链接:https://www.runoob.com/docker/docker-tutorial.html -## flask部署总结笔记 +## 在docker上部署项目 -### 处理虚拟环境 -建立虚拟环境 +**安装docker** -``` -virtualenv -p /usr/bin/python3 venv +**docker**的安装十分简单,你只需要执行一行命令: + +```sh +sudo yum install docker ``` -激活虚拟环境 -``` -source venv/bin/activate -``` -安装依赖 +**构建镜像** -``` -pip install -r requirements.txt +通过本地的dockerfile**构建一个镜像**,请进入(CD到)**项目主目录**,执行: + +```sh +docker build -t questionnaire:1.0 ``` -ps. requirements.txt的生成方式 +其中,【questionnaire】为**项目镜像名**,1.0为**项目版本号**,可以自行设置 + + + +**运行容器** +```shell +docker run -p 8080:5001 questionnaire:1.0 ``` -pip freeze > requirements.txt + +项目将myschool **容器内**暴露出的端口(**5001**)映射到**本地服务器**端口(**8080**)上 + + + +**测试部署** + +访问服务器的**8080**端口,如果返回以下信息,说明部署完成: + +```json +{ + "errorCode": 0, + "message": "Your project is running successfully!" +} ``` -### 处理gunicorn +附:下面是本项目的**dockerfile**文件,在**项目根目录**下,可以将项目打包成镜像,**一般不需要修改**: -撰写配置文件(此处比较简略 如要查看完成的配置文件样例 请参阅官方配置样例)位于项目根目录下 +```dockerfile +FROM python:3.8 +COPY . . +WORKDIR . +RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple \ + && pip install gunicorn -i https://pypi.tuna.tsinghua.edu.cn/simple +CMD ["gunicorn", "-c", "gunicorn.py", "manage:app"] +EXPOSE 5001 +``` -https://github.com/benoitc/gunicorn/blob/master/examples/example_config.py -```python -# gunicorn.py +下面是本项目的**gunicorn**配置文件,在**项目根目录**下: + +```python bind = '127.0.0.1:5001' # 绑定ip和端口号 workers = 1 # 进程数 threads = 2 # 指定每个进程开启的线程数 loglevel = 'debug' # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置 -daemon = True # 守护进程 accesslog = "/home/ubuntu/questionnaire/log/gunicorn_access.log" # 访问日志文件 errorlog = "/home/ubuntu/questionnaire/log/gunicorn_error.log" # 错误日志文件 ``` -进入服务器虚拟环境 安装gunicorn -``` -pip install gunicorn -``` -运行app 左边的manage是你的py文件名 右边的app是你的文件里的app名 - -``` -gunicorn -c gunicorn.py manage:app -``` +## 设置转发 - - -### 处理nginx +进行如上操作后,项目部署在了docker容器的**5001**端口,然后映射到服务器的**8080**端口 +之后你可以利用nginx进行转发: 撰写配置文件 位于项目根目录下 @@ -96,18 +115,74 @@ server { include /home/ubuntu/questionnaire/questionnaire.conf; ``` -执行命令 -停止nginx服务 + +## 以下是一般方法(不推荐) + +### 处理虚拟环境 + +建立虚拟环境 ``` -service nginx stop +virtualenv -p /usr/bin/python3 venv ``` -开启nginx服务 +激活虚拟环境 + +``` +source venv/bin/activate +``` + +安装依赖 + +``` +pip install -r requirements.txt +``` + +ps. requirements.txt的生成方式 + +``` +pip freeze > requirements.txt +``` + + + +### 处理gunicorn + +撰写配置文件(此处比较简略 如要查看完成的配置文件样例 请参阅官方配置样例)位于项目根目录下 + +https://github.com/benoitc/gunicorn/blob/master/examples/example_config.py + +```python +# gunicorn.py + +bind = '127.0.0.1:5001' # 绑定ip和端口号 + +workers = 1 # 进程数 +threads = 2 # 指定每个进程开启的线程数 +loglevel = 'debug' # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置 +daemon = True # 守护进程,这里如果你是使用docker部署的,请不要添加 +accesslog = "/home/ubuntu/questionnaire/log/gunicorn_access.log" # 访问日志文件 +errorlog = "/home/ubuntu/questionnaire/log/gunicorn_error.log" # 错误日志文件 ``` -service nginx stop + +进入服务器虚拟环境 安装gunicorn + ``` +pip install gunicorn +``` + +运行app 左边的manage是你的py文件名 右边的app是你的文件里的app名 + +``` +gunicorn -c gunicorn.py manage:app +``` + + + +### 设置转发 + +请参阅上方docker部署的【设置转发】条目。 @@ -131,4 +206,4 @@ kill -9 pid yuzhanglong -记于2020年2月6日00:31:21 \ No newline at end of file +更新于2020年6月5日00:27 \ No newline at end of file diff --git a/gunicorn.py b/gunicorn.py index 95c9111..acbeff9 100644 --- a/gunicorn.py +++ b/gunicorn.py @@ -5,6 +5,5 @@ workers = 1 # 进程数 threads = 2 # 指定每个进程开启的线程数 loglevel = 'debug' # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置 -daemon = True accesslog = "/home/ubuntu/questionnaire/log/gunicorn_access.log" # 访问日志文件 errorlog = "/home/ubuntu/questionnaire/log/gunicorn_error.log" # 错误日志文件