nginx+django+uwsgi

最近来了兴致,想搞一下django开发,so, ?搭建一下环境

1、安装django,可能通过pip install 或者源码安装(因为环境是python2.6.6的环境,所以这里采用django 1.4.17的版本):

# tar zxvf Django-1.4.17# cd Django-1.4.17# python setup.py installrunning installrunning buildrunning build_pyrunning build_scriptsrunning install_librunning install_scriptschanging mode of /usr/bin/django-admin.py to 755running install_datarunning install_egg_infoRemoving /usr/lib/python2.6/site-packages/Django-1.4.17-py2.6.egg-infoWriting /usr/lib/python2.6/site-packages/Django-1.4.17-py2.6.egg-info

因为之前安装了一次所以输出比较少

# pythonPython 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import django>>> django.VERSION(1, 4, 17, 'final', 0)

2、安装uwsgi。uwsgi是一个快速的,纯C语言开发的,自维护、对开发者友好的WSGI服务器,旨在提供专业的python web应用发布和开发功能

# tar zxvf uwsgi-2.0.9.tar.gz # cd uwsgi-2.0.9socket/plugin.o -lpthread -lm -rdynamic -ldl -lz -L/usr/local/lib -lpcre -lssl -lcrypto -lxml2 -lz -lm -lpthread -ldl -lutil -lm -lpython2.6 -lcrypt################# uWSGI configuration #################pcre = Truekernel = Linuxmalloc = libcexecinfo = Falseifaddrs = Truessl = Truezlib = Truelocking = pthread_mutexplugin_dir = .timer = timerfdyaml = embeddedjson = Falsefilemonitor = inotifyrouting = Truedebug = Falsecapabilities = Falsexml = libxml2event = epoll############## end of uWSGI configuration #############total build time: 1 seconds*** uWSGI is ready, launch it with ./uwsgi ***# cp uwsgi /usr/bin# cd ..

如果在这个过程中遇到下面错误,可以执行yum remove pcre-devel 然后再执行:

c:undefined reference to `pcre_free_study'collect2: ld returned 1 exit status*** error linking uWSGI ***make: *** [all] Error 1

也可以使用pip进行安装:

# pip install uwsgiRequirement already satisfied (use --upgrade to upgrade): uwsgi in /usr/lib/python2.6/site-packagesCleaning up...

3、nginx配置

nginx的安装,我们在这里就不讲了,经常做的工作,相必大家都会,下面看一下vhost的配置:

# cat localhost.confserver{    listen 80;    server_name localhost;    location / {        uwsgi_pass 127.0.0.1:9090;        include uwsgi_params;        uwsgi_param UWSGI_CHDIR  /data/www/OMserverweb;        uwsgi_param UWSGI_SCRIPT django_wsgi;        access_log off;    }    location ^~ /static{        root /data/www/OMserverweb/OMserverweb;    }    location ~* ^.+.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {        root /data/www/OMserverweb/OMserverweb/static;        access_log off;    }}

4、配置uwsgi。创建uwsgi配置文件/data/nginx/config/uwsgi.ini,详细内容如下:

# cat uwsgi.ini [uwsgi]socket = 127.0.0.1:9090master = truepidfile = /var/run/uwsgi.pidprocesses = 8chdir = /data/www/OMserverwebpythonpath = ..profiler = truememory-report=trueenable-threads=truelogdate=truelimit-as=6048daemonize=/data/nginx/logs/django.log

进入到网站根目录/data/www:

# django-admin.py startproject OMserverweb# tree.├── django_wsgi.py├── django_wsgi.pyc├── manage.py└── OMserverweb    ├── __init__.py    ├── __init__.pyc    ├── settings.py    ├── settings.pyc    ├── urls.py    ├── urls.pyc    ├── wsgi.py    └── wsgi.pyc

创建django_wsgi:

# cat django_wsgi.py#!/usr/bin/env python# coding: utf-8import osimport sys# 将系统的编码设置为UTF8reload(sys)sys.setdefaultencoding('utf8')os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OMserverweb.settings")from django.core.handlers.wsgi import WSGIHandlerapplication = WSGIHandler()

5、启动:

# uwsgi --ini /data/nginx/conf/uwsgi.ini # /etc/init.d/nginx restartStopping nginx:                                            [  OK  ]Starting nginx:                                            [  OK  ]# ps aux | grep uwsgiroot     10159  0.0  0.3 156504  5920 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     10160  0.0  0.9 221416 18228 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     10161  0.0  0.9 221436 18208 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     10162  0.0  0.9 221552 18344 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     10163  0.0  0.9 221548 18364 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     10164  0.0  0.9 221552 18300 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     10165  0.0  0.7 215352 14252 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     10166  0.0  0.9 221448 18224 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     10167  0.0  0.7 215352 14248 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.iniroot     29104  0.0  0.0 103240   864 pts/0    S+   13:18   0:00 grep uwsgi

打开网页输入域名:

It worked!Congratulations on your first Django-powered page.Of course, you haven't actually done any work yet. Here's what to do next:If you plan to use a database, edit the DATABASES setting in OMserverweb/settings.py.Start your first app by running python manage.py startapp [appname].You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!

说明环境搭建成功

如果登陆admin出现样式丢失需要操作两步

1、修改settings.py中STATIC_ROOT为你的static静态文件的物理路径,比如说我静态文件存放在/data/www/OMserverweb/static/中,最后修改settings.py中STATIC_ROOT指向STATIC_ROOT = ‘/data/www/OMserverweb/static/’

2、运行python manage.py collectstatic命令,这将从Django资源包中复制必须的静态文件到STATIC_ROOT

指示的static文件夹中,这其中包括admin界面所必须的样式表(style)、图片(image)及脚本(js)等。

└── static    └── admin        ├── css        │?? ├── base.css        │?? ├── changelists.css        │?? ├── dashboard.css        │?? ├── forms.css        │?? ├── ie.css        │?? ├── login.css        │?? ├── rtl.css        │?? └── widgets.css        ├── img        │?? ├── changelist-bg.gif        │?? ├── changelist-bg_rtl.gif        │?? ├── chooser-bg.gif        │?? ├── chooser_stacked-bg.gif        │?? ├── default-bg.gif        │?? ├── default-bg-reverse.gif        │?? ├── deleted-overlay.gif        │?? ├── gis        │?? │?? ├── move_vertex_off.png        │?? │?? └── move_vertex_on.png        │?? ├── icon_addlink.gif        │?? ├── icon_alert.gif        │?? ├── icon_calendar.gif        │?? ├── icon_changelink.gif        │?? ├── icon_clock.gif        │?? ├── icon_deletelink.gif        │?? ├── icon_error.gif        │?? ├── icon-no.gif        │?? ├── icon_searchbox.png        │?? ├── icon_success.gif        │?? ├── icon-unknown.gif        │?? ├── icon-yes.gif        │?? ├── inline-delete-8bit.png        │?? ├── inline-delete.png        │?? ├── inline-restore-8bit.png        │?? ├── inline-restore.png        │?? ├── inline-splitter-bg.gif        │?? ├── nav-bg.gif        │?? ├── nav-bg-grabber.gif        │?? ├── nav-bg-reverse.gif        │?? ├── nav-bg-selected.gif        │?? ├── selector-icons.gif        │?? ├── selector-search.gif        │?? ├── sorting-icons.gif        │?? ├── tool-left.gif        │?? ├── tool-left_over.gif        │?? ├── tool-right.gif        │?? ├── tool-right_over.gif        │?? ├── tooltag-add.gif        │?? ├── tooltag-add_over.gif        │?? ├── tooltag-arrowright.gif        │?? └── tooltag-arrowright_over.gif        └── js            ├── actions.js            ├── actions.min.js            ├── admin            │?? ├── DateTimeShortcuts.js            │?? ├── ordering.js            │?? └── RelatedObjectLookups.js            ├── calendar.js            ├── collapse.js            ├── collapse.min.js            ├── compress.py            ├── core.js            ├── getElementsBySelector.js            ├── inlines.js            ├── inlines.min.js            ├── jquery.init.js            ├── jquery.js            ├── jquery.min.js            ├── LICENSE-JQUERY.txt            ├── prepopulate.js            ├── prepopulate.min.js            ├── SelectBox.js            ├── SelectFilter2.js            ├── timeparse.js            └── urlify.js

nginx+django+uwsgi,首发于运维者。

孤独是为了孤独背后的解脱,孤独的过程,就是一个寻找真爱的过程。

nginx+django+uwsgi

相关文章:

你感兴趣的文章:

标签云: