配置 python(django)+nginx+uwsgi

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

先到官网下载所需软件

下载uwsgi

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下

sudo apt-get libxml2-dev python-dev

centOS

yum -i libxml2-devel python-devel

下面就可以安装uwsgi

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


2、配置

1)打开nginx配置文件

server {
    listen   80; 
    server_name example.com;

    access_log /var/log/nginx/example.com-access.log ;
    error_log /var/log/nginx/example.com-error.log ;

    location / {
            uwsgi_pass 127.0.0.1:3031;
            include uwsgi_params;
    }

}

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

import os,sys

if not os.path.dirname(__file__) in sys.path[:1]:
    sys.path.insert(0, os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

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

<uwsgi>
    <socket>127.0.0.1:3031</socket>
    <chdir>/home/hysia/website/blog</chdir>
    <pythonpath>..</pythonpath>
    <module>wsgi</module>
    <daemonize>/var/log/uwsgi/uwsgi.log</daemonize>
</uwsgi>

接下来运行

uwsgi -x /home/example/django.xml

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

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

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

#!/bin/bash

PORT=3031
PROCESSES=4
LOG=/var/log/uwsgi

PID=`pidof -o %PPID /usr/bin/uwsgi`

. /etc/rc.conf
. /etc/rc.d/functions

case "$1" in
  start)
    stat_busy "Starting uwsgi"
    if [ -n "$PID" ]; then
      stat_busy "uwsgi is already running"
      stat_die
    else
      uwsgi -s ":$PORT" -M -p $PROCESSES -d $LOG &> /dev/null
      add_daemon uwsgi
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping uwsgi"
    killall -QUIT uwsgi &> /dev/null
    rm_daemon uwsgi
    stat_done
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0




你可能感兴趣的:(配置 python(django)+nginx+uwsgi)