首页 > 代码库 > nginx+uwsgi部署django项目

nginx+uwsgi部署django项目

操作可参考http://www.linuxyw.com/353.html (nginx+uwsgi+django+python环境部署文档)

这里只说明几点我的配置


环境:

Nginx版本:1.7.9

Django版本: 1.6.8

Python版本:2.7.5

Uwsgi版本:2.0.9


安装MySQL-python:

最好源码安装,yum install -y MySQL-python可能因为路径问题import MySQLdb失败,下载地址:

http://sourceforge.net/projects/mysql-python/files/mysql-python/

tar zxvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
python setup.py install


django项目的路径和结构图:

(fabric-bolt)[root@gfs1 fabric_bolt]# tree -d -L 3

`-- work
    |-- myweb
    |   `-- static
    `-- work


创建django项目:

项目位置:/www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/(这路径下面已经有其他的django app)

创建项目:

cd  /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt
django-admin.py startproject work


配置Nginx:

在nginx配置文件中添加以下内容:

vim /usr/local/nginx/conf

        location / {
            root /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work;
            include   uwsgi_params;
            uwsgi_pass  192.168.1.121:9000;
            uwsgi_param UWSGI_CHDIR /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work;
            uwsgi_param UWSGI_SCRIPT django_wsgi;
            access_log /usr/local/nginx/logs/access.log;
        }
        location /static/ {
            alias /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work/myweb/static/;
            index index.html;
}

work是项目名,mynew是app名


配置uwsgi:

在nginx中添加一个uwsgi配置文件:

vim /usr/local/nginx/conf/uwsgi.xml

<uwsgi>
<socket>192.168.1.121:9000</socket>
<pidfile>/usr/local/nginx/conf/uwsgi.pid</pidfile>
<processes>2</processes>
<pythonpath>/www/fabric-bolt/lib/python2.7/site-packages</pythonpath> #注意,按照原文配置/tmp/uwsgi.log总是报错"ImportError: No module named django.core.handlers.wsgi",但是django路径在sys.path中存在,在python命令行中输入import django.core.handlers.wsgi也正常,这就显得很矛盾。怎么办?就是添加这句!添加后启动uwsgi的时候/tmp/uwsgi.log会提示"added /www/fabric-bolt/lib/python2.7/site-packages/ to pythonpath.",这才是正确的!
<module>django_wsgi</module>
<chdir>/www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work</chdir>
<daemonize>/tmp/uwsgi.log</daemonize>
</uwsgi>


创建django应用模块:

vim /www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work/django_wsgi.py

import os,sys

#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myweb.settings")
#path = "/www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work"
#if path not in sys.path:
 #   sys.path.insert(0, "/www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work")

os.environ["DJANGO_SETTINGS_MODULE"] = "work.settings"

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


访问测试:

出现以下界面,说明配置成功

技术分享


copy几张图片或html等文件到/www/fabric-bolt/lib/python2.7/site-packages/fabric_bolt/work/myweb/static/目录下,可以访问到:

技术分享

nginx+uwsgi部署django项目