Linux 下 將使用Python-Django開發的web應用佈置到服務器上(親測有效)

寫在前面:html

Django是一個卓越的新一代Web框架,相信使用Python的人對此並不陌生,但將咱們完成的web應用佈置到到服務器上並非一件容易的事情。python

Django詳細的教程能夠參考http://python.usyiyi.cn/django/index.html。nginx

Django有本身的一個調試服務器,經過在項目文件夾下執行:web

  python  manage.py runserver 8080(參數8080是設置的端口號,指明該服務器使用端口號爲8080)shell

可是此語句也僅限在本身的機器上進行調試本身的項目,並不能將其佈置在服務器上,供其餘用戶使用。django

因此此處,我將介紹一種詳細的佈置過程(親測有效),有問題歡迎你們評論探討。vim

 

使用Nginx和uWSGI部署Python Web站點的方法。OS:Ubuntu Server 14.04 LTS瀏覽器

 

基礎環境配置
sudo apt-get install python-setuptools服務器

 

安裝Nginx框架

什麼是Nginx?

Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行.


首先須要添加Nginx庫到apt-get source中:

sudo add-apt-repository ppa:nginx/stable
# 若是報錯 sudo: add-apt-repository: command not found,請先安裝software-properties-common包
# 安裝software-properties-common包
sudo apt-get install software-properties-common

 

升級已有的包,並確保系統上有uWSGI所需的編譯器和工具:
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential python-dev

 

安裝:
sudo apt-get install nginx
sudo /etc/init.d/nginx start
以上步驟完畢,就能夠在瀏覽器訪問127.0.0.1並見到頁面:welcome nginx

 


安裝uWSGI
Nginx是一個提供靜態文件訪問的web服務,然而,它不能直接執行託管Python應用程序,而uWSGI解決了這個問題。

WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx)與應用服務器(如uWSGI服務器)通訊的一種規範。
sudo pip install uwsgi

 

配置Nginx
咱們不須要對Nginx默認配置文件(/etc/nginx/sites-enabled/default)作任何改變,只須要在相應的Web應用裏單獨配置便可。這樣作的好處就是:各項目配置隔離,不干擾其餘項目。

 

如下爲例子,首先咱們編輯配置文件,找到include項的位置,增長鬚要部署項目的nginx配置文件。

 

sudo vim /etc/nginx/nginx.conf
# 增長如下行 (全部Web應用都放在一個文件夾,方便之後reload Nginx)
include /data/www/*/conf/nginx.conf;×(放在http第一行便可)

 

# reload (若是使用了restart,配置文件錯誤致使全部全掛)
sudo /etc/init.d/nginx reload

 

# 也能夠先檢查配置文件是否正確
sudo nginx -t
出現如下表明檢查所有經過
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

 

# 更多幫助
sudo nginx -h

 


Demo for nginx.conf
(只包括如下代碼便可,記得修改路徑)
server {

listen 8080;
server_name home;
index index.html index.htm;

 

access_log /home/tom/www/mysite/logs/access.log;
error_log /home/tom/www/mysite/logs/error.log;

 

location /favicon.ico {
alias /home/tom/www/favicon.ico;
}

 

location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/mysite.sock;
}

 

location /static {
alias /home/tom/www/static;
}

 

location /media {
alias /home/tom/www/media;
}

 

}
此時,訪問8010端口再也不是404而應該是502頁面。以下圖所示:

 

 

 

這是正常的,Nginx和uWSGI是經過socket文件鏈接的。因爲咱們並無配置uWSGI,這個文件並不存在。

 

配置uWSGI
首先肯定uwsgi是能夠正常啓動並服務的,因爲咱們使用的是虛擬環境,因此使用如下命令:
uwsgi --http :8000 --chdir path/conf --home path/env/ --wsgi-file path/conf/wsgi.py
若是在瀏覽器能夠經過ip:8000訪問,表示OK。

 

檢查經過後,開始使用文件配置:(如下面爲例子)
[uwsgi]

 

#permissions for the socket file
chmod-socket = 666

 

# variables
projectname = mysite
projectdomain = /home/tom/www
base = /home/tom/www/mysite
LC_ALL = zh_CN.UTF-8

 


# plugins
protocol = uwsgi
plugins = python

 

# the base directory(full path)
chdir = %(base)/mysite

 

# django wsgi.py
module = wsgi

 

 

 

socket = /tmp/mysite.sock
buffer-size = 32768
threads = 10
master = true

----------------------------------

 

修改:
wsgi.py中的「setting」
setting 中的 「urls」

(有待詳細說明)

--------------------------------

 

 

執行uWSGI,用新建立的配置文件做爲參數:

 

uwsgi --ini path/conf/uwsgi.ini

 


可能發生的錯誤: !!! UNABLE to load uWSGI plugin: ./python_plugin.so: cannot open shared object file: No such file or directory !!!

 

首先安裝:

 

sudo apt-get install uwsgi-plugin-python
以上不能解決,請先檢查uwsgi版本和配置。使用:

 

/usr/bin/uwsgi --ini path/conf/uwsgi.ini

 

 

可能發生權限問題執行如下語句:
sudo chmod -R 777 ./調節權限

 

 

------------------------------------------------------

一次安裝成功後,若修改文件內容只需重複執行如下語句
sudo /etc/init.d/nginx reload
uwsgi --ini conf/uwsgi.ini

 

-----------歡迎評論討論---------------------

 

申明:此文章由The_Third_Wave發表於:http://www.ttwshell.com/ ,轉載需帶上此聲明和本文連接。

相關文章
相關標籤/搜索