Flask 应用配置 Nginx 和 uwsgi

安装

$ sudo apt-get install nginx
$ sudo apt-get install python-pip
$ sudo pip install flask uwsgi

创建一个 Flask 应用

$ cd ~
$ mkdir blog
$ cd blog
$ touch views.py uwsgi.ini blog.sock

文件内容:

views.py

from flask import Flask  
app = Flask(__name__)

@app.route('/')
def blog():  
    return 'Hello, world!'

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

uwsgi.ini

[uwsgi]
#application's base folder
base = /home/shaobing/blog

#socket file's location
socket = /home/shaobing/blog/blog.sock

#permissions for the socket file
chmod-socket = 666

#the variable that holds a flask application inside the module imported at line #6
wsgi-file = views.py  
callable = app

#location of log files
logto = uwsgi.log  

Nginx 配置

$ sudo vim /etc/nginx/sites-available/blog
文件内容,在server_name 填上域名或IP(pengfei.com)(域名已解析到此服务器IP)
server {  
    listen 80;
    server_name pengfei.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/shaobing/blog/blog.sock;
    }
}
$ sudo ln -s /etc/nginx/sites-avialable/blog /etc/nginx/sites-enabled/
$ sudo service nginx reload

启动 uwsgi

$ cd ~/blog
$ uwsgi --ini uwsgi.ini &

在浏览器打开 http://pengfei.com 即可访问。

Wang Shaobing

继续阅读此作者的更多文章