码迷,mamicode.com
首页 > 编程语言 > 详细

Linux 下 将使用Python-Django开发的web应用布置到服务器上(亲测有效)

时间:2016-03-21 22:56:56      阅读:763      评论:0      收藏:0      [点我收藏+]

标签:

写在前面:

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

 

-----------欢迎评论讨论---------------------

 

申明:此文章由The_Third_Wave发表于:http://www.ttwshell.com/ ,转载需带上此声明和本文链接。

Linux 下 将使用Python-Django开发的web应用布置到服务器上(亲测有效)

标签:

原文地址:http://www.cnblogs.com/shuaishuai-it/p/5304056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!