简介
WSGI协议
Flask致力于如何生成HTML代码,而Web服务器用于处理和响应HTTP请求。Flask和Web服务器之间的通信,需要一套双方都遵守的接口协议。WSGI协议就是用来统一这两者的接口的。
WSGI容器——Gunicorn
常用的WSGI容器有Gunicorn和uWSGI,性比较而言,Gunicorn配置简单,性能优越。
Nginx
Nginx是一款轻量级的Web 服务器/反向代理服务器。
假设我们有一个todos的项目,项目结构如下:
gavinsun:python-flask-todos gavinsun$ tree . ├── README.markdown # 说明文档 ├── requirements.txt # 环境需求 ├── test.py # 测试文件 └── todos #todos应用包 ├── __init__.py ├── static # 静态资源 │ └── style.css ├── templates # 模板 │ └── todos.html ├── todos.db # 数据库文件 ├── todos.py # todos应用 ├── todos.sublime-workspace └── wsgi.py # 用来启动todos应用
wsgi.py代码如下:
from todos import app if __name__ == '__main__': app.run()
不使用Gunicorn,直接启动app的方法如下:
(.venv) gavinsun:python-flask-todos gavinsun$ python wsgi.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 281-552-583
这样虽然能过运行,但是,性能太差,使用Gunicorn性能会好很多。
使用Gunicorn启动app
(.venv) gavinsun:python-flask-todos gavinsun$ pip install gunicorn (.venv) gavinsun:python-flask-todos gavinsun$ gunicorn todos.todos:app [2018-04-07 10:48:30 +0800] [3458] [INFO] Starting gunicorn 19.7.1 [2018-04-07 10:48:30 +0800] [3458] [INFO] Listening at: http://127.0.0.1:8000 (3458) [2018-04-07 10:48:30 +0800] [3458] [INFO] Using worker: sync [2018-04-07 10:48:30 +0800] [3461] [INFO] Booting worker with pid: 3461 或者 (.venv) gavinsun:python-flask-todos gavinsun$ cd todos/ (.venv) gavinsun:todos gavinsun$ gunicorn wsgi:app [2018-04-07 10:49:46 +0800] [3465] [INFO] Starting gunicorn 19.7.1 [2018-04-07 10:49:46 +0800] [3465] [INFO] Listening at: http://127.0.0.1:8000 (3465) [2018-04-07 10:49:46 +0800] [3465] [INFO] Using worker: sync [2018-04-07 10:49:46 +0800] [3468] [INFO] Booting worker with pid: 3468
部署过程
创建专门用于部署Web应用的用户
$ useradd deploy $ mkdir /home/deploy $ chown deploy:deploy /home/deploy $ usermod -a -G sudo deploy $ passwd deploy $ chsh -s /bin/bash deploy
准备工作,安装如下工具
sudo apt install ufw python-dev python-virtualenv python-pip git nginx supervisor
安装虚拟环境(一个项目对应一个虚拟环境)
$ mkdir ~/.virtualenvs && cd ~/.virtualenvs $ virtualenv flask-todos $ source ~/.virtualenvs/flask-todos/bin/activate
下载项目代码,安装requirements.txt
(flask-todos)$ mkdir ~/sites && cd ~/sites (flask-todos)$ git clone https://github.com/alexandersimoes/flask-todos.git (flask-todos)$ cd flask-todos (flask-todos)$ pip install -r requirements.txt
安装Nginx
sudo apt install nginx
配置Nginx
(flask-todos)$ sudo rm /etc/nginx/sites-enabled/default (flask-todos)$ sudo nano /etc/nginx/sites-available/flask-todos
flask-todos配置文件如下:
server { listen 80; server_name 97.64.81.242; root /home/deploy/sites/flask-todos; access_log /home/deploy/sites/flask-todos/logs/nginx/access.log; error_log /home/deploy/sites/flask-todos/logs/nginx/error.log; location / { proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://127.0.0.1:8000; break; } } location /config{ alias /home/gavin/config/; autoindex on; } location /static { alias /home/deploy/sites/flask-todos/todos/static/; autoindex on; } }
创建用于保存nginx log的文件路径
(flaskdeploy)$ mkdir -p ~/sites/flaskdeploy/logs/nginx
创建符号链接,启用flask-todos配置
(flaskdeploy)$ sudo ln -s /etc/nginx/sites-available/flaskdeploy /etc/nginx/sites-enabled/ (flaskdeploy)$ sudo nginx -t (flaskdeploy)$ sudo service nginx restart
关于sites-available和sites-enabled的说明:
The sites-available folder is for storing all of your vhost configurations, whether or not they're currently enabled. The sites-enabled folder contains symlinks to files in the sites-available folder. This allows you to selectively disable vhosts by removing the symlink. You should edit files only in sites-available directory. Do never edit files inside the sites-enabled directory, otherwise you can have problems if your editor runs out of memory or, for any reason, it receives a SIGHUP or SIGTERM.
总之,在sites-available中创建网站的配置文件,在sites-enabled中创建要启用的网站的配置文件的符号链接。千万不要在sites-enabled中直接创建配置文件。
使用supervisor管理 gunicorn 命令
安装
sudo apt install supervisor
把flask-todos启动命令添加到supervisor中进行管理
创建配置文件flask-todos
sudo vi /etc/supervisor/conf.d/flask-todos.conf
在flask-todos.conf中添加如下内容
[program:flask-todos] command = /home/deploy/.virtualenvs/flask-todos/bin/gunicorn wsgi:app -w 2 directory = /home/deploy/sites/flask-todos/todos user = deploy stdout_logfile = /home/deploy/sites/flask-todos/logs/gunicorn/gunicorn_stdout.log stderr_logfile = /home/deploy/sites/flask-todos/logs/gunicorn/gunicorn_stderr.log redirect_stderr = True environment = PRODUCTION=1
执行下面3条命令,如果没有报错,flask-todos将以后台服务的形式启动。
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start flask-todos
至此,部署完毕!