django+nginx+uwsgi部署web站点
环境:
创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站建设、做网站、昆山网络推广、重庆小程序开发、昆山网络营销、昆山企业策划、昆山品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供昆山建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
django:1.8.16
python:2.7.13
pip:2.7
uwsgi:2.0.15
project路径: /opt/cmdb/
Uwsgi的安装配置
1、安装python2.7 (省略安装过程)
2、安装pip2.7 (省略安装过程)
3、安装uwsgi(注意:要用pip2.7安装)
pip2.7 install uwsgi pip2.7 install requests ln -s /usr/local/python2.7/bin/uwsgi /usr/bin/uwsgi
4、配置uwsgi.ini
路径: /opt/cmdb/uwsgi.ini
文件内容:
[root@localhost cmdb]# cat uwsgi.ini
[uwsgi] socket = 127.0.0.1:8088 chdir=/opt/cmdb wsgi-file = cmdb/wsgi.py pidfile = /var/run/uwsgi.pid daemonize = /var/log/uwsgi.log perl-auto-reload = 2 #buffer-size = 102400 master = true processes = 2 threads = 4
Uwsgi:常用参数和选项
关于参数的具体使用,可以阅读官方文档http://uwsgi-docs.readthedocs.org/en/latest/Options.html ,在这里列出一些常用的参数:
chdir 项目目录
home virtualenv目录(如没有运行virtualenv虚拟环境,则无需设置)
socket 套接字文件或TCP套接字,例如:site1.uwsgi.sock 或 127.0.0.1:8000
uid 用户id
gid 用户组id
processes 工作进程数
harakiri 进程超过该时间未响应就重启该进程(默认单位为秒)
module 要启动的wsgi模块入口,如:mysite.wsgi:application
ini 指定ini配置文件
xml 指定xml配置文件(与ini类似)
file 指定要运行的wsgi程序文件,如:test.py
emperor Emperor模式
so-keepalive 开启TCP KEEPALIVE(unix套接字方式下无效)
uwsgi服务init脚本 /etc/init.d/cmdb
#!/bin/bash # Comments to support chkconfig on Linux # chkconfig: 35 85 15 # description: uwsgi is an HTTP(S) server, HTTP(S) reverse # # author mail@zhaoyanan.cn # # chmod +x /etc/rc.d/init.d/uwsgi # chkconfig --add uwsgi # chkconfig --level 2345 uwsgi on # # Change History: # date author note # 2016/11/16 mail@zhaoyanan.cn create, refer to nginx, and http://uwsgi-docs.readthedocs.io/en/latest/Management.html set -e PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="uwsgi daemon" NAME=uwsgi DAEMON=/usr/bin/$NAME ##指向uwsgi的命令路径 SCRIPTNAME=/etc/init.d/$NAME ##启动脚本路径 CONFFILE=/opt/cmdb/uwsgi.ini ##uwsgi.ini配置文件路径 PIDFILE=/var/run/uwsgi.pid ##pid文件路径 test -x $DAEMON || exit 0 d_start(){ $DAEMON --ini $CONFFILE || echo -n " already running" } d_stop() { $DAEMON --stop $PIDFILE || echo -n " not running" } d_reload() { $DAEMON --reload $PIDFILE || echo -n " counld not reload" } d_freload() { $DAEMON --die-on-term $PIDFILE || echo -n " counld not force reload" } case "$1" in start) echo -n "Starting $DESC:$NAME" d_start echo "." ;; stop) echo -n "Stopping $DESC:$NAME" d_stop echo "." ;; reload) echo -n "Reloading $DESC configuration..." d_reload echo "reloaded." ;; force_reload) echo -n "The official provision of the parameters, tested and found not to support..." # d_freload # echo "force reloaded." echo "." ;; restart) echo -n "Restarting $DESC: $NAME" d_stop sleep 2 d_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force_reload}" >&2 exit 3 ;; esac exit 0
Nginx安装配置
1、安装nginx
yum -y install nginx
2、配置nginx
[root@localhost cmdb]# cat /etc/nginx/conf.d/cmdb.conf
upstream django { server 127.0.0.1:8088; } server { listen 80; server_name 172.16.42.128; charset utf-8; client_max_body_size 10M; location /static { alias /opt/cmdb/static; } location / { uwsgi_send_timeout 300; uwsgi_connect_timeout 300; uwsgi_read_timeout 300; uwsgi_pass django; include /etc/nginx/uwsgi_params; } }
启动站点
1、启动nginx服务
/etc/init.d/nginx start (删除默认的default.conf配置)
2、启动uwsgi
/etc/init.d/cmdb start
排错:
1、在实际操作中发现,启动uwsgi服务后,访问站点出现“502 Bad Gateway”的报错,后来发现是在settings中设置了不允许访问站点
ALLOWED_HOSTS = []
改成
ALLOWED_HOSTS = [‘*’]
后问题解决。
2、由于python2.6 不支持django1.8 ,所以需要在服务器上安装python2.7,并且在安装之前,最好输入以下命令,将可能用到的包都装上,否则出现问题时,需要重新编译安装python2.7
yum -y install zlib-devel bzip2-devel openssl-devel yum -y install ncurses-devel sqlite-devel readline-devel yum -y install tk-devel gdbm-devel db4-devel libpcap-devel yum -y install xz-devel libffi-devel
3、用pip安装uwsgi时,一定要用pip2.7(用python2.7安装的pip) 进行安装
4、invalid request block size: 4161 (max 4096)...skip报错解决
在访问站点时,出现了invalid request block size: 4161 (max 4096)...skip报错解决的报错。
解决办法是在uwsgi.ini配置文件中增加一条配置:buffer-size = 102400
将buffer-size设置大一些
参考链接:http://blog.csdn.net/hshl1214/article/details/47294657
参考链接:
http://code.ziqiangxuetang.com/django/django-nginx-deploy.html
http://uwsgi-docs.readthedocs.io/en/latest/Options.html
本文题目:django+nginx+uwsgi部署web站点
标题路径:http://pwwzsj.com/article/jpophh.html