flask+uwsgi+nginx简单部署应用

  • 1,创建虚拟环境: mkvirtualenv flask_uwsgi_demo

  • 2,安装flask : pip install flask

  • 3,安装和配置uwsgi:pip install uwsgi

    vim uwsgi.ini

    [uwsgi]
    #使用nginx连接时使用
    socket=0.0.0.0:10000
    #这个必须写,不然会报找不到application
    callable = app    
    #项目目录绝对路径
    chdir=/project/flask_demo
    #指定flask运行的文件,是一个相对路径
    wsgi-file =flask_uwsgi_demo.py   
    #指定解释器目录,每一个虚拟环境的python目录不同,用which python查看
    home=/root/Envs/flask_uwsgi_demo
    processes=4
    threads=2
    master=True
    pidfile=uwsgi.pid
    
  • 4,配置nginx: vi /etc/nginx/nginx.conf

    server {
        listen       80;
        server_name  10.0.0.11;  #(注意不带斜杠,加引号)
        location /index {
            include uwsgi_params; 
            uwsgi_pass  10.0.0.11:10000;
            }
        }
    
  • 5,编写flask:vim flask_uwsgi_demo.py

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route("/index")
    def helloWorld():
        return "Hello World"
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=10000)
    
  • 6,启动uwsgi,启动nginx,访问

    (flask_uwsgi_demo) [root@slave flask_demo]# uwsgi --ini uwsgi.ini 
    
    [root@slave project]# /usr/sbin/nginx    #如果是重启,加上-s reload
    
    10.0.0.11/index
    
  • 7,目录结构

    [root@slave project]# tree
    ├── flask_demo
        ├── flask_uwsgi_demo.py
        ├── __pycache__
        │   └── flask_uwsgi_demo.cpython-34.pyc
        ├── uwsgi.ini
        └── uwsgi.pid
    
  • 8,启动uwsgi报错

    以下报错,没有进入虚拟环境,或者uwsgi.ini配置错误,没找对环境

    *** Operational MODE: preforking+threaded ***
    Traceback (most recent call last):
      File "./flask_uwsgi_demo.py", line 1, in 
        from flask import Flask
    ImportError: No module named 'flask'
    unable to load app 0 (mountpoint='') (callable not found or import error)
    

    以下报错,没有找到app,主要是配置文件错误,我添加了callable = app,问题解决

    *** Operational MODE: preforking+threaded ***
    unable to find "application" callable in file flask_uwsgi_demo.py
    unable to load app 0 (mountpoint='') (callable not found or import error)
    *** no app loaded. going in full dynamic mode ***
    uWSGI running as root, you can use --uid/--gid/--chroot options
    

    以下正确

    *** Operational MODE: preforking+threaded ***
    WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x121ff90 pid: 13743 (default app)
    

你可能感兴趣的:(flask+uwsgi+nginx简单部署应用)