Python3+flask+nginx+uwsgi

配置項目的時候通常使用虛擬環境,是各個項目的環境獨立起來,更多方便管理。至於如何使用搜索便可,並不難
一、安裝python3
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
xz -d Python-3.6.4.tar.xz
tar -xvf Python-3.6.4.tar
 
./configure --prefix=/usr/local/python3.6.4 --enable-shared CFLAGS=-fPIC
加上--enable-shared和-fPIC以後能夠將python3的動態連接庫編譯出來,默認狀況編譯完lib下面只有python3.xm.a這樣的文件,python自己能夠正常使用,可是若是編譯第三方庫須要python接口的好比caffe等,則會報錯;因此這裏建議按照上面的方式配置,另外若是openssl不使用系統yum安裝的,而是使用本身編譯的比較新的版本可使用--with-openssl=/usr/local/openssl這種方式指定,後面目錄爲openssl實際安裝的目錄,另外編譯完還要將openssl的lib目錄加入ld運行時目錄中便可. 
 
make;make install;
添加軟鏈接
ln -s /usr/local/python3.6.4/bin/python3 /usr/bin/python3
ln -s /usr/local/python3.6.4/bin/pip3 /usr/bin/pip3
 
防止運行Python是提示找不到庫
cp libpython3.6m.so.1.0 /usr/lib/ /usr/lib64/
 
Python3安裝後致使yum不能使用,由於執行yum須要python2的版本,修改兩個文件
vim /usr/bin/yum vim /usr/libexec/urlgrabber-ext-down 把#!/usr/bin/python 修改成 #!/usr/bin/python2
 
二、先安裝nginx(http://www.javashuo.com/article/p-bzmtxaxc-bv.html)。配置uwsgi
pip install uwsgi
ln -s /usr/local/python3.6.4/bin/uwsgi /usr/bin/uwsgi
 
vim config.ini
 
[uwsgi]
chdir=/home/Py
pythonpath= /home/Py
#項目啓動文件
wsgi-file = /home/Py/run.py
#爲你的項目入口文件
module=run
#實例化flask的變量 application = Flask(__name__)
callable=application
#當修改文件內容後重啓uwsgi
py-autoreload=1
#開啓一個master進程監控項目運行
master=true
#uwsgi的端口。要與項目運行的端口一致
socket=/home/Py/flask_socket.sock
socket=127.0.0.1:5000
processes=4
#threads=2
buffer-size=65536
#容許用內嵌的語言啓動線程。這將容許你在app程序中產生一個子線程。
enable-threads=true
pidfile=/tmp/uwsgi-master.pid
uid=nginx
gid=root
#當服務器退出的時候自動刪除unix socket文件和pid文件。
vacuum=true
#皇帝」模式運行。在這種模式下,它將件事uWSGI配置文件的路徑而且爲每個它找到的文件生成實例(‘諸侯’)。不管何時一個配置文件被修改了,皇帝都會自動重啓諸侯
emperor=true
logto=/var/log/nginx/uwsgi.log
 
後臺啓動
uwsgi --ini config.ini &
 
● 經常使用選項:
socket : 地址和端口號,例如:socket = 127.0.0.1:50000
processes : 開啓的進程數量
workers : 開啓的進程數量,等同於processes(官網的說法是spawn the specified number ofworkers / processes)
chdir : 指定運行目錄(chdir to specified directory before apps loading)
wsgi-file : 載入wsgi-file(load .wsgi file)
stats : 在指定的地址上,開啓狀態服務(enable the stats server on the specified address)
threads : 運行線程。因爲GIL的存在,我以爲這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 容許主進程存在(enable master process)
daemonize : 使進程在後臺運行,並將日誌打到指定的日誌文件或者udp服務器(daemonize uWSGI)。實際上最經常使用的,仍是把運行記錄輸出到一個本地文件上。
pidfile : 指定pid文件的位置,記錄主進程的pid號。
vacuum : 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)
disable-logging : 不記錄請求信息的日誌。只記錄錯誤以及uWSGI內部消息到日誌中。若是不開啓這項,那麼你的日誌中會大量出現這種記錄:
[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)
 
配置nginx
location /{
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
uwsgi_param UWSGI_PYHOME /usr/bin/python; #也可對應虛擬環境
uwsgi_param UWSGI_CHDIR /home/Py;
uwsgi_param UWSGI_SCRIPT run:application;
}
有定義路由前綴home時 http://192.168.164.128/home/index/
 
最後若是有多個項目的話 若是用域名到很簡單配置多個nginx server 和uwsgi就能夠
下面將配置使用ip訪問不一樣項目
假如我有兩個項目 分貝在 Py1 和 Py2
匹配該路徑下的項目 重定向到81端口 定義81端口的項目uwsgi配置 一個項目對應一個uwsgi配置
location /Py1/{
proxy_pass http://127.0.0.1:81/;
}
server{
listen 81;
server_name localhost;
location /{
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
uwsgi_param UWSGI_PYHOME /usr/bin/python;
uwsgi_param UWSGI_CHDIR /home/Py1;
uwsgi_param UWSGI_SCRIPT run:application;
}
}
 
location /Py2/{
proxy_pass http://127.0.0.1:82/;
}
server{
listen 82;
server_name localhost;
location /{
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
uwsgi_param UWSGI_PYHOME /usr/bin/python;
uwsgi_param UWSGI_CHDIR /home/Py2;
uwsgi_param UWSGI_SCRIPT run:application;
}
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息