Flask+uwsgi+Nginx deploy application
I wrote a website with Flask before, and then I wanted to deploy it to a cloud host. After searching for a while, the deployment plan I decided to adopt is:
- Web server using uWSGI
- Use Supervisor to reference uwsgi as a regular startup service
- Reverse proxy based on Nginx
Install Python environment, Flask
I applied for a cloud host on Vultr, and the system uses Ubuntu 15.10. Come up to install the python environment first, install pip, virtualenv. Create a new virtual environment venv and activate it:
wb@vultr:~/myBlog$ source venv/bin/activate
(venv) wb@vultr:~/myBlog$
Next, install Flask and Flask's dependencies, which depend on the project.
The startup file manage.py of my project is as follows:
import os
if os.path.exists('.env'):
print('Importing environment from .env...')
for line in open('.env'):
var = line.strip().split('=')
if len(var) == 2:
os.environ[var[0]] = var[1]
from app import create_app, db
from app.models import User, Post
from flask.ext.script import Manager,Shell
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
def make_shell_context():
return dict(app=app,db=db,User=User)
manager.add_command("shell",Shell(make_context=make_shell_context))
if __name__=='__main__':
manager.run()
So the way to run Flask is as follows:
python manage.py runserver --host 0.0.0.0
Then open the browser, enter your cloud host IP and port number (5000) such as: 1.2.3.4:5000
, you can see your website.
Install uWSGI
The way to install uWSGI is also very simple, pip install it:
(venv) wb@vultr:~/myBlog$pip install uwsgi
After installation is complete configuration. config.ini
Create a new project in the project directory config.ini
and enter the following in it:
[uwsgi]
master = true
home = venv
wsgi-file = manage.py
callable = app
socket = :3031
processes = 4
threads = 2
buffer-size = 32768
Among them, home
is the directory of the virtual environment, wsgi-file
is the python startup file, callable=app
this app is a variable in the manage.py program file, and the type of this variable is the application class of Flask. socket
Specifies the port to use at startup. I wrote it before http = 0.0.0.0:3031
, but I found that it didn't work. After checking Stack Overflow, I changed it to socket.
Run uwsgi:
(venv) wb@vultr:~/myBlog$uwsgi config.ini
[uWSGI] getting INI configuration from config.ini
*** Starting uWSGI 2.0.12 (64bit) on [Thu Apr 21 08:41:52 2016] ***
compiled with version: 5.2.1 20151010 on 19 April 2016 02:03:42
At this point, uWSGI has been started normally and the Flask project has been loaded into it. ctrl+c closes the program. 1.2.3.4:3031
At this point, you can see the project by typing in the browser . But this is only the command startup form, and it cannot be accessed when it is closed. To have it start with the server and run as a background service is what the runtime environment actually needs. Therefore, additional tools are needed to bootstrap uWSGI.
Install supervisor
我们使用supervisor引导uWSGI。安装方法:
sudo apt-get install supervisor
supervisor的全局配置文件在/etc/supervisor/supervisor.conf
中,但是我们一般不改动,我们新建一个文件blog_supervisor.conf
放在/etc/supervisor/conf.d
目录下,里面输入以下内容:
[program:blog]
# 启动命令入口
command=/home/wb/myBlog/venv/bin/uwsgi /home/wb/myBlog/config.ini
# 命令程序所在目录
directory=/home/wb/myBlog
#运行命令的用户名
user=wb
autostart=true
autorestart=true
#日志地址
stdout_logfile=/home/wb/myBlog/logs/uwsgi_supervisor.log
启动supervisor服务:sudo service supervisor start
安装Nginx
Nginx是个著名的反向代理服务器。安装方法:
sudo apt-get install nginx
配置文件在/etc/nginx/sites-available
中的default
中,将其替换掉即可。新的default
文件内容如下:
server {
listen 80;
server_name X.X.X.X; #公网地址
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
uwsgi_param UWSGI_PYHOME /home/wb/myBlog/venv; # 指向虚拟环境目录
uwsgi_param UWSGI_CHDIR /home/wb/myBlog; # 指向网站根目录
uwsgi_param UWSGI_SCRIPT manage:app; # 指定启动程序
uwsgi_read_timeout 100;
}
}
重启Nginx服务:sudo service nginx restart
这时候打开浏览器,输入你的主机IP,就可以看到你的项目了。