Flask项目部署

在服务器上使用gunicorn + nginx部署Flask项目

Let’s go

软件安装

不同操作系统安装方法可能不同, 以下的安装方法都是Linux系统下的, 其他操作系统可自行百度

  • Python
    安装Python环境, 可自行选择Python2或Python3
  • pip
    安装pip, pip 是一个安装和管理 Python 包的工具, 方便易用
  • Flask
    Flask是一个使用 Python 编写的轻量级 Web 应用框架, 其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2, 也被称为 “microframework” ,因为它使用简单的核心,用 extension 增加其他功能。运行命令pip install flask进行安装
  • gunicorn
    guincorn是支持wsgi协议的http server,与各种Web框架兼容,只需非常简单的执行,轻量级的资源消耗,以及相当迅速, 解决django、flask这些web框架自带wsgi server性能低下的问题, 使用命令pip install gunicorn进行安装
  • nginx
    Nginx(“engine x”)是一个轻量级高性能的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占有内存少,并发能力强, 可实现负载均衡,拦截静态请求, 伪静态化并缓存,减少动态请求数量, 访问控制,限速,限连接数等等; 使用命令sudo apt-get install nginx进行安装

软件配置

  • Flask 应用

新建一个简单的Flask应用,完整项目请看这里>>,应用的目录结构如下,项目主要代码文件为server.py

1
2
3
4
5
6
7
8
9
10
11
12
13
├── gunicornConf.py
├── init.sql
├── server.py
├── static
│   ├── bg.jpg
│   ├── btn_bg.png
│   ├── memory.apk
│   ├── memory.png
│   ├── tip.jpeg
│   └── userguide.pdf
└── templates
├── download.html
└── import.html

  • gunicorn 配置

gunicornConf.py 中对 gunicorn 进行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# *-* coding: utf-8 *-*
import gevent.monkey
import multiprocessing

reload = True # 修改代码时自动重启gunicorn
bind = "0.0.0.0:5000" # 绑定ip和端口
workers = multiprocessing.cpu_count() * 2 # 进程数
threads = multiprocessing.cpu_count() * 2 # 每个进程的线程数
gevent.monkey.patch_all()
worker_class = "gevent"
keepalive = 5
backlog = 2048
proc_name = 'gunicorn.proc'
pidfile = '/tmp/gunicorn.pid'
debug = True
loglevel = "debug"

配置完gunicorn后在项目目录下运行命令gunicorn -c gunicornConf.py server:app运行Flask项目,其中server对应项目文件server.py,而app对应server.py中的应用

  • nginx 配置

nginx 主要用来进行转发以及静态资源代理,安装完nginx后运行命令

1
sudo cp /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/Menory.conf

复制默认配置文件并命名为Memory.conf(文件名自行根据项目进行命名,文件后缀名为conf即可)。(CentOS下默认配置文件为/etc/nginx/conf.d/default.conf
然后以管理员身份编辑Memory.conf,内容如下:

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
server {
# nginx 使用80端口,可自行更改
listen 80 default_server;
listen [::]:80 default_server;

root /home/huangjw/Desktop/MemoryServer; # 项目根目录

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

# 静态资源直接从Nginx发布目录读取。
location ~ /.*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|wav)$ {
root /home/huangjw/Desktop/MemoryServer;
expires 15d; # 设置过期时间15天
}

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://127.0.0.1:5000; # 转发,指向 gunicorn host 的服务地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac).
location ~ /\. {
deny all;
}
}

注意:将nginx的配置文件/etc/nginx/nginx.conf中的include /etc/nginx/sites-enabled/*;一行改为include /etc/nginx/sites-enabled/*.conf;, 因为默认的配置文件和Memory.conf如果都使用80端口会产生冲突。
然后使用命令sudo service nginx restart重启nginx,此时访问服务器80端口的请求都会被转发到5000端口。

后台运行

运行Flask项目后,终端会输出调试信息,但是终端一关闭,gunicorn就会退出,可以使用命令nohup gunicorn -c gunicornConf.py server:app > debug.log &后台运行gunicorn,并将调试信息重定向到文件中,运行该命令后会返回gunicorn的进程号,该进程号也会写进前面gunicornConf.py中定义的/tmp/gunicorn.pid中,可以使用命令kill $(cat /tmp/gunicorn.pid)退出gunicorn。如果想要查看调试信息,可以使用命令tail -f debug.log实时查看调试信息。

主要链接

文章目录
  1. Let’s go
    1. 软件安装
    2. 软件配置
    3. 后台运行
  2. 主要链接
|
-->