nginx+uwsgi+flask快速部署

简单部署流程:

1、在centos安装python及pip,

pip安装地址:http://pip.readthedocs.org/en/latest/installing.html

安装步骤比较简单,执行:

python get-pip.py

2、安装flask和uwsgi包,命令如下:

pip install uwsgi

pip install flask

3、添加flask的文件,以hello world为例:

1
2
3
4
5
6
7
8
9
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World!'

if __name__ == '__main__':
app.run()

保存为hello_flask.py文件,然后添加run文件:

1
2
3
from hello_flask import app
if __name__ == "__main__":
app.run()

保存为 WSGI.py 文件

4、配置uwsgi,添加uwsgi配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[uwsgi]
#application's base folder
base = /var/python/hello_flask

#python module to import
app = hello_flask
module = WSGI

pyargv = -e Production
pythonpath = %(base)

#socket file's location
socket = /tmp/%n.sock

#permissions for the socket file
chmod-socket = 666

#the variable that holds a flask application inside the module imported at line #6
callable = app

#location of log files
logto = /var/python/hello_flask/%n.log

保存为flask_uwsgi.ini

5、最后配置nginx,相关配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server{
server_name www.helloflask.com;
listen 80;

root /var/python/hello_flask;

location ~ ^/static/ {
root /var/python/hello_flask;
access_log off;
expires 4d;
}

location / { try_files $uri @hello; }
location @hello{
include uwsgi_params;
uwsgi_pass unix:/tmp/flask_uwsgi.sock;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}

然后重启启动nginx,加载配置

6、最后启动uwsgi服务,让外网能够访问

uwsgi -d –ini flask_uwsgi.ini

这样就在后台启动uwsgi服务,至此简单配置完毕,外网就可以访问我们的flask网站了。