美文网首页
Debian9配置Flask环境

Debian9配置Flask环境

作者: 发条蛙 | 来源:发表于2017-11-02 17:48 被阅读0次

在本地使用Ubuntu安装Nginx+uWSGI+Flask安装成功后,想在VPS上进行部署,但VPS上的Debian9安装过程中遇到了一些问题,索性就再描述一下Debian9上边安装Flask的步骤。我的vps上使用用户名clockfrog安装了一个anaconda的环境,因此需要在该环境下进行使用。这里先假定所需要的nginxvirtualenvvirtualenvwrapper都已经准备好了,只描述一些与Ubuntu上的安装不同的关键步骤:

Flask安装配置

  1. 在当前环境的~/.bashrc中的最后有如下两行:
    export PATH="/home/clockfrog/anaconda3/bin:$PATH"
    source /home/clockfrog/anaconda3/bin/virtualenvwrapper.sh
    
  2. 创建虚拟环境:
    mkvirtualenv dataminging
    
  3. 虚拟环境中安装Flask:
    pip install Flask
    
  4. 创建文件~/dataminging/dataminging.py并写入如下内容:
    from flask import Flask
    application = Flask(__name__)
    
    
    @application.route("/")
    def index():
        return "Hello World!\r\n"
    
    if __name__ == "__main__":
        application.run()
    

uWSGI安装配置

  1. 虚拟环境中安装uWSGI:
    pip install uwsgi
    
  2. 创建配置文件/etc/uwsgi/sites/datamining.ini并写入如下内容:
    [uwsgi]
    uid = clockfrog
    gid = www-data
    logto = /var/log/uwsgi.log
    pidfile = /var/run/uwsgi.pid
    
    
    project = datamining
    base = /home/clockfrog
    
    chdir = %(base)/%(project)
    home = %(base)/.virtualenvs/%(project)
    module = %(project):application
    
    master = true
    processes = 8
    
    cheaper = 2
    cheaper-initial = 4
    cheaper-step = 1
    
    cheaper-algo = spare
    cheaper-overload = 4
    
    socket = %(base)/%(project)/%(project).sock
    chmod-socket = 664
    vacuum = true
    

systemd管理uWSGI

  1. 创建systemd管理配置文件/lib/systemd/system/uwsgi.service,内容如下:
    [Unit]
    Description=uWSGI Emperor
    After=syslog.target
    
    [Service]
    ExecStart=/home/clockfrog/.virtualenvs/datamining/bin/uwsgi --ini /etc/uwsgi/sites/datamining.ini
    RuntimeDirectory=uwsgi
    Restart=always
    KillSignal=SIGQUIT
    Type=notify
    StandardError=syslog
    NotifyAccess=all
    
    [Install]
    WantedBy=multi-user.target
    
  2. systemd管理相关命令如下:
    1. 启用systemd对uwsgi的管理:
      systemctl --user enable uwsgi
      
    2. 修改/lib/systemd/system/uwsgi.service后需要重新加载:
      systemctl daemon-reload
      
    3. 启动应用:
      systemctl start uwsgi
      
    4. 关闭应用:
      systemctl stop uwsgi
      

Nginx安装配置

  1. 编辑配置文件/etc/nginx/nginx.conf,在http块中,注释掉如下行:
    include /etc/nginx/conf.d/*.conf;
    
  2. 创建文件/etc/nginx/sites-available/datamining,内容如下:
    server {
        listen 80;
    
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/home/clockfrog/datamining/datamining.sock;
        }
    }
    
  3. 使用如下命令启用firstflask的配置:
    sudo ln -s /etc/nginx/sites-available/datamining /etc/nginx/sites-enabled/
    
    这里务必记得删除目录/etc/nginx/sites-enabled/下的default文件。
  4. 进行配置检查并重启服务:
    sudo service nginx configtest
    sudo service nginx restart
    

安装完成测试

  1. 访问如下链接可以出现Hello World!即可:
    http://localhost
    

相关文章

网友评论

      本文标题:Debian9配置Flask环境

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