環(huán)境:
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、做網(wǎng)站、昆山網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、昆山網(wǎng)絡(luò)營銷、昆山企業(yè)策劃、昆山品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供昆山建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址: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
文件內(nèi)容:
[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:常用參數(shù)和選項(xiàng)
關(guān)于參數(shù)的具體使用,可以閱讀官方文檔http://uwsgi-docs.readthedocs.org/en/latest/Options.html ,在這里列出一些常用的參數(shù):
chdir 項(xiàng)目目錄
home virtualenv目錄(如沒有運(yùn)行virtualenv虛擬環(huán)境,則無需設(shè)置)
socket 套接字文件或TCP套接字,例如:site1.uwsgi.sock 或 127.0.0.1:8000
uid 用戶id
gid 用戶組id
processes 工作進(jìn)程數(shù)
harakiri 進(jìn)程超過該時(shí)間未響應(yīng)就重啟該進(jìn)程(默認(rèn)單位為秒)
module 要啟動(dòng)的wsgi模塊入口,如:mysite.wsgi:application
ini 指定ini配置文件
xml 指定xml配置文件(與ini類似)
file 指定要運(yùn)行的wsgi程序文件,如:test.py
emperor Emperor模式
so-keepalive 開啟TCP KEEPALIVE(unix套接字方式下無效)
uwsgi服務(wù)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 ##啟動(dòng)腳本路徑 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; } }
啟動(dòng)站點(diǎn)
1、啟動(dòng)nginx服務(wù)
/etc/init.d/nginx start (刪除默認(rèn)的default.conf配置)
2、啟動(dòng)uwsgi
/etc/init.d/cmdb start
排錯(cuò):
1、在實(shí)際操作中發(fā)現(xiàn),啟動(dòng)uwsgi服務(wù)后,訪問站點(diǎn)出現(xiàn)“502 Bad Gateway”的報(bào)錯(cuò),后來發(fā)現(xiàn)是在settings中設(shè)置了不允許訪問站點(diǎn)
ALLOWED_HOSTS = []
改成
ALLOWED_HOSTS = [‘*’]
后問題解決。
2、由于python2.6 不支持django1.8 ,所以需要在服務(wù)器上安裝python2.7,并且在安裝之前,最好輸入以下命令,將可能用到的包都裝上,否則出現(xiàn)問題時(shí),需要重新編譯安裝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時(shí),一定要用pip2.7(用python2.7安裝的pip) 進(jìn)行安裝
4、invalid request block size: 4161 (max 4096)...skip報(bào)錯(cuò)解決
在訪問站點(diǎn)時(shí),出現(xiàn)了invalid request block size: 4161 (max 4096)...skip報(bào)錯(cuò)解決的報(bào)錯(cuò)。
解決辦法是在uwsgi.ini配置文件中增加一條配置:buffer-size = 102400
將buffer-size設(shè)置大一些
參考鏈接: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