ubuntu云主机上Flask+uwsgi+Nginx配置
環境:
ubuntu:18.10
全程使用root權限!!!
云主機IP
所有文件請見文末附錄
?
第一步,編譯安裝nginx以及必要的系統組件
①apt-get install zlib1g-dev
apt-get install libssl-dev
apt-get install libpcre3 libpcre3-dev
apt-get install -y uwsgi-plugin-python3(這個應該可以不需要)
需要自己編譯nginx安裝,
wget http://nginx.org/download/nginx-1.12.1.tar.gz && tar -zxf nginx-1.12.1.tar.gz && mv nginx-1.12.1 nginx && cd nginx && ./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/nginx.pid \
--lock-path=/usr/local/nginx/nginx.lock \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/usr/local/nginx/client \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--with-http_stub_status_module \
--with-http_ssl_module \
--http-scgi-temp-path=/usr/local/nginx/scgi
objs文件夾下面的的MakeFile刪除-Werror
make && make install?
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
然后需要重新開個終端或者重新遠程連接云主機,才會讓nginx -t的時候改變查找路徑(因為nginx.conf的默認路徑是/etc/nginx/nginx.conf)
②pip3 install uwsgi
如果調試的過程中不小心刪除了nginx的error.log,可以使用nginx -s reload讓error.log再次生成
#-------------------------------------------------------------------------------
?
第二步,啟動uwsgin、工程和nginx
當前目錄下啟動uwsgi
uwsgi -M --ini uwsgi.ini
啟動web應用
python3 main.py
如果報錯信息提示找不到sock文件,可以手動生成socket緩存文件
touch usr/local/nginx/conf/uwsgi.sock
啟動nginx
nginx -c /usr/local/nginx/conf/nginx.conf
nginx -s reload
?
第三步,測試
chromium瀏覽器打開:
http://IP:10073/uwsgi_learn/
效果如下:
嘗試打開多個網頁,然后輸入:
root@ubuntuguest:/usr/local/nginx/conf# lsof -i:10073
COMMAND ? PID USER ? FD ? TYPE DEVICE SIZE/OFF NODE NAME
nginx ? 31896 root ? ?3u ?IPv4 421798 ? ? ?0t0 ?TCP ubuntuguest.lan:10073->115.213.79.161:5766 (ESTABLISHED)
nginx ? 31896 root ? ?5u ?IPv4 421745 ? ? ?0t0 ?TCP ubuntuguest.lan:10073->115.213.79.161:5721 (ESTABLISHED)
nginx ? 31896 root ? ?6u ?IPv4 339467 ? ? ?0t0 ?TCP *:10073 (LISTEN)
root@ubuntuguest:/usr/local/nginx/conf# lsof -i:10071
COMMAND ? PID USER ? FD ? TYPE DEVICE SIZE/OFF NODE NAME
python3 32756 root ? ?3u ?IPv4 404126 ? ? ?0t0 ?TCP localhost:10071 (LISTEN)
另外根據[1]:
Nginx 總并發連接數 = worker 數量 * worker_connections
注意PID數量可以超過配置文件中的process數量,process數量表示的是有多少個邏輯CPU核會被安排這個web app的任務,
當然如果只有一個人開一個網頁去訪問,其他的邏輯CPU也是處于圍觀狀態.
#---------------------------------------------------------
如果碰到502報錯,注意查看/usr/local/nginx/error.log,
如果碰到internal server error,查看工程文件夾(和main.py同一個路徑)下面的run.log(由uwsgi啟動的時候自動生成),
根據報錯信息來排錯.
?
?
#------------------ 實驗附錄-------------------------------
main.py
from flask import Flask import os app = Flask(__name__) @app.route("/uwsgi_learn/") def hello():return '''<h1 style="color:blue">Hello World!</h1>''' def killport(port):command='''kill -9 $(netstat -nlp | grep :'''+str(port)+''' | awk '{print $7}' | awk -F"/" '{ print $1 }')'''os.system(command) if __name__ == "__main__":port=10071killport(port)app.run(host='127.0.0.1',port=port)uwsgi完整文件:
[uwsgi] wsgi-file = ./main.py callable=app master = true processes = 4 chdir =./ socket = /usr/local/nginx/conf/uwsgi.sock #socket = 127.0.0.1:10072 logto = ./run.log chmod-socket = 660 vacuum = true limit-as=256?
nginx.conf
user root; worker_processes 2; error_log /usr/local/nginx/error.log; pid /usr/local/nginx/nginx.pid;events {worker_connections 1024; }http {include /usr/local/nginx/conf/mime.types;default_type application/octet-stream;server {listen 10073;server_name localhost;location / {include uwsgi_params;uwsgi_pass unix:/usr/local/nginx/conf/uwsgi.sock;proxy_http_version 1.1; proxy_set_header Connection "";uwsgi_read_timeout 1800;uwsgi_send_timeout 300;proxy_read_timeout 300;}} }Reference:
[1]https://blog.csdn.net/pencilseo/article/details/82021218
總結
以上是生活随笔為你收集整理的ubuntu云主机上Flask+uwsgi+Nginx配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Package 'xxxx' is no
- 下一篇: 《关于gevent的几点思考》阅读笔记