django 部署

通常將nginx作爲服務器的最前端,nginx把所有靜態請求自已來處理,nginx將所有非靜態請求通過uwsgi傳遞給django,完成一次web請求

 
一 安裝python 2.7.6

1.1 終端運行

#curl -kL http://xrl.us/pythonbrewinstall | bash

1.2 在~/.bashrc 末尾添加

[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc

1.3 退出控制檯以重新運行shell(如果被牆的話,到官網上下載pythonbrew1.3.4的安裝包)

export PYTHONBREW_ROOT=/path/to/pythonbrew  
curl -kLO http://xrl.us/pythonbrewinstall  
chmod +x pythonbrewinstall  
./pythonbrewinstall  
pythonbrew install 2.7.5  
pythonbrew switch 2.7.5

二 easy_install 國內源添加(防止被牆)

$ easy_install -i http://mirrors.tuna.tsinghua.edu.cn/pypi/simple package  (以清華mirrors爲例)

$ vim ~/.bashrc

  alias easy_install='easy_install -i http://mirrors.tuna.tsinghua.edu.cn/pypi/simple'

$ source ~/.bashrc

三 pip 安裝

$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ python get-pip.py

四 uwsgi安裝
4.1 安裝

export LDFLAGS="-Xlinker --no-as-needed"
pip install uwsgi

4.2 測試

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

執行shell命令

uwsgi --http :8001 --wsgi-file test.py

訪問網頁

http://127.0.0.1:8001/

4.3 配置django

測試django是否能跑起來,然後關閉django程序

./manage.py runserver 0.0.0.0:80

編寫django_wsgi.py,將其放在manage.py的同一個目錄下,編寫文件時注意mysite.settings要改爲yourproject.settings

#!/usr/bin/env python
# coding: utf-8

import os
import sys

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

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

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

測試運行

uwsgi --http :8000 --chdir /path/to/mysite --module django_wsgi

五 nginx安裝
5.1 下載並安裝

apt-get install nginx

5.2 在manage.py 的同級目錄下創建uwsgi.xml,注意修改/path/to/mysite

<uwsgi>
    <socket>:8077</socket>
    <chdir>/path/to/mysite</chdir>
    <module>django_wsgi</module>
    <processes>4</processes> <!-- 進程數 -->
    <daemonize>uwsgi.log</daemonize>
</uwsgi>

 5.3 配置 /etc/nginx/nginx.conf

server {

        listen   80;
        server_name www.you.com;
        access_log /home/work/var/test/logs/access.log;
        error_log /home/work/var/test/logs/error.log;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
         include        uwsgi_params;
         uwsgi_pass     127.0.0.1:8077;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /static/ {
            alias  /home/work/src/sites/testdjango1/testdjango/collectedstatic/;
            index  index.html index.htm;
        }

        location /media/ {
            alias  /home/work/src/sites/testdjango1/testdjango/public/media/;
        }
    }

重啓nginx 服務器

nginx -s reload

啓動uwsgi服務器,在瀏覽器中輸入域名,查看是否生效

uwsgi -x uwsgi.xml

關閉服務的方法,將uwsgi進程殺死即可

 

 

 

 

 

 

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章