Autor: wangxianlong

2016/7/10 16:17:55

环境:

  • centos 6.5
  • python 2.7.5
  • django 1.9
  • nginx 1.8
  • selinux diabled
  • 项目目录: /data/task/xuptlib/

安装必要的软件

python

由于centos自带的python2.6.6已经驱动不起来django1.9了. 大概会报这样的错误

Traceback (most recent call last):File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_versionFile "/usr/lib/python2.6/site-packages/django/utils/version.py", line 7, in <module>
from django.utils.lru_cache import lru_cacheFile "/usr/lib/python2.6/site-packages/django/utils/lru_cache.py", line 28
fasttypes = {int, str, frozenset, type(None)},
SyntaxError: invalid syntax

所以我们升级为python2.7.5. 我们用来pyenv来管理环境,当然也可以重新编译python

# 下载pyenv脚本
wget https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer chmod +x pyenv-installer
./pyenv-installer # 配置环境
cat /etc/profile.d/pyenv.sh
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)" source /etc/profile # 安装python 2.7.5
pyenv install 2.7.5 # 使用 python2.7.5
pyenv local 2.7.5
pyenv rehash

django

yum install -y  python-pip
pip install django==1.9
pip install Mysql-python # 连接数据库

nginx

wget http://nginx.org/download/nginx-1.8.1.tar.gz

./configure --prefix=/usr/local/nginx --with-http_stub_status_module--with-http_gzip_static_module && make && make install

ln -s /usr/local/nginx/sbin/nginx /usr/bin/

uwsgi

pip install uwsgi

测试一下: uwsgi --http :8000 --wsgi-file test.py

# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2 在浏览器中看到helloworld

部署

首先看下流程,知道流程思路清晰,做的才不会乱,问题也好解决:

the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django

1. 部署nginx

配置文件

cat /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 2; #主进程数 events {
use epoll; # 使用epoll I/O模型
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream;
server_tokens off; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 45; server {
listen 80;
server_name x.x.x.x;
charset utf-8;
client_max_body_size 32M; location /static {
alias /data/task/xuptlib/staticfile; #静态文件位置,自己需要使用django来collect静态文件
} location / { # 注意使用scoket,免去TCP的连接消耗
uwsgi_pass unix:///data/task/xuptlib/bookhelper.sock;
include uwsgi_params; # 在配置文件conf中
}
} }

配置完成:

nginx -t
nginx

2. django的部署

2.1 关闭django的debug

DEBUG = False
ALLOWED_HOSTS = ['*'] # 注意允许的主机不要忘写了, 否则HTTP400会来 # 顺带提一下可以关闭url中admin后台管理

2.2 配置静态文件收集

# 刚才有在nginx配置文件中提到哦!!
STATIC_ROOT = os.path.join(BASE_DIR, "staticfile/") # 收集
python manage.py collectstatic

3. uwsgi

可以理解uwsgi在django和nginx搭了桥. 身份有点像Tomcat

配置文件/或者直接用命令

cat /etc/uwsgi.ini
[uwsgi]
chdir = /data/task/xuptlib #项目父目录
module = bookhelper.wsgi #项目下的wsgi文件位置
socket = /data/task/xuptlib/bookhelper.sock # socket 文件位置,和nginx配置文件中的一样哦!!
chmod = 666 # socket 权限, 不够的话,会permission denied
master = true
processes = 3 # 起的三个子进程数
vacuum = true # 退出清理环境
pidfile = /var/run/uwsgi.pid
daemonize = /var/log/uwsgi.log # 日志位置

uwsgi启动脚本

[root@rikewang xuptlib]# cat /etc/init.d/uwsgi
#! /bin/sh PATH="/root/.pyenv/plugins/pyenv-virtualenv/shims:/root/.pyenv/shims:/root/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" NAME=uwsgi
EXEC=uwsgi
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME do_start() {
$EXEC $CONFIGFILE &> /dev/null || echo -n "uwsgi already running"
} do_stop() {
$EXEC --stop $PIDFILE &> /dev/null || echo -n "uwsgi not running"
rm -f $PIDFILE
}
do_reload() {
$EXEC --reload $PIDFILE &> /dev/null || echo -n "uwsgi can't reload"
}
do_status() {
ps aux|grep $EXEC
} case "$1" in
status)
echo -en "Status $NAME: \n"
do_status
;;
start)
echo -en "Starting $NAME: \n"
do_start
;;
stop)
echo -en "Stopping $NAME: \n"
do_stop
;;
reload|graceful)
echo -en "Reloading $NAME: \n"
do_reload
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
exit 3
;;
esac exit 0

在回顾一下流程:

the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django

部署django应用的更多相关文章

  1. mac osx 上面部署Django项目 apache+mysql+mod_wsgi

    1.安装Xcode command line tools 首先,编译mysql和Homebrew需要用到Xcode command line tools,所以首先安装command line tool ...

  2. Ubuntu上通过nginx部署Django笔记

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式.今天在Ubuntu上使用Nginx部署Django服务,虽然不是第一次搞这个了,但是发现还是跳进了好多坑,g ...

  3. Apache2.4部署django出现403 Forbidden错误解决办法

    前言:Apache2.4部署django出现403 Forbidden错误最好要结合apache中的错误日志来观察出现何种错误导致出现403错误 下午百度了一下午没找到解决办法,试了n种方法,简直坑爹 ...

  4. 五步教你实现使用Nginx+uWSGI+Django方法部署Django程序

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...

  5. Ubuntu16.04 apache2 wsgi 部署django

    在Ubuntu16.04上部署django其实还算简单直观,最重要的问题就是路径设置正确,并且保证版本统一,这个测试是在 Apache/2.4.18 (Ubuntu)  apt-get install ...

  6. 通过Nginx部署Django(基于ubuntu)

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...

  7. apache部署django记录

    在ubuntu下通过apache部署django 首先需要下载python,django,apache以及wsgi模块 python基本已经自带,我用的是2.7,不是的话可以重新装一个 下载djang ...

  8. Docker 使用指南 (六)—— 使用 Docker 部署 Django 容器栈

    版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/98 来源:腾云阁 https://www.qclou ...

  9. Windows下Apache部署Django过程记录

    Win7/Apache/Python2.7/Django1.9部署Web   环境: Windows7 Apache httpd-2.4.16-win64-VC14 Python2.7.11 Djan ...

  10. fabric自动化部署django

    使用fabric部署django应用 使用fabric部署django应用 本文是我的网站易读中文网自动化部署的脚本实现,以下代码在ubuntu和debian中测试通过 由于网站使用的是python技 ...

随机推荐

  1. 该如何理解AMD ,CMD,CommonJS规范--javascript模块化加载学习总结

    是一篇关于javascript模块化AMD,CMD,CommonJS的学习总结,作为记录也给同样对三种方式有疑问的童鞋们,有不对或者偏差之处,望各位大神指出,不胜感激. 本篇默认读者大概知道requi ...

  2. android anr分析方法

    目录(?)[+] 案例1关键词ContentResolver in AsyncTask onPostExecute high iowait 案例2关键词在UI线程进行网络数据的读写   一:什么是AN ...

  3. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

  4. Virtualbox虚拟机安装Ubuntu图文版

    这篇文章给大家介绍一下如何在Windows系统下的Virtual Box虚拟机软件中安装Ubuntu系统. 适用环境:Windows系统作为物理机,在此平台上搭建一个Virtual Box虚拟平台,在 ...

  5. Java初学(七)

    一.内部类 1.内部类概述:把类定义在其他类内部,这个类被称为内部类(内部类可以使用static修饰,外部类不可) 2.内部类访问特点:内部类可以直接访问外部类成员,包括私有的     外部类要访问内 ...

  6. html input file标签的上传文件 注意点

    文件上传框  代码格式:<input type=“file” name=“...” size=“15” input enctype="multipart/form-data“ maxl ...

  7. Python2.x与Python3.x的区别

    这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...

  8. MySQL主从分离读写复制

    参考教程:http://www.yii-china.com/post/detail/283.html MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器( ...

  9. js ajax 向后台传递数组

    //定义数组 var _arrays = []; for (var i = 0; i < 10; i++) { _arrays.push(i); } //post $.post(href, {l ...

  10. BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler错误的解决方法

    出现这个问题是因为我的spring3.0里的包是单独引用的,缺少了别的包 譬如Configuration problem: Unable to locate Spring NamespaceHandl ...