配置 python(django)+nginx+uwsgi

配置 python(django)+nginx+uwsgi

分类: python 513人阅读 评论(0) 收藏 举报

uWSGI is a fast, self-healing and developer/sysadmin-friendly application container server coded in pure C。

先到官网下载所需软件

下载uwsgi

 

  1. wget  http://projects.unbit.it/downloads/uwsgi-0.9.9.3.tar.gz  

下载nginx(>0.8)

http://wiki.nginx.org/Install

下载django

https://www.djangoproject.com/download/


1、安装uwsgi

系统必须先安装libxml2-dev python-dev

ubuntu下

  1. sudo apt-get libxml2-dev python-dev  


centOS

  1. yum -i libxml2-devel python-devel  

 

下面就可以安装uwsgi

  1. tar zxvf uwsgi-0.9.9.2.tar.gz  
  2. cd uwsgi-0.9.9.2  
  3. make -f Makefile.Py26 #和你安装PYTHON版本一致  
  4. cp uwsgi /usr/sbin/uwsgi  


2、配置

1)打开nginx配置文件

  1. server {  
  2.     listen   80;   
  3.     server_name example.com;  
  4.   
  5.     access_log /var/log/nginx/example.com-access.log ;  
  6.     error_log /var/log/nginx/example.com-error.log ;  
  7.   
  8.     location / {  
  9.             uwsgi_pass 127.0.0.1:3031;  
  10.             include uwsgi_params;  
  11.     }  
  12.   
  13. }  


2)在django项目主目录下创建 django_wsgi.py

  1. import os,sys  
  2.   
  3. if not os.path.dirname(__file__) in sys.path[:1]:  
  4.     sys.path.insert(0, os.path.dirname(__file__))  
  5. os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'  
  6.   
  7. from django.core.handlers.wsgi import WSGIHandler  
  8. application = WSGIHandler()  


3)在django项目主目录下创建 django.xml

  1. <uwsgi>  
  2.     <socket>127.0.0.1:3031</socket>  
  3.     <chdir>/home/hysia/website/blog</chdir>  
  4.     <pythonpath>..</pythonpath>  
  5.     <module>wsgi</module>  
  6.     <daemonize>/var/log/uwsgi/uwsgi.log</daemonize>  
  7. </uwsgi>  


接下来运行

  1. uwsgi -x /home/example/django.xml  

OK,输入127.0.0.1就能看到 django worked!

不过每次重启服务器都需要输上面那行代码比较麻烦,可以将代码复制到 /etc/init.d/rc.local ,这样开机就自动启动

从网上找了段代码也可以试试,或者将下面代码copy到 /etc/init.d/uwsgi

    1. #!/bin/bash  
    2.   
    3. PORT=3031  
    4. PROCESSES=4  
    5. LOG=/var/log/uwsgi  
    6.   
    7. PID=`pidof -o %PPID /usr/bin/uwsgi`  
    8.   
    9. . /etc/rc.conf  
    10. . /etc/rc.d/functions  
    11.   
    12. case "$1" in  
    13.   start)  
    14.     stat_busy "Starting uwsgi"  
    15.     if [ -n "$PID" ]; then  
    16.       stat_busy "uwsgi is already running"  
    17.       stat_die  
    18.     else  
    19.       uwsgi -s ":$PORT" -M -p $PROCESSES -d $LOG &> /dev/null  
    20.       add_daemon uwsgi  
    21.       stat_done  
    22.     fi  
    23.     ;;  
    24.   stop)  
    25.     stat_busy "Stopping uwsgi"  
    26.     killall -QUIT uwsgi &> /dev/null  
    27.     rm_daemon uwsgi  
    28.     stat_done  
    29.     ;;  
    30.   restart)  
    31.     $0 stop  
    32.     sleep 1  
    33.     $0 start  
    34.     ;;  
    35.   *)  
    36.     echo "usage: $0 {start|stop|restart}"    
    37. esac  
    38. exit 0  
posted @ 2013-06-06 11:12  天涯海角路  阅读(216)  评论(0)    收藏  举报