部署django应用
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