centos7+python3.6+flask+virtualenv+uwsgi+nginx(多站點部署)

使用python3.6進行python項目的多站點部署,其中需要讓uWSGI使用python3.6,而不是系統(tǒng)默認的python2.7,還要讓uWSGI使用虛擬環(huán)境目錄下的模塊。

python3.6

# 系統(tǒng)默認的是python2.7,yum會使用2.7,所以安裝3.6完不要去替換系統(tǒng)的
# 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

# 用來隔離環(huán)境的
pip install virtualenv

# 指定python3.6來創(chuàng)建環(huán)境目錄
virtualenv -p /usr/local/bin/python3.6 env3.6

# 以后安裝模塊先進入env3.6環(huán)境再安裝
. env3.6/bin/activate
pip install ...

# 退出環(huán)境
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/下創(chuàng)建site1和site2目錄
mkdir site1 site2

# 把項目代碼放進去
# 用virtualenv創(chuàng)建env3.6虛擬環(huán)境

# 在/data/wwwroot/,新建uwsgi目錄
mkdir uwsgi && cd uwsgi

# 新建uwsgi的vhost配置文件
mk dir ini && cd ini
touch uwsgi3031.ini

# uwsgi3031.ini內(nèi)容如下
[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           //子進程數(shù)
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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容