美文网首页
[Centos7]Gunicorn部署Falsk,并用Super

[Centos7]Gunicorn部署Falsk,并用Super

作者: 司马天 | 来源:发表于2019-08-06 13:54 被阅读0次

    环境安装

    安装Python3.6.8(用root安装)

    安装依赖库

    yum -y install git gcc make patch zlib-devel gdbm-devel openssl-devel sqlite-devel bzip2-devel readline-devel
    

    下载Python3.6.8安装包

    wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
    

    创建安装目录

    mkdir -p /usr/local/python3
    

    解压安装包

    tar -zxvf Python-3.6.8.tgz
    

    进入解压后的目录,编译安装

    cd Python-3.6.8
    ./configure --prefix=/usr/local/python3
     make && make install
    

    建立python3的软链接

    ln -s /usr/local/python3/bin/python3 /usr/bin/python3
    

    将/usr/local/python3/bin加入环境变量

    vi ~/.bashrc
    export PATH=$PATH:/usr/local/python3/bin
    source ~/.bashrc
    

    修改Python3的用户以及组

    chown -R maxwell /usr/local/python3/
    chgrp -R maxwell /usr/local/python3/
    

    安装项目依赖包(离线安装,事先准备好安装文件)

    安装flask及其依赖包

    pip3 install Click-7.0-py2.py3-none-any.whl
    pip3 install Werkzeug-0.14.1-py2.py3-none-any.whl 
    pip3 install itsdangerous-1.1.0-py2.py3-none-any.whl 
    pip3 install MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
    pip3 install Jinja2-2.10.1-py2.py3-none-any.whl
    pip3 install Flask-1.0.3-py2.py3-none-any.whl
    

    安装tensorflow及其依赖包

    tar -xzvf PyYAML-5.1.2.tar.gz
    cd PyYAML-5.1.2/
    python3 setup.py install
    
    tar -xzvf gast-0.2.2.tar.gz
    cd gast-0.2.2/
    python3 setup.py install
    
    tar -xzvf termcolor-1.1.0.tar.gz
    cd termcolor-1.1.0/
    python3 setup.py install
    
    tar -xzvf absl-py-0.7.1.tar.gz
    cd absl-py-0.7.1/
    python3 setup.py install
    
    pip3 install six-1.12.0-py2.py3-none-any.whl
    pip3 install numpy-1.17.0-cp36-cp36m-manylinux1_x86_64.whl
    pip3 install h5py-2.9.0-cp36-cp36m-manylinux1_x86_64.whl
    pip3 install scipy-1.3.0-cp36-cp36m-manylinux1_x86_64.whl
    pip3 install Keras-2.1.6-py2.py3-none-any.whl
    pip3 install Keras_Applications-1.0.6-py2.py3-none-any.whl 
    pip3 install grpcio-1.22.0-cp36-cp36m-manylinux1_x86_64.whl
    pip3 install wheel-0.33.4-py2.py3-none-any.whl 
    pip3 install astor-0.8.0-py2.py3-none-any.whl
    pip3 install protobuf-3.9.0-cp36-cp36m-manylinux1_x86_64.whl 
    pip3 install Markdown-3.1.1-py2.py3-none-any.whl
    pip3 install setuptools-41.0.1-py2.py3-none-any.whl
    pip3 install tensorboard-1.12.0-py3-none-any.whl 
    pip3 install Keras_Preprocessing-1.1.0-py2.py3-none-any.whl
    pip3 install tensorflow-1.12.0-cp36-cp36m-manylinux1_x86_64.whl
    

    安装pandas及其依赖包

    pip3 install python_dateutil-2.8.0-py2.py3-none-any.whl
    pip3 install pytz-2019.2-py2.py3-none-any.whl 
    pip3 install pandas-0.24.2-cp36-cp36m-manylinux1_x86_64.whl
    

    安装gunicorn

    pip3 install gunicorn-19.9.0-py2.py3-none-any.whl
    

    安装requests及其依赖包

    pip3 install urllib3-1.24-py2.py3-none-any.whl
    pip3 install idna-2.8-py2.py3-none-any.whl
    pip3 install certifi-2019.6.16-py2.py3-none-any.whl 
    pip3 install chardet-3.0.4-py2.py3-none-any.whl 
    pip3 install requests-2.22.0-py2.py3-none-any.whl
    

    安装xlwt

    pip3 install xlwt-1.3.0-py2.py3-none-any.whl
    

    安装xlrd

    pip3 install xlrd-1.2.0-py2.py3-none-any.whl
    

    安装ibm_db

    tar -xzvf ibm_db-3.0.1.tar.gz 
    cd ibm_db-3.0.1/
    python3 setup.py install  # 此步需要联网,需要下载驱动
    

    安装gevent及其依赖包

    pip3 install greenlet-0.4.15-cp36-cp36m-manylinux1_x86_64.whl 
    pip3 install gevent-1.4.0-cp36-cp36m-manylinux1_x86_64.whl
    

    安装supervisor及其依赖包

    pip3 install meld3-1.0.2-py2.py3-none-any.whl 
    pip3 install supervisor-4.0.4-py2.py3-none-any.whl
    

    gunicorn配置,在项目根目录创建gc.py,编辑如下内容

    import gevent.monkey
    gevent.monkey.patch_all()
    
    import multiprocessing
    
    # 调试用,生产环境设为False
    # debug = True
    
    # 绑定地址:端口
    bind = "0.0.0.0:8888"
    
    # gunicorn进程ID日志
    pidfile = "log/gunicorn.pid"
    
    # loglevel用于控制errorlog的信息级别,可以设置为debug、info、warning、error、critical
    loglevel = "debug"
    
    # accesslog是访问日志,可以通过access_log_format设置访问日志格式
    accesslog = "log/access.log"
    
    errorlog = "log/debug.log"
    
    # True意味着开启后台运行,默认为False
    daemon = False
    
    # 请求超时时间
    timeout = 100
    
    # 启动的进程数
    # workers = multiprocessing.cpu_count()
    workers = 4
    
    # 进程的模式类型,默认为sync模式
    worker_class = "gevent"
    
    x_forwarded_for_header = 'X-FORWARDED-FOR'
    

    supervisor配置

    导出配置模板到项目根目录

    echo_supervisord_conf > supervisor.conf
    

    编辑supervisor.conf,并在尾部添加内容如下

    [program:risk]  # risk是自己给进程取的名,随意
    command=gunicorn -c gc.py manager:flask_app
    ;directory=/home/maxwell/Downloads/riskcontrol  # 项目目录
    startsecs=0 
    stopwaitsecs=0   
    autostart=true
    autorestart=true
    

    关闭系统防火墙

    查看防火墙状态
    firewall-cmd --state
    关闭防火墙
    systemctl stop firewalld.service
    

    启动服务

    方式一:通过gunicorn运行服务(缺点是需要通过查找pid杀掉进程)
    gunicorn -c gc.py manager:flask_app
    
    方式二:通过supervisor运行服务(推荐)
    > 加载配置文件并启动supervisor
    supervisord -c supervisor.conf
    > 修改完配置文件后重新启动supervisor
    supervisorctl -c supervisor.conf reload
    > 开启服务
    supervisorctl -c supervisor.conf start risk
    
    supervisor命令参考
    supervisorctl -c supervisor.conf reload :修改完配置文件后重新启动supervisor
    supervisorctl -c supervisor.conf status :查看supervisor监管的进程状态
    supervisorctl -c supervisor.conf start 进程名 :启动XXX进程 进程名就是你刚刚配置文件的进程名
    supervisorctl -c supervisor.conf stop 进程名 :停止XXX进程
    supervisorctl -c supervisor.conf stop all:停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
    supervisorctl -c supervisor.conf update:根据最新的配置文件,启动新配置
    

    参考资料

    Centos安装Python
    Supervisor命令详解
    Gunicorn部署Flask,并用Supervisor托管

    相关文章

      网友评论

          本文标题:[Centos7]Gunicorn部署Falsk,并用Super

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