標籤:django wsgi uwsgi gunicorn
前面章節我們都是通過python manage.py runserver運行伺服器,訪問django,但這隻適用於測試環境。當項目正式發布後,我們需要一個穩定可持續的伺服器,比如apache、nginx等等。接下來我們將用apache、nginx啟動伺服器。
wsgi:一種實現了python解析的通用介面標準/協議,實現了python web程式與伺服器互動。
uwsgi:他也是一種通訊協定,是uWSGI伺服器自有的協議;
uWSGI:一種python的web伺服器,實現了uwsgi、WSGI兩種協議,而uwsgi實現了WSGI、uwsgi、http等協議。apache、nginx都必須引用模組才能解析動態語言。只不過這兩個具備很好的靜態內容處理能力。
gunicorn/uwsgi:都是wsgi協議(python web server gateway interface)的實現,它們做的事情是協議轉換,協議的一頭是web app(如flask, django等framework的app),另一頭是web server(如apache, nginx等);
gunicore/uwsgi在預設的情況下都是同步模型,但都比通常的web framework實現得好。
gevent是利用協程(greenlet庫)實現的一個高效能非同步IO的庫,它做的事情就是實現非同步模型。
====================================================================
Apache (wsgi模組)
yum install mod_wsgi
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/
<VirtualHost *:80> WSGIScriptAlias / /opt/simplecmdb/simplecmdb/wsgi.py WSGIDaemonProcess simplecmdb python-path=/opt/simplecmdb:/usr/lib64/python2.7/site-packages WSGIProcessGroup simplecmdb Alias /static /usr/lib64/python2.7/site-packages/django/contrib/admin/static <Directory /opt/simplecmdb/simplecmdb> <Files wsgi.py> Require all granted </Files></Directory><Directory /usr/lib64/python2.7/site-packages/django/contrib/admin/static> Require all granted</Directory></VirtualHost>WSGISocketPrefix /var/run/wsgi
nginx (gunicorn模組)
pip install gunicorn
server { listen 192.168.1.106:8000; server_name node01; location /static/admin/ { root /usr/lib/python2.6/site-packages/django/contrib/admin/; index index.html index.htm; } location / { proxy_pass http://localhost:8000; }}
cd ~/項目下/ && gunicorn 項目名.wsgi:application -D
如果訪問不到,去掉-D後台模式,看是否執行成功;
另外查看見監聽連接埠,至少兩個一個是nginx配置啟動的8000,一個是127.0.0.1的8000。
------------------------------------------------------------------------------------------------
uwsgi模組 :效率要比wsgi好的多的多
--------------------------------------------------------------------------------------------------------------
nginx (uwsgi模組)
pip install uwsgi
建立test.py檔案,內容如下:
def application(env, start_response): start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)]) return "Hello World"
運行:uwsgi --http 10.0.18.33:8001 --wsgi-file test.py
訪問:10.0.18.33:8001 輸出 Hello World 即成功。
cat /etc/uwsgi9090.ini
[uwsgi]socket = 127.0.0.1:8001master = true //主進程vhost = true //多站模式no-site = true //多站模式時不設定入口模組和檔案workers = 2 //子進程數reload-mercy = 10 vacuum = true //退出、重啟時清理檔案max-requests = 1000 limit-as = 512buffer-size = 30000pidfile = /var/run/uwsgi9090.pid daemonize = /website/uwsgi9090.log
修改nginx設定檔:vim uwsgi_django.conf
server { listen 10.0.18.33:8001; server_name uwsgi.django.test; location / { uwsgi_pass 127.0.0.1:8001 #必須和uwsgi中的設定一致 uwsgi_param UWSGI_SCRIPT HelloWorld.wsgi; #入口檔案,即wsgi.py相對於項目根目錄的位置,“.”相當於一層目錄 uwsgi_param UWSGI_CHDIR /qqqqqqqqq/python/django/HelloWorld; #項目根目錄 index index.html index.htm; client_max_body_size 35m; }}
啟動:uwsgi --ini /etc/uwsgi9090.ini && /usr/local/nginx/sbin/nginx
測試:首先查看連接埠是不是都起來了,然後訪問http://10.0.18.33:8001/,出現django介面即可。
重啟:fuser -k 8001/tcp
-----------------------------------------------------------------------------------------------------
錯誤一:ImportError: No module named HelloWorld.settings
unable to load app 0 (mountpoint=‘‘) (callable not found or import error)
解決:export PYTHONPATH=$PYTHONPATH:/qqqqqqqqq/python/django/HelloWorld ,因為在sys.path中找不到項目路徑,當然加入到path的方法也很多,以上只是其中一種。
錯誤二:ImportError: No module named django.core.wsgi
unable to load app 0 (mountpoint=‘10.0.18.33:8001|‘)
解決:vim /etc/uwsgi9090.ini 添加pythonpath = /usr/lib/python2.7/site-packages 即可
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />-----------------------------------------------------------------------------------------------------
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/8C/4B/wKioL1hp9ezQF68-AABAw4Ag59c170.png" title="clipboard.png" style="width:750px;height:138px;" alt="wKioL1hp9ezQF68-AABAw4Ag59c170.png" width="750" vspace="0" hspace="0" height="138" border="0" />
Django -- 整合apache、nginx