原文:http://www.huangdc.com/103 python
在瞭解 uWSGI 以前,咱們不妨先了解一下 nginx
python http服務器? web
要使 Python 寫的程序能在 Web 上被訪問,還須要搭建一個支持 Python 的 HTTP 服務器,列舉幾個如 Gunicorn 、uWSGI 、FAPWS三、Aspen、Mod_WSGI等等 shell
WSGI是什麼? django
WSGI,全稱 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是爲 Python 語言定義的 Web 服務器和 Web 應用程序或框架之間的一種簡單而通用的接口。自從 WSGI 被開發出來之後,許多其它語言中也出現了相似接口。 bootstrap
WSGI 的官方定義是,the Python Web Server Gateway Interface。從名字就能夠看出來,這東西是一個Gateway,也就是網關。網關的做用就是在協議之間進行轉換。 vim
WSGI 是做爲 Web 服務器與 Web 應用程序或應用框架之間的一種低級別的接口,以提高可移植 Web 應用開發的共同點。WSGI 是基於現存的 CGI 標準而設計的。 bash
不少框架都自帶了 WSGI server ,好比 Flask,webpy,Django、CherryPy等等。固然性能都很差,自帶的 web server 更多的是測試用途,發佈時則使用生產環境的 WSGI server或者是聯合 nginx 作 uwsgi 。 服務器
好了,接下來看看 app
什麼是uWSGI ?
uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的做用是與uWSGI服務器進行交換。
要注意 WSGI / uwsgi / uWSGI 這三個概念的區分。
a、WSGI看過前面小節的同窗很清楚了,是一種通訊協議。
b、uwsgi同WSGI同樣是一種通訊協議。
c、而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。
爲何有了uWSGI爲何還須要nginx?
由於nginx具有優秀的靜態內容處理能力,而後將動態內容轉發給uWSGI服務器,這樣能夠達到很好的客戶端響應
部署配置
一、python + django + bootstrap (略)
可查看:http://my.oschina.net/u/588586/blog/345675
二、下載並安裝 uWSGI
[root@localhost tools]# wget --no-check-certificate [root@localhost tools]# tar zxf uwsgi-2.0.8.tar.gz [root@localhost tools]# cd uwsgi-2.0.8 [root@localhost uwsgi-2.0.8]# make [root@localhost uwsgi-2.0.8]# cp uwsgi /usr/bin/
三、nginx 配置
這裏的項目路徑是 /data/myproject/
## vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name 192.168.16.128; location / { root /data/myproject/; include uwsgi_params; uwsgi_pass 127.0.0.1:9000; uwsgi_param UWSGI_CHDIR /data/myproject; uwsgi_param UWSGI_SCRIPT django_wsgi; access_log /usr/local/nginx/logs/access.log; } location /static { expires 30d; autoindex on; add_header Cache-Control provate; alias /data/myproject/static; } } ## reload nginx [root@localhost sbin]# service nignx reload
在nginx目錄中添加一個uwsgi配置文件:
### vim /usr/local/nginx/conf/uwsgi.xml <uwsgi> <socket>127.0.0.1:9000</socket> <listen>200 </listen> <master>true </master> <pidfile>/usr/local/nginx/uwsgi.pid </pidfile> <processes>8 </processes> <pythonpath>/data/myproject/ </pythonpath> <pythonpath>/data </pythonpath> <module>django_wsgi</module> <profiler>true </profiler> <memory-report>true </memory-report> <enable-threads>true </enable-threads> <logdate>true </logdate> <limit-as>6048 </limit-as> <daemonize>/dev/null</daemonize> </uwsgi>
在項目目錄下增長django_wsgi.py 目錄
##vim /data/myproject/django_wsgi.py import os os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
四、增長一個 uwsgi 啓動文件
## vim /etc/init.d/uwsgi
## 記得加執行權限 chmod +x /etc/init.d/uwsgi
#!/bin/bash # uwsgi script # it is v.0.0.1 version. # chkconfig: - 89 19 # description: uwsgi # processname: uwsgi PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH uwsgi_config=/usr/local/nginx/conf/uwsgi.xml uwsgi_pn=`ps aux|grep -v "grep"|grep -c "uwsgi"` uwsgi_pid=`ps -eo pid,comm|grep uwsgi|sed -n 1p|awk '{print $1}'` uwsgi_PID=/usr/local/nginx/logs/uwsgi.pid uwsgi=/usr/bin/uwsgi RETVAL=0 prog="uwsgi" # Source function library. . /etc/rc.d/init.d/functions if [ $(id -u) != "0" ]; then printf "Error: You must be root to run this script!\n" exit 1 fi # Start nginx daemons functions. start() { if [ $uwsgi_pn -gt 5 ];then action "uwsgi is running!" /bin/true exit 0 fi daemon $uwsgi -x ${uwsgi_config} action "uwsgi start ..." /bin/true } # Stop nginx daemons functions. stop() { if [ $uwsgi_pn -gt 5 ] then #kill -9 `ps -eo pid,comm|grep uwsgi|sed -n 1p|awk '{print $1}'` ps -eo pid,comm|grep uwsgi|awk '{print $1}' |xargs kill -9 RETVAL=$? action "uwsgi stopping ..." /bin/true else action "uwsgi not running!" /bin/false fi } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; *) echo $"Usage: $prog {start|stop|restart}" exit 1 esac exit $RETVAL
啓動
[root@localhost init.d]# service uwsgi start [uWSGI] parsing config file /usr/local/nginx/conf/uwsgi.xml uwsgi start ... [ OK ] [root@localhost conf]# netstat -ntlp |grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 9426/uwsgi