ubuntu安装nginx+uwsgi+django

1、安装python
2、安装django
https://www.djangoproject.com/download/

tar xzvf Django-1.4.3.tar.gz;
cd Django-1.4.3;
sudo python setup.py install;

3、创建djanggo项目

django-admin.py startproject mysite;
tree mysite;
输出
mysite/
├── manage.py
└── mysite
    ├── application.py
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

到此可以直接用manage.py来启动项目了

python manage.py runserver 0.0.0.0:9999;
用地址http://127.0.0.1:9999/访问

4、安装uwsgi

http://projects.unbit.it/uwsgi/wiki/WikiStart#Getit
wget http://projects.unbit.it/downloads/uwsgi-1.4.4.tar.gz
tar -xzvf uwsgi-1.4.4.tar.gz;
cd uwsgi-1.4.4;
python uwsgiconfig.py --build;
python setup.py install;

5、配置nginx

vi /etc/nginx/sites-enabled/python
server {
        listen 81;
        index index.html index.htm;
         server_name localhost;
         location / {
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:9090;
        }
}

重启nginx
6、配置mysite中的uwsgi入口并启动uwsgi

vi /home/wangliang/mysite/mysite/uwsgi_idnex.py
import os,sys
import django.core.handlers.wsgi
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
sys.path.append('/home/wangliang/mysite/') #不知道为什么django生成的项目会有两极目录,这个目录是第一级mysite
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
application = django.core.handlers.wsgi.WSGIHandler()

启动uwsgi

uwsgi -s 0.0.0.0:9090 \
--chdir /home/wangliang/mysite/mysite \
-t 30 -M -p5 -w uwsgi_idnex --limit-as 128 -R 10000 -d /var/log/uwsgi.log &

tail -f /var/log/uwsgi.log 看到如下信息表示加载成功了

*** Operational MODE: preforking ***
/home/wangliang/mysite/mysite
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xf60f20 pid: 4151 (default app)
*** uWSGI is running in multiple interpreter mode ***

访问地址http://127.0.0.1:81

7、出现的错误

ImportError: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
解决办法
vi /home/wangliang/mysite/mysite/uwsgi_index.py
增加
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
sys.path.append('/home/wangliang/mysite/') #不知道为什么django生成的项目会有两极目录,这个目录是第一级mysite
 
*** Operational MODE: preforking ***
ImportError: No module named uws_app
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
502异常
启动uwsgi时候要传--chdir到django项目的目录如 --chdir /home/wangliang/mysite/mysite 这个mysite是django生成的项目的第二级mysite。

8、创建helloworld应用

cd /home/wangliang/mysite;
python manage.py startapp hehe;
产生如下文件
├── hehe
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── views.py
├── manage.py
└── mysite
    ├── application.py
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    ├── uwsgi_idnex.py
    └── wsgi.py

vi hehe/views.py

from django.http import HttpResponse
from django.shortcuts import render_to_response
 
def index(request):
    return HttpResponse("<b>HW</b>")

编辑/home/wangliang/mysite/mysite/setting.py

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hehe',
)

编辑/home/wangliang/mysite/mysite/urls.py

from django.conf.urls import patterns, include, url
 
urlpatterns = patterns('',
    (r'^hehe/', 'hehe.views.index'),
)
测试地址
http://127.0.0.1:81/hehe
uwsgi -s :7777 --chdir /data/www -t 30 -M -p5 -w code --limit-as 128 -R 10000 -d /var/log/uwsgi.log &

参考资料

http://www.cnblogs.com/zhouej/archive/2012/03/25/2379646.html
https://docs.djangoproject.com/en/1.4/
http://www.vimer.cn/2011/07/linux%E4%B8%8Bnginxpythonuwsgi%E9%83%A8%E7%BD%B2%E6%80%BB%E7%BB%93djangoweb-py.html
http://www.nonease.com/nginx-uwsgi-webpy-python-deployment.html/
http://blog.liuts.com/post/216/
http://shell909090.com/blog/2011/05/%E4%BD%BF%E7%94%A8uwsgi%E6%90%AD%E5%BB%BApython%E5%BA%94%E7%94%A8/
http://messense.me/nginx-and-uwsgi-install-on-centos.html
http://davidx.me/2012/04/19/use-uwsgi-to-deploy-your-application/
http://blog.ops.cc/hosting/22935dcc7d57e320.html
http://blog.sina.com.cn/s/blog_ab101bfb01017fn1.html

原创文章,转载请注明: 转载自海波无痕

本文链接地址: ubuntu安装nginx+uwsgi+django

文章的脚注信息由WordPress的wp-posturl插件自动生成

此条目发表在server分类目录,贴了, , , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论链接可以 移除 nofollow.