美文网首页
centos7+python3.6+flask+virtuale

centos7+python3.6+flask+virtuale

作者: HJaycee | 来源:发表于2018-01-12 23:56 被阅读0次

    使用python3.6进行python项目的多站点部署,其中需要让uWSGI使用python3.6,而不是系统默认的python2.7,还要让uWSGI使用虚拟环境目录下的模块。

    python3.6

    # 系统默认的是python2.7,yum会使用2.7,所以安装3.6完不要去替换系统的
    # yum只有python3.4的源 所以不用yum来装
    wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
    tar -xzvf Python-3.6.0.tgz -C  /tmp
    cd  /tmp/Python-3.6.0/
    # 把Python3.6安装到 /usr/local 目录
    ./configure --prefix=/usr/local
    make
    make altinstall
    

    pip

    # pip是python的依赖包管理工具
    wget https://bootstrap.pypa.io/get-pip.py
    python get-pip.py
    

    virtualenv

    # 用来隔离环境的
    pip install virtualenv
    
    # 指定python3.6来创建环境目录
    virtualenv -p /usr/local/bin/python3.6 env3.6
    
    # 以后安装模块先进入env3.6环境再安装
    . env3.6/bin/activate
    pip install ...
    
    # 退出环境
    deactivate
    

    uwsgi

    # 这是python服务
    
    # 随便指定一个目录
    cd /data
    
    # 下载安装用这种方式,不要用pip
    wget wget http://projects.unbit.it/downloads/uwsgi-2.0.15.tar.gz
    tar -zxvf uwsgi-2.0.15.tar.gz
    cd uwsgi-2.0.15/
    python3.6 uwsgiconfig.py --build
    python3.6 setup.py install
    
    # 看下版本是不是python3.6
    uwsgi --python-version
    

    配置nginx

    # 不知道配置文件在哪就用这个命令找下
    find / -name nginx.conf 
    vi nginx.conf
    
    # 替换其中的 server {}
    server {
        listen 80;
        server_name _;
        access_log /data/wwwlogs/access_nginx.log combined;
        #error_page 404 /404.html;
        #error_page 502 /502.html;
        location /nginx_status {
          stub_status on;
          access_log off;
          allow 127.0.0.1;
          deny all;
        }
        location / {
          include uwsgi_params;
          uwsgi_pass  127.0.0.1:3031;      
        }
      }
    
      server {
        listen 81;
        server_name _;
        access_log /data/wwwlogs/access_nginx.log combined;
        #error_page 404 /404.html;
        #error_page 502 /502.html;
        location /nginx_status {
          stub_status on;
          access_log off;
          allow 127.0.0.1;
          deny all;
        }
        location / {
          include uwsgi_params;
          uwsgi_pass  127.0.0.1:3032;
        }
      }
    
    # 重启nginx
    service nginx restart
    

    配置uwsgi

    # 假设服务器目录在/data/wwwroot/pyweb/
    # 在pyweb/下创建site1和site2目录
    mkdir site1 site2
    
    # 把项目代码放进去
    # 用virtualenv创建env3.6虚拟环境
    
    # 在/data/wwwroot/,新建uwsgi目录
    mkdir uwsgi && cd uwsgi
    
    # 新建uwsgi的vhost配置文件
    mk dir ini && cd ini
    touch uwsgi3031.ini
    
    # uwsgi3031.ini内容如下
    [uwsgi]
    chdir = /data/wwwroot/pyweb/site1
    home = /data/wwwroot/pyweb/site1/env3.6
    wsgi-file = index.py
    callable = app
    socket = 127.0.0.1:3031
    master = true         //主进程
    vhost = true          //多站模式
    no-stie = true        //多站模式时不设置入口模块和文件
    workers = 2           //子进程数
    reload-mercy = 10
    vacuum = true         //退出、重启时清理文件
    max-requests = 1000
    limit-as = 512
    buffer-sizi = 30000
    daemonize = /data/wwwroot/uwsgi/log/uwsgi3031.log
    
    # 同理复制一份uwsgi3032.ini,修改其中的目录、端口、和日志配置
    
    # 新建3个脚本 start.sh stop.sh restart.sh
    
    # start.sh
    
    uwsgi ini/uwsgi3031.ini && uwsgi ini/uwsgi3032.ini
    
    # stop.sh
    
    echo "#############################"
    pids=`ps -ef | grep uwsgi | grep -v grep | awk '{print $2}'`
    echo "running uwsgi pids:"
    echo $pids
    
    for pid in $pids
    do
      kill -9 $pid
      echo "kill:$pid"
    done
    echo "done"
    echo "#############################"
    
    # restart.sh
    
    sh stop.sh && sh start.sh
    
    # 重启uwsgi服务
    sh restart.sh
    

    service方式启动uwsgi

    cd /etc/init.d
    touch uwsgi
    chmod -x uwsgi
    
    #!/bin/bash
    
    set -e
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DESC="uwsgi daemon"
    NAME=uwsgi
    DAEMON=/usr/local/bin/$NAME
    SCRIPTNAME=/etc/init.d/$NAME
    CONFIGPATH=/data/wwwroot/uwsgi
     
    test -x $DAEMON || exit 0
    
    case "$1" in
    restart)
        # 杀死uwsgi进程
        pids=`ps -ef | grep -E '(uwsgi).+(ini)' | grep -v grep | awk '{print $2}'`
        echo "running uwsgi pids:"
        echo $pids
    
        for pid in $pids
        do
          kill -9 $pid
          echo "kill:$pid"
        done
        echo "all killed"
        # 启动
        find $CONFIGPATH -name *.ini | xargs $DAEMON
        echo "all running"
    ;;
    *)
        echo "Usage: $SCRIPTNAME {restart}" >&2
        exit 3
    ;;
    esac
     
    exit 0
    

    相关文章

      网友评论

          本文标题:centos7+python3.6+flask+virtuale

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