Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
更新部署方式,推荐使用docker来部署
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhanglong committed Jun 4, 2020
1 parent d417aee commit 8c703af
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 39 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# EasyQuestionnaire-backend
# 旧的部署方法不推荐了!!!因为略显麻烦,且一不小心容易出错,强烈建议使用docker,轻松、快速、幸福地部署本项目,满满的幸福感~,请查看项目目录下的flask部署总结笔记查看具体方法~

#### 最近在折腾别的项目,然后发现本项目的代码有些烂,期末考试考完之后(大概6月20日)准备重构本项目问卷提交、数据分析的业务逻辑

#### 此项目是问卷调查平台的后端项目 基于Python的flask框架

Expand Down
151 changes: 113 additions & 38 deletions flask部署总结笔记.md
Original file line number Diff line number Diff line change
@@ -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进行转发:

撰写配置文件 位于项目根目录下

Expand Down Expand Up @@ -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部署的【设置转发】条目。



Expand All @@ -131,4 +206,4 @@ kill -9 pid

yuzhanglong

记于2020年2月6日00:31:21
更新于2020年6月5日00:27
1 change: 0 additions & 1 deletion gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" # 错误日志文件

0 comments on commit 8c703af

Please sign in to comment.