1. 安装Nginx
详细步骤请戳。
2. 安装uWSGI
pip install uWSGI
3. 运行uWSGI
根据Flask文档里写的配置是可以运行的,但是有不少缺陷。
- 1.如果你用virtualenv,需要指定python 的home目录;
- 2.Nginx由于权限问题无法读取/tmp/uwsgi.sock文件。
那么上面的命令可以改成:
uwsgi -s /tmp/uwsgi.sock -w flask_file_name:app -H /path/to/virtual/env --chmod-socket 666
4. 配置Nginx
server {
listen 80;
server_name myapp.com;
root /path/to/flask/static;
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
5. 安装supervisor
pip install supervisor
6. 配置supervisor
找到/etc/supervisord.conf,在文件末尾处添加如下内容:
[program:myapp]
command=/path/to/virtual/env/bin/uwsgi -s /tmp/uwsgi.sock -w flask_file_name:app -H /path/to/virtual/env --chmod-socket 666
directory=/path/to/app
autostart=true
autorestart=true
stdout_logfile=/path/to/app/logs/uwsgi.log
redirect_stderr=true
stopsignal=QUIT
7. 管理uWSGI进程
执行supervisroctl可以进入管理控制台
如果希望重新加载uWSGI进程,你可以
supervisorctl reload myapp
如果是重启的话,
supervisorctl restart myapp
更多指令,请参考supervisor手册,如果觉得烦,可以先这篇中文介绍。
本文转载自 http://blog.rebill.info/archives/deploying-Flask-application-with-nginx-uWSGI-and-Supervisor.html。有一些小的改动,不过尚未完全验证,待验证完毕后我会对有疑问或者是错误的地方进行更新的。欢迎各位留言。