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/ ,转载需带上此声明和本文连接。

相关文章
相关标签/搜索