美文网首页
部署flask+gunicorn+nginx

部署flask+gunicorn+nginx

作者: windvalley | 来源:发表于2017-03-22 17:52 被阅读0次

    安装python3.4

    wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tar.xz
    tar xf Python-3.4.1.tar.xz
    ./configure --prefix=/usr/local/python3.4 -j4
    make -j4 && make install
    echo "export PATH=$PATH:/usr/local/python3.4/bin" >>/etc/profile
    

    安装flask

    现在开始安装 Flask 以及一些我们会用到的扩展。首选的方式就是创建一个虚拟环境,这个虚拟环境能够安装所有的东西,而你的系统主Python不会受到影响。另外一个好处就是这种方式不需要你拥有root权限。

    mkdir microblog
    cd microblog/
    python3 -m venv flask
    cd flask/
    bin/pip install flask
    bin/pip install flask-login
    bin/pip install flask-openid
    bin/pip install flask-mail
    bin/pip install flask-sqlalchemy
    bin/pip install sqlalchemy-migrate
    bin/pip install flask-whooshalchemy
    bin/pip install flask-wtf
    bin/pip install flask-babel
    bin/pip install guess_language
    bin/pip install flipflop
    bin/pip install coverage
    

    写一个hello world程序

    cd microblog
    mkdir -p app/static 
    mkdir -p app/templates
    mkdir tmp
    
    #app/__init__.py
    
    from flask import Flask
    
    app = Flask(__name__)
    from app import views
    
    #app/views.py
    
    from app import app
    
    @app.route('/')
    @app.route('/index')
    def index():
    return "Hello, World!"
    

    安装gunicorn

    gunicorn,是一种web服务器网关接口,类似wsgi和uwsgi

    flask/bin/pip install  gunicorn
    flask/bin/pip freeze  #看安装过哪些包
    flask/bin/gunicorn -w4 -b127.0.0.1:5000 app:app  #启动flask web实例
    

    安装nginx

    使用openresty

    yum install -y zlib zlib-devel pcre  pcre-devel
    wget https://openresty.org/download/openresty-1.11.2.1.tar.gz --no-check-certificate 
    
    ./configure --prefix=/usr/local/openresty -j4
    gmake -j4
    gmake install
    
    #nginx.conf
    
    server {
        listen 80;
        server_name example.org;
        access_log  logs/host.access.log  main;
    
        location / {
            proxy_pass http://127.0.0.1:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }
    
    /usr/local/openresty/bin/openresty  -t
    /usr/local/openresty/bin/openresty
    

    相关文章

      网友评论

          本文标题:部署flask+gunicorn+nginx

          本文链接:https://www.haomeiwen.com/subject/ufnonttx.html