flask 怎么使用nginx + uwsgi 部署

使用flask开发一个应用,需要部署,可是按照网络上的一些blog总是会报各种错误,实在配置不好了,烦请大叫指教。

使用的是阿里云服务器,Ubuntu 12.04.4 LTS。

大致步骤是模仿开源中国步骤完成的,如果完全按照原来的步骤进行是可以访问的,但是如果更改成我只得代码就会报
502 Bad Gateway
我的代码是存放在Github
-----下面是我修改之后的配置文件。
1. 创建/var/www/flask-blog-test/flask-blog-test.conf

server {
    listen      80;
    server_name localhost;
    charset     utf-8;
    client_max_body_size 75M;

    location /static {
        root /var/www/flask-blog-test/;
    }

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/flask-blog-test/flask-blog-test.sock;
    }
}
ln -s /var/www/flask-blog-test/flask-blog-test.conf /etc/nginx/conf.d/
/etc/init.d/nginx restart
  1. 创建/var/www/flask-blog-test/flask-blog-test-uwsgi.ini
[uwsgi]
#application's base folder
base = /var/www/flask-blog-test

#python module to import
app = app
module = %(app)

home = %(base)/venv
pythonpath = %(base)

#socket file's location
socket = /var/www/flask-blog-test/%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/log/uwsgi/%n.log

```
sudo mkdir -p /var/log/uwsgi


3. 启动uwsgi

uwsgi --ini /var/www/flask-blog-test/flask-blog-test-uwsgi.ini


4. 文件目录是

flask-blog-test

│──── venv
│ │──── bin
│ │────include
│ │──── lib
│ │──── local
│────app.py

│────static
│────templates
│────flask-blog-test-uwsgi.ini
│────flask-blog-test.conf

阅读 17.4k
1 个回答

nginx+uwsgi+python+flask给你一个参考吧

1.修改/etc/nginx/nginx.conf文件

...

http {
    ...
    ##
    # Virtual Host Configs
    ##

    #include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
}

默认配置基础上改了Virtual Host Configs

include /etc/nginx/sites-enabled/*.conf;

2.创建/etc/nginx/sites-enabled/www.xxx.com.conf文件

server {
    listen 80;
    server_name www.xxx.com xxx.com;
    access_log /var/log/nginx/www.xxx.com.access.log;
    error_log /var/log/nginx/www.xxx.com.error.log;
    location /
    {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi_www.xxx.com.sock;
    }
}

3.创建/etc/uwsgi/apps-enabled/www.xxx.com.ini文件

[uwsgi]
plugins = python
vhost = true
chmod-socket = 666
socket = /tmp/uwsgi_www.xxx.com.sock
venv = /var/www/www.xxx.com/env
chdir = /var/www/www.xxx.com
module = runserver
callable = app

用了virtualenv:

venv = /var/www/www.xxx.com/env

这是代码部署的目录:

chdir = /var/www/www.xxx.com

4.代码的结构

代码的目录是这样的:

├── env
│   ├── bin
│   ├── include
│   ├── lib
│   └── local
├── runserver.py
└── abc
    ├── __init__.py
    ├── models.py
    ├── static
    ├── templates
    └── views.py

代码目录里有一个runserver.py:

#coding=utf-8
from abc import app
...

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

其中appFlask对象

from flask import Flask
app = Flask(__name__)

5.修改权限

sudo chmod -R 775 /var/www/www.xxx.com/
sudo chown -R www-data:www-data /var/www/www.xxx.com/

6.记得安装uwsgi-plugin-python

sudo apt-get install uwsgi-plugin-python
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏