Nginx uWsgi Django環境搭建

因爲一個監控項目(使用django開發)交接到所在項目組,須要瞭解python的web開發和環境搭建。html

這裏記錄環境的搭建流程,以及遇到的問題和處理方案。python

環境信息: CentOS 6.6 (內網IP:100.84.73.45) Nginx 1.2.8 Python 2.6.6 Django 1.6 uwsgi 2.0.11.2nginx

注意:Django1.6匹配的python最高版本是2.6,若是安裝過高版本的django,須要使用python2.6以上版本,不然安裝過程會報錯:invalid syntaxgit

這裏個人機器自帶了python2.6.6,就不介紹python的安裝了。github

安裝pip

pip是python的包管理工具,咱們使用pip進行python的管理web

# wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
# python get-pip.py

安裝uWSGI

uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。django

  • 使用pip安裝 uwsgi
# export LDFLAGS="-Xlinker --no-as-needed"
# pip install uwsgi
  • 測試uwsgi

建立一個test.py的測試文件vim

# pwd
/home/test/django
# touch test.py
# vim test.py

test.py內容以下:瀏覽器

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"
  • 啓動uwsgi
# pwd
/home/test/django
# uwsgi --http :13010 --wsgi-file test.py

在瀏覽器中輸入:http://100.84.73.45:13010服務器

uwsgi http方式啓動

uwsgi安裝和啓動正常。

安裝django

  • 使用pip安裝 django

因爲使用的是python2.6.6,所以須要安裝1.6版本的django。

安裝指定版本的django的方式以下:

# pip install -v django==1.6
  • 新建Django工程
# pwd
/home/test/django
#  django-admin.py startproject MySite
# ls
MySite  test.py
  • 使用uwsgi啓動django工程
# pwd
/home/test/django
# cd MySite 
# uwsgi --http :13010 --wsgi-file MySite/wsgi.py

在瀏覽器中輸入:http://100.84.73.45:13010

uwsgi django結合

到此django安裝成功,而且和uwsgi結合成功。

  • 補充信息

也能夠使用以下命令啓動django工程

# pwd
/home/test/django/MySite
# uwsgi --http :13010 --module MySite.wsgi

若是是單獨調試django,能夠使用django自帶的簡單服務器進行。

# pwd
/home/test/django/MySite
# python manage.py runserver 0.0.0.0:13010

安裝nginx

  • 源碼安裝nginx

咱們採用源碼方式安裝。 搜狐鏡像下載地址:http://mirrors.sohu.com/nginx/ 我選擇的是 1.2.8 版本

安裝步驟,能夠看這裏:http://xyuex.blog.51cto.com/5131937/1013414

  • 配置nginx
# pwd
/home/test/local/nginx
# touch nginx_django_unix.conf

nginx_django_unix.conf內容以下:

worker_processes  2;
 
events {
    use epoll;
    worker_connections  1024;
}

http {
    include       conf/mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;

    upstream django {
        #server 127.0.0.1:13010; # nginx使用tcp方式與uwsgi交互
        server unix:///home/test/django/MySite/MySite.sock; # nginx使用unix本地socket方式與uwsgi交互
    }

    server {
        listen       13000;
        server_name  uwsgi_django;
        client_max_body_size 75M;
 
        location / {
            uwsgi_pass django;
            uwsgi_pass_header http;
            
            uwsgi_read_timeout 300;
            include conf/uwsgi_params;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 }
  • 啓動uwsgi 和 nginx
# pwd
/home/test/django/MySite
# uwsgi --socket ./MySite.sock --module MySite.wsgi
# cd ~/local/nginx
# pwd
/home/test/local/nginx
# sbin/nginx -c nginx_uwsgi.conf

在瀏覽器中輸入:http://100.84.73.45:13000

nginx uwsgi django打通

到此 nginx uwsgi django三個完成告終合。

過程當中遇到的問題

uwsgi的交互方式

nginx與uwsgi有三種交互方式:http、tcp、unix本地sock http方式主要是利用nginx的反向代理功能。 TCP方式是ngix經過tcp方式和uwsgi交互。 unix本地sock是經過本地的sock文件進行交互。

  • 若是使用本地sokc方式,uwsgi的啓動應該是
# uwsgi --socket ./MySite.sock --module MySite.wsgi

而且nginx的upsteam配置sock文件地址

  • 若是是TCP方式,uwsig的啓動應該是
# uwsgi --socket :13010 --module MySite.wsgi

而且nginx的upsteam配置ip和端口

  • 採用反向代理方式,uwsgi的啓動應該是
# uwsgi --http:13010 --module MySite.wsgi

nginx的完整配置以下:

worker_processes  2;
 
events {
    use epoll;
    worker_connections  1024;
}

http {
    include       conf/mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;

    upstream django {
        server 127.0.0.1:13010;
    }

    server {
        listen       13000;
        server_name  uwsgi_django;
        client_max_body_size 75M;
 
        location / {
            proxy_pass http://django; # 注意這裏使用的是proxy_pass協議是http
            
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 }
相關文章
相關標籤/搜索