nginx+django+uwsgi

最近來了興致,想搞一下django開發,so,  搭建一下環境html

一、安裝django,可能經過pip install 或者源碼安裝(由於環境是python2.6.6的環境,因此這裏採用django 1.4.17的版本):python

 
# tar zxvf Django-1.4.17
# cd Django-1.4.17
# python setup.py install
running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
changing mode of /usr/bin/django-admin.py to 755
running install_data
running install_egg_info
Removing /usr/lib/python2.6/site-packages/Django-1.4.17-py2.6.egg-info
Writing /usr/lib/python2.6/site-packages/Django-1.4.17-py2.6.egg-info

由於以前安裝了一次因此輸出比較少linux

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

二、安裝uwsgi。uwsgi是一個快速的,純C語言開發的,自維護、對開發者友好的WSGI服務器,旨在提供專業的python web應用發佈和開發功能nginx

 
# tar zxvf uwsgi-2.0.9.tar.gz 
# cd uwsgi-2.0.9
socket/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 = True
kernel = Linux
malloc = libc
execinfo = False
ifaddrs = True
ssl = True
zlib = True
locking = pthread_mutex
plugin_dir = .
timer = timerfd
yaml = embedded
json = False
filemonitor = inotify
routing = True
debug = False
capabilities = False
xml = libxml2
event = 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 而後再執行:web

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

也可使用pip進行安裝:django

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

三、nginx配置json

nginx的安裝,咱們在這裏就不講了,常常作的工做,相必你們都會,下面看一下vhost的配置:服務器

 
# cat localhost.conf
server{
  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;
  }
}

四、配置uwsgi。建立uwsgi配置文件/data/nginx/config/uwsgi.ini,詳細內容以下:app

 
# cat uwsgi.ini 
[uwsgi]
socket = 127.0.0.1:9090
master = true
pidfile = /var/run/uwsgi.pid
processes = 8
chdir = /data/www/OMserverweb
pythonpath = ..
profiler = true
memory-report=true
enable-threads=true
logdate=true
limit-as=6048
daemonize=/data/nginx/logs/django.log

進入到網站根目錄/data/www:socket

 
# 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-8

import os
import sys

# 將系統的編碼設置爲UTF8
reload(sys)
sys.setdefaultencoding('utf8')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OMserverweb.settings")

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

五、啓動:

 
# uwsgi --ini /data/nginx/conf/uwsgi.ini 
# /etc/init.d/nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

# ps aux | grep uwsgi
root     10159  0.0  0.3 156504  5920 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     10160  0.0  0.9 221416 18228 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     10161  0.0  0.9 221436 18208 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     10162  0.0  0.9 221552 18344 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     10163  0.0  0.9 221548 18364 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     10164  0.0  0.9 221552 18300 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     10165  0.0  0.7 215352 14252 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     10166  0.0  0.9 221448 18224 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     10167  0.0  0.7 215352 14248 ?        S    11:32   0:00 uwsgi --ini /data/nginx/conf/uwsgi.ini
root     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!

說明環境搭建成功

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息