http://www.runoob.com/django/django-nginx-uwsgi.html
http://www.cnblogs.com/my_life/articles/6479568.html
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
在django中的view里設置的全局變量只是針對當前請求而言,新來的一個請求其全局變量仍然是初始值,跟上一次請求做的修改沒有任何關系。 我的理解:本質上就是一個cgi程序,執行完就結束,前后沒有任何的關系。
即使在settings.py里的變量也不行,也達不到全局變量的效果,並且只能讀,不同請求對變量的修改不會被共享。
http://www.cnblogs.com/my_life/articles/7602395.html
一個web服務器面對的是外部世界。它能直接從文件系統提供文件 (HTML, 圖像, CSS等等)。然而,它無法 *直接*與Django應用通信;它需要借助一些工具的幫助,這些東西會運行運用,接收來自web客戶端(例如瀏覽器)的請求,然后返回響應。
一個Web服務器網關接口(Web Server Gateway Interface) - WSGI - 就是干這活的。 WSGI 是一種Python標准。
uWSGI是一種WSGI實現。在這個教程中,我們將設置uWSGI,讓它創建一個Unix socket,並且通過WSGI協議提供響應到web服務器。最后,我們完整的組件棧看起來將是這樣的:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
在你開始設置uWSGI之
===========================================================
在前面的章節中我們使用 python manage.py runserver 來運行服務器。這只適用測試環境中使用。
正式發布的服務,我們需要一個可以穩定而持續的服務器,比如apache, Nginx, lighttpd等,本文將以 Nginx 為例。
安裝基礎開發包
Centos 下安裝步驟如下:
yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
CentOS 自帶 Python 2.4.3,但我們可以再安裝Python2.7.5:
cd ~ wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 tar xvf Python-2.7.5.tar.bz2 cd Python-2.7.5 ./configure --prefix=/usr/local make && make altinstall
安裝django
http://www.runoob.com/django/django-install.html
/path/to/python/pip install django
[root@solar django]# python Python 2.7.3 (default, May 15 2014, 14:49:08) [GCC 4.8.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> django.VERSION (1, 6, 5, 'final', 0) >>>
我們可以看到輸出了Django的版本號,說明安裝成功。
新建一個django工程
django-admin.py startproject mysite
cd mysite
安裝 測試uwsgi
uwsgi:https://pypi.python.org/pypi/uWSGI
uwsgi 參數詳解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi
uwsgi --version # 查看 uwsgi 版本
測試 uwsgi 是否正常:
新建 test.py 文件,內容如下:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
然后在終端運行:
uwsgi --http :8001 --wsgi-file test.py
在瀏覽器內輸入:http://127.0.0.1:8001,查看是否有"Hello World"輸出,若沒有輸出,請檢查你的安裝過程。
使用uwsgi啟動一個http服務,監聽在8001端口,把收到的來自客戶端的http請求轉發給test.py進行處理。 工作方式如下:
the web client <-> uWSGI <-> Python
測試你的Django項目
現在,我們想讓uWSGI做同樣的事,但是返回一個Django站點(使用django python庫寫的東西)而不是 test.py 模塊。
如果你還沒有這樣做,那么先請確保你的 mysite 項目實際上正常工作:
python manage.py runserver 0.0.0.0:8000 //測試django站點是否正常
python manage.py runserver 這種方式只適用測試環境中使用。
如果正常,則使用uWSGI來運行它:
uwsgi --http :8000 --module mysite.wsgi
此時的工作方式是: the web client <-> uWSGI <-> Django
通常我們不會讓瀏覽器直接與uWSGI通信。那是web服務器的工作。
安裝nginx
為你的站點配置nginx
你會需要 uwsgi_params 文件,可用在uWSGI發行版本的 nginx 目錄下,或者從https://github.com/nginx/nginx/blob/master/conf/uwsgi_params 找到。
# mysite_nginx.conf # 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 } }
nginx和uWSGI以及test.py
讓nginx對 test.py 應用說句”hello world”吧。
uwsgi --socket :8001 --wsgi-file test.py
這幾乎與之前相同,除了這次有一個選項不同:
socket :8001: 使用uwsgi協議,端口為8001
同時,已經配置了nginx在那個端口與uWSGI通信,而對外使用8000端口。此時的工作方式:
the web client <-> the web server(監聽在8000端口) <-> the socket <-> uWSGI(監聽在8001端口) <-> Python
使用Unix socket而不是端口
目前,我們使用了一個TCP端口socket,因為它簡單些,但事實上,使用Unix socket會比端口更好 - 開銷更少。
編輯 mysite_nginx.conf, 修改它以匹配:
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)
然后重啟nginx.
再次運行uWSGI:
uwsgi --socket mysite.sock --wsgi-file test.py
這次, socket 選項告訴uWSGI使用哪個文件。
配置uWSGI以允許.ini文件
uwsgi 配置
uwsgi支持ini、xml等多種配置方式,本文以 ini 為例, 在/ect/目錄下新建uwsgi9090.ini,添加如下配置:
[uwsgi] socket = 127.0.0.1:9090 //#與前端nginx進行通信的端口 master = true //主進程 vhost = true //多站模式 no-site = true //多站模式時不設置入口模塊和文件 workers = 2 //子進程數 reload-mercy = 10 vacuum = true //退出、重啟時清理文件 max-requests = 1000 limit-as = 512 buffer-size = 30000 pidfile = /var/run/uwsgi9090.pid //pid文件,用於下面的腳本啟動、停止該進程 daemonize = /website/uwsgi9090.log //日志保存的地方
然后使用這個文件運行uswgi:
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
Nginx 配置
找到nginx的安裝目錄(如:/usr/local/nginx/),打開conf/nginx.conf文件,修改server配置:
server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; //必須和uwsgi中的設置一致 uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件,即wsgi.py相對於項目根目錄的位置,“.”相當於一層目錄 uwsgi_param UWSGI_CHDIR /demosite; //項目根目錄 index index.html index.htm; client_max_body_size 35m; } }
你可以閱讀 Nginx 安裝配置 了解更多內容。
設置完成后,在終端運行:
uwsgi --ini /etc/uwsgi9090.ini & /usr/local/nginx/sbin/nginx
在瀏覽器輸入:http://127.0.0.1,你就可以看到 django 的 "It work" 了。
數據流向:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
======
以上的Nginx和uwsgi的配置,經測試發現不行,可能是我的配置的問題
https://www.zhihu.com/question/27295854
鏈接:https://www.zhihu.com/question/27295854/answer/37184556
來源:知乎
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
- 創建一個django項目
然后你的目錄是這樣的django-admin.py startproject hellohello/ ├── hello │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py - 配置uwsgi.ini,不清楚的配置自行搜索
[uwsgi] chdir=/home/liaoziqian/hello //django創建的工程目錄 uid=nobody gid=nobody module=hello.wsgi:application //關鍵之處,讓uwsgi能和django的工程聯動起來; 否則會出現internal error; no python application found, check your startup logs for errors socket=/home/liaoziqian/hello/uwsgi.sock //關鍵之處,經測試發現tcp socket不行 master=true workers=5 pidfile=/home/liaoziqian/hello/uwsgi.pid vacuum=true thunder-lock=true //驚群效應避免 enable-threads=true harakiri=30 post-buffering=4096 daemonize=/home/liaoziqian/hello/uwsgi.log - uwsgi相關命令
啟動uwsgi:uwsgi --ini uwsgi.ini 停止uwsgi:uwsgi --stop uwsgi.pid 重新加載配置:uwsgi --reload uwsgi.pid - nginx配置
server { listen 8080; location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/home/liaoziqian/hello/uwsgi.sock; } } - 啟動uwsgi,啟動nginx,一個簡單的nginx + uwsgi + django示例就完成了
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html
添加並發和監控
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html
你想進行的第一個調整可能是增加並發性 (默認情況下,uWSGI啟動一個單一的進程和一個單一的線程)。
你可以用 --processes 選項添加更多的進程,或者使用 --threads 選項添加更多的線程 (或者可以同時添加)。
master=true //最好同時帶上該配置,子進程掛掉的話會重新被拉起,否則會出現[uwsgi] <defunct>僵屍子進程
web應用部署的一個常見問題是“卡住的請求”。你所有的線程/worker都卡住了 (請求阻塞) ,而你的應用無法接收更多的請求。要避免這個問題,你可以設置一個 harakiri 定時器。它是一個監控器 (由master進程管理),會摧毀那些卡住超過指定秒數的進程 (小心選擇 harakiri 值)。
harakiri = 30
重啟uwsgi
There are several ways to make uWSGI gracefully restart.
# using kill to send the signal
kill -HUP `cat /tmp/project-master.pid` # or the convenience option --reload uwsgi --reload /tmp/project-master.pid # or if uwsgi was started with touch-reload=/tmp/somefile touch /tmp/somefile
Or from your application, in Python:
uwsgi.reload()
Or in Ruby,
UWSGI.reload
停止uwsgi
kill -INT `cat /tmp/project-master.pid` # or for convenience... uwsgi --stop /tmp/project-master.pid
http://www.cnblogs.com/my_life/articles/7606981.html
WSGI 是分成 server 和 framework (即 application) 兩部分 ,嚴格說 WSGI 只是一個協議, 規范 server 和 framework 之間連接的接口。
WSGI framework 就是我們經常提到的 Django 這種框架。
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
驚群現象出現在這樣的情況:主進程綁定並監聽socket,然后調用fork,在各個子進程進行accept。無論任何時候,只要有一個連接嘗試連接,所有的子進程都將被喚醒,但只有一個會連接成功,其他的會得到一個EAGAIN的錯誤,浙江導致巨大的CPU資源浪費,如果在進程中使用線程,這個問題被再度放大。一個解決方法是串行化accept,在accept前防止一個鎖。
--log-maxsize <bytes>
[uwsgi] socket=/data/ffmpeg_monitor/ffmpeg_monitor.sock chdir=/data/ffmpeg_monitor module=ffmpeg_monitor.wsgi master=true processes=10 thunder-lock=true harakiri=30 chmod-socket=666 pidfile=/var/run/uwsgi_ffmpeg_monitor.pid daemonize=/data/ffmpeg_monitor/log/ffmpeg_monitor_uwsgi.log log-maxsize=20000
