1,目的
在生产环境下,可以通过Nginx+uwsgi+Flask部署Web服务,从而达到高并发高稳定性的要求。
如果要部署多个APP,可以采用单个Nginx,多个uwsgi+Flask的方式来实现,如下图所示。
2,安装过程
2.1,升级软件包
sudo apt-get update
2.2,安装virtualenv和python环境
sudo apt-get install build-essential python-dev python-pip
sudo pip install virtualenv
2.3,在virtualenv中部署flask app,并测试
- 创建存放网站的目录
mkdir mysite
- 配置virtualenv和安装flask
进入mysite目录,然后创建虚拟环境.env,激活虚拟环境,然后安装flask
cd mysite
virtualenv .env # 创建Python虚拟环境
source .env/bin/activate # 进入Python虚拟环境,退出命令是deactivate
pip install flask # 在虚拟环境下安装flask
- 在mysite目录下创建hello.py
from flask import Flask
app = Flask(__name__)
@app.route("/app1/")
def hello():
return "Hello World!"
@app.route("/app1/flask/")
def hello_flask():
return "Hello World! Hello Flask!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)
需要注意的是,app.run()只是开发时测试使用,故需要放置在if __name__ == "__main__"
下,这样uwsgi才不会执行app.run()方法。而host需要设置为0.0.0.0,表示让flask监听机器的所有ip地址的8080端口。
- 启动测试
执行以下命令,可以启动Flask。通过浏览器访问192.168.1.32:8080/app1/,如果返回“Hello World!”,则证明启动OK。
python hello.py
2.4,在virtualenv中部署uwsgi,并测试
- 进入到Python虚拟环境,并安装uwsgi
source .env/bin/activate # 进入Python虚拟环境,退出命令是deactivate
pip install uwsgi # 在虚拟环境下安装uwsgi
- 创建uwsgi目录,做好目录规划如下
(.env) kevin@orange:~/web/flask/mysite$ tree .
.
├── hello.py
├── hello.pyc
├── uwsgi
│ ├── uwsgi.log
│ ├── uwsgi.pid
│ ├── uwsgi.sock
│ └── uwsgi.status
└── uwsgi.ini
- 修改uwsgi配置文件
(.env) kevin@orange:~/web/flask/mysite$ vi uwsgi.ini
[uwsgi]
chdir=/home/kevin/web/flask/mysite/
home=/home/kevin/web/flask/mysite/.env
module=hello
callable=app
master=true
processes=2
chmod-socket=666
logfile-chmod=644
uid=kevin_web
gid=kevin_web
procname-prefix-spaced=mysite
py-autoreload=1
#http=0.0.0.0:8080
vacuum=true
socket=%(chdir)/uwsgi/uwsgi.sock
stats=%(chdir)/uwsgi/uwsgi.status
pidfile=%(chdir)/uwsgi/uwsgi.pid
daemonize=%(chdir)/uwsgi/uwsgi.log
配置参数的含义,可参考http://www.jianshu.com/p/c3b13b5ad3d7
- 启动uwsgi(在虚拟环境下),并测试
(.env) kevin@orange:~/web/flask/mysite$ uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
(.env) kevin@orange:~/web/flask/mysite$ ps -ef | grep mysite
zhangsh+ 2270 1 0 16:15 ? 00:00:00 mysite uWSGI master
zhangsh+ 2273 2270 0 16:15 ? 00:00:00 mysite uWSGI worker 1
zhangsh+ 2274 2270 0 16:15 ? 00:00:00 mysite uWSGI worker 2
zhangsh+ 2278 2171 0 16:15 pts/1 00:00:00 grep --color=auto mysite
2.5,安装nginx,并配置测试
- 安装nginx(不在python虚拟环境下)
sudo apt-get install nginx
- 编辑配置文件:/etc/nginx/conf.d/flask.conf
server {
listen 81;
server_name www.mysite.com;
charset utf-8;
client_max_body_size 5M;
location /app1/ {
include uwsgi_params;
uwsgi_pass unix:/home/kevin/web/flask/mysite/uwsgi/uwsgi.sock;
}
location /static {
alias /home/kevin/web/flask/mysite/static;
}
}
- nginx启动测试
kevin@orange:~/web/flask/mysite$ sudo service nginx start
kevin@orange:~/web/flask/mysite$ ps -ef | grep nginx
root 2324 1 0 16:19 ? 00:00:00 nginx: master process /usr/sbin/nginx
www-data 2325 2324 0 16:19 ? 00:00:00 nginx: worker process
www-data 2326 2324 0 16:19 ? 00:00:00 nginx: worker process
www-data 2327 2324 0 16:19 ? 00:00:00 nginx: worker process
www-data 2328 2324 0 16:19 ? 00:00:00 nginx: worker process
zhangsh+ 2330 2171 0 16:20 pts/1 00:00:00 grep --color=auto nginx
2.6,服务测试
- Http访问测试,一切OK
kevin@Blue:~$ curl http://192.168.1.32:81/app1/flask/
Hello World! Hello Flask!
kevin@Blue:~$ curl http://192.168.1.32:81/app1/
Hello World!
- 浏览器访问测试,一切OK
3,参考资料
-
如何理解Nginx, WSGI, Flask之间的关系
http://blog.csdn.net/lihao21/article/details/52304119 -
使用virtualenv, uwsgi, nginx来布署flask
http://blog.csdn.net/lihao21/article/details/47731903 -
Serving Flask With Nginx
http://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/
网友评论