内容正在载入中,请稍后……
公告
About me
搜索
统计
其他
7 Nov.2012

CentOS上使用nginx+uwsgi部署django 不指定

作者: 我就是个世界   分类:技术&学习 » Django   出处:本站原创            | |
1.先下载安装 setuptools-0.6c11
    

wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086
sh setuptools-0.6c11-py2.6.egg
    

2.安装virtualenv及pip
    

    easy_install virtualenv
    easy_install pip
    


3.在指定的位置创建ENV虚拟环境
    

    cd home                 进入指定目录
    virtualenv myproject     创建env虚拟环境
    cd myproject             进入虚拟环境目录
    source ./bin/activate   激活env虚拟环境
    pip install django      安装django最新版本1.3
    django-admin.py startproject mysite     创建django项目mysite
    cd mysite
    django-admin.py startapp myapp              创建django应用APP myapp
    


4.安装uwsgi

    

    yum install python-devel
    yum install libxml2(需要的组件)

                #wget http://projects.unbit.it/downloads/uwsgi-0.9.6.2.tar.gz
                #tar -zxvf uwsgi-0.9.6.2.tar.gz
                #cd uwsgi-0.9.6.2
                #make
                #cp uwsgi /usr/bin
    


    因为我用虚拟环境,所以进入虚拟环境使用pip安装
    

    cd /home/myproject
    source ./bin/activate
    pip install uwsgi       安装uwsgi
    


5.创建Nginx相关配置 django配置文件django_uwsgi.conf

    

    cd /usr/local/nginx/conf
    vi django_uwsgi.conf
-----------------------------------
# Django project
server {
        listen  80;
        server_name v.husw.net;

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

        location ^~ /static {
            root   /home/myproject/mysite;
        }

        location ^~ /admin/ {
            uwsgi_pass   127.0.0.1:3031;
            include  uwsgi_params;
            access_log   off;
        }

        location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {
            root    /home/myproject/mysite/static;
            access_log  off;
        }

}
-----------------------------------

    *Nginx-0.8.50默认已添加uwsgi输出头支持
    include uwsgi_params;

    vi nginx.conf
    追加子配置文件django_uwsgi.conf
-----------------------------------
http{
........
include django_uwsgi.conf;
}
-----------------------------------
    


6、UWSGI配置

    

    mkdir -p /home/myproject/uwsgi;cd /home/myproject/uwsgi
    mkdir -p /home/myproject/logs
    echo "" > /home/myproject/logs/django.log
    vi uwsgi.xml
-----------------------------------
<uwsgi>
  <socket>127.0.0.1:3031</socket>
  <listen>200</listen>
  <master>true</master>
  <pidfile>/usr/local/nginx/uwsgi.pid</pidfile>
  <processes>8</processes>
  <pythonpath>/home/myproject/mysite</pythonpath>
  <pythonpath>/home/myproject/</pythonpath>
  <module>django_wsgi</module>
  <profiler>true</profiler>
  <memory-report>true</memory-report>
  <enable-threads>true</enable-threads>
  <logdate>true</logdate>
  <limit-as>256</limit-as>
  <daemonize>/home/myproject/logs/django.log</daemonize>
</uwsgi>
-----------------------------------
    

参数说明  http://projects.unbit.it/uwsgi/wiki/Doc
/home/myproject/mysite/  是我的django项目目录
limit-as 是内存占用
listen     是等待客户端连接时间  超过这个时间没反应直接断掉
127.0.0.1:3031  是 uwsgi 使用的端口
django_wsgi   这个是要在django项目根目录建立一个同名的 .py 文件  即 django_wsgi.py内容看下一步

7、创建应用模块运行文件django_wsgi.py

    

    cd /home/myproject/mysite
    vi django_wsgi.py
-----------------------------------
from django.core.handlers.wsgi import WSGIHandler
import os, sys

import site
site.addsitedir('/home/myproject/lib/python2.6/site-packages/')
activate_this = '/home/myproject/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

ROOT = '/home/myproject/'
sys.path.append(ROOT)
sys.path.append('/home/myproject/lib/python2.6/site-packages/')
sys.stdout = sys.stderr
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
application = WSGIHandler()
-----------------------------------
    


8、启动与关闭服务
    

    uwsgi -x /home/myproject/uwsgi/uwsgi.xml
    /usr/local/nginx/sbin/nginx

    killall -9 uwsgi    #关闭uwsgi服务
    /usr/local/nginx/sbin/nginx -s  reload  #平滑重启
    


9、查看进程
    
ps -ef|grep uwsgi|grep -v grep


10、监听端口
    
netstat -an|grep 3031



11、访问测试
    输入http://v.husw.net/


*附uwsgi启动脚本

#cd /home/uwsgi
#vi uwsgiserver.sh

-----------------------------------
#!/bin/bash
if [ $1 = start ];then
  psid=`ps aux|grep "uwsgi"|grep -v "grep"|wc -l`
  if [ $psid -gt 2 ];then
    echo "uwsgi is running!"
    exit 0
  else
    uwsgi -x /home/myproject/uwsgi/uwsgi.xml
  fi
  echo "Start uwsgi service [OK]"
elif [ $1 = stop ];then
  killall -9 uwsgi
  echo "Stop uwsgi service [OK]"
elif [ $1 = restart ];then
  killall -9 uwsgi
  uwsgi -x /home/myproject/uwsgi/uwsgi.xml
  echo "Restart uwsgi service [OK]"
else
  echo "Usages: sh uwsgiserver.sh [start|stop|restart]"
fi
-----------------------------------

#sh uwsgiserver.sh start
[uWSGI] parsing config file /home/uwsgi/uwsgi.xml
Start uwsgi service [OK]

12. 安装MySQL-python连接库、PIL 及第三方扩展 django-tagging django-uni-form django-cms


    yum install MySQL-python
    pip install pil django-tagging django-uni-form django-cms


参考网站:
http://blog.liuts.com/post/216
http://askbot.org/wiki/index.php/Askbot_on_VirtualHost

=================================================
Centos 5.5默认安装的Python版本是2.4。要升级到2.6或者其他版本需要下载源码包自己编译安装

# 下载 2.7.1 版
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz

# 安装
tar zxf Python-2.7.1.tgz
./configure
make && make install

# 查看安装的版本信息
/usr/local/bin/python2.7 -V

# 查看新装的版本,生效了然后做个软连接应该就OK
mv /usr/bin/python /usr/bin/python-bak
ln -s /usr/local/bin/python2.7 /usr/bin/python

# 升级安装好以后yum会无法使用,因为yum使用的是2.4版本的,
# 所以要修改 yum 的配置文件以便能够识别2.4版本下的 python
vim /usr/bin/yum

# 将文件头部的
#!/usr/bin/python

# 改成
#!/usr/bin/python2.4

就可以继续使用 yum 了
------------------------------------


wget http://www.python.org/ftp/python/2.6/Python-2.6.tgz

tar zxf Python-2.6.tgz
./configure
make && make install

/usr/local/bin/python2.6 -V

mv /usr/bin/python /usr/bin/python-bak
ln -s /usr/local/bin/python2.6 /usr/bin/python



vim /usr/bin/yum
# 将文件头部的
#!/usr/bin/python

# 改成
#!/usr/bin/python2.4

就可以继续使用 yum 了
=================================================

PYTHON_HOME=/usr/local/lib/python2.6/
export PYTHON_HOME
PATH=$PYTHON_HOME/bin:$PATH
export PATH

    安装egg包报错:setuptools-0.6c11-py2.6.egg: line 3: exec: python2.6: not found

    如果你的环境中python版本不是2.6,那么需要先升级python版本到2.6,升级步骤看最后!如果上面的方法无法安装egg包,那么尝试下面的方法!
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e
tar zxf setuptools-0.6c11.tar.gz
cd  setuptools-0.6c11
python setup.py install

wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.6.4.tar.gz#md5=1072b66d53c24e019a8f1304ac9d9fc5
tar zxf virtualenv-1.6.4.tar.gz
cd virtualenv-1.6.4
python setup.py install

Processing virtualenv-1.6.4-py2.6.egg
creating /usr/local/lib/python2.6/site-packages/virtualenv-1.6.4-py2.6.egg
Extracting virtualenv-1.6.4-py2.6.egg to /usr/local/lib/python2.6/site-packages
Adding virtualenv 1.6.4 to easy-install.pth file
Installing virtualenv script to /usr/local/bin

Installed /usr/local/lib/python2.6/site-packages/virtualenv-1.6.4-py2.6.egg
Processing dependencies for virtualenv==1.6.4
Finished processing dependencies for virtualenv==1.6.4



PATH="$PATH":/usr/local/lib/python2.6/site-packages/

echo $PATH
/usr/local/lib/python2.6//bin:/usr/local/LuNamp/cmd/:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/local/lib/python2.6/site-packages/

分享到QQ空间
最后编辑: 我就是个世界 编辑于November 8, 2012 22:47
发表评论
  昵称 [注册]
  密码 (游客无需密码)
  网址
  电邮
OpenID登入 权限选项 表情