环境:
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安装)
1 2 3 | 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
1 2 3 4 5 6 7 8 9 10 11 | [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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #!/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
1 | yum -y install nginx |
2、配置nginx
[root@localhost cmdb]# cat /etc/nginx/conf.d/cmdb.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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中设置了不允许访问站点
1 | ALLOWED_HOSTS = [] |
改成
1 | ALLOWED_HOSTS = [‘*’] |
后问题解决。
2、由于python2.6 不支持django1.8 ,所以需要在服务器上安装python2.7,并且在安装之前,最好输入以下命令,将可能用到的包都装上,否则出现问题时,需要重新编译安装python2.7
1 2 3 4 | 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设置大一些