django 高效能部署 nginx uwsgi
阿新 • • 發佈:2019-01-05
- 由於專案需求,需要接入高併發和域名,根據市場調研,最終決定使用uwsgi+nginx
- uwsgi官網介紹:
uWSGI 是一個(巨大的) C 應用,所以你需要一個 C 編譯器(比如 gcc 或者 clang)和 Python 開發版標頭檔案。 - 首先安裝uwsgi
pip install uwsgi
- 測試uwsgi功能是否正常
# 建立test.py 檔案
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
#執行
uwsgi --http :8000 --wsgi-file test.py
#瀏覽器展示
hello world
- 配置uwsgi檔案,看字義就知道,不過多解釋
# uwsgi.ini
[uwsgi]
socket = 127.0.0.1:3031
static-map=/static=/usr/local/py_work/RS/pools/static
vacuum=true
chdir = /usr/local/py_work/RS
wsgi-file = /usr/local /py_work/RS/RS/wsgi.py
processes = 4
threads = 2
;stats = 127.0.0.1:9191
daemonize=/usr/local/uwsgi/RS.log
- 啟動帶django專案的uwsgi
uwgsi uwsgi.ini
# 出現以下log變為成功
[uWSGI] getting INI configuration from RS/uwsgi.ini
[uwsgi-static] added mapping for /static => /usr/local/py_work/RS/pools/static
- 安裝nginx,mac系統直接brew就好了,沒別的,中間有個小坑,這種自己裝的和公司裝的目錄結構有點小不一樣,迷惑了一會,公司裝的nginx conf檔案是在v.host 資料夾下面,自己安裝的是在servers資料夾下
- 配置nginx with uwsgi,官網說明是這樣的,客戶端到服務端的通訊
the web client <-> the web server
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
- 訪問本地8080端口出現頁面即為成功