寫在前面:
Django是一個卓越的新一代Web框架,相信使用Python的人對此並不陌生,但將我們完成的web應用布置到到服務器上並不是一件容易的事情。
Django詳細的教程可以參考http://python.usyiyi.cn/django/index.html。
Django有自己的一個調試服務器,通過在項目文件夾下執行:
python manage.py runserver 8080(參數8080是設置的端口號,指明該服務器使用端口號為8080)
但是此語句也僅限在自己的機器上進行調試自己的項目,並不能將其布置在服務器上,供其他用戶使用。
所以此處,我將介紹一種詳細的布置過程(親測有效),有問題歡迎大家評論探討。
使用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
-----------歡迎評論討論---------------------