1. 程式人生 > 其它 >nginx+uwsgi+flask

nginx+uwsgi+flask

平時我們都是使用這種啟動方式
nohup python3 app.py >/dev/null 2>&1 &
但是用uwsgi會更好。
例子:先建立虛擬環境
#直接建立
virtualenv venv
#啟用進入虛擬環境
source venv/bin/activate
#如果想退出
deactivate
#-----------------
如何使用uwsgi啟動flask呢?
1.先安裝uwsgi
pip install uwsgi
2.寫一個flask檔案 app.py
3.寫啟動配置檔案config.ini
[uwsgi]
http-socket = :5000
chdir = . #應該是專案目錄
virtualenv = /home/venv
wsgi-file = app.py #啟動檔案
callable = app #檔案裡面的app變數
master = true
4.啟動命令
uwsgi --ini config.ini
5.測試是否成功
curl 127.0.0.1:5000

--------------------------
# 啟動uWSGI伺服器
uwsgi --ini uwsgi.ini
# 重啟uWSGI伺服器
sudo service uwsgi restart
# 檢視所有uWSGI程序
ps aux | grep uwsgi
# 停止所有uWSGI程序
sudo pkill -f uwsgi -9


對nginx的操作---------------------
啟動
/usr/local/nginx/sbin/nginx
#去修改配置資訊

--------------------------------------
nginx與uwsgi互動
1.更改uwsgi的配置檔案*.ini
[uwsgi]
...
#使用nginx連線時, 監控地址
socket=127.0.0.1:8080
#直接做web伺服器時, 所監控地址
#http=127.0.0.1:8080
2.修改nginx的配置檔案
server {
listen 80;
server_name localhost;

location / {
include uwsgi_params;
uwsgi_pass localhost:8080;
}
2.2,替換nginx配置檔案

/usr/local/nginx/conf/nginx.conf
worker_processes  1;
 events {
     worker_connections  1024;
 }
 http {
     include       mime.types;
     default_type  application/octet-stream;
     sendfile        on;
     keepalive_timeout  65;
 
     server {
         listen       80;
         server_name  localhost;
 
         location 
/ { include uwsgi_params; uwsgi_pass localhost:5000; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
View Code

 

3.重啟nginx和uwsgi