環境:html
django:1.8.16 python
python:2.7.13nginx
pip:2.7sql
uwsgi:2.0.15django
project路徑: /opt/cmdb/bash
Uwsgi的安裝配置服務器
一、安裝python2.7 (省略安裝過程)app
二、安裝pip2.7 (省略安裝過程)python2.7
三、安裝uwsgi(注意:要用pip2.7安裝)socket
pip2.7 install uwsgi pip2.7 install requests ln -s /usr/local/python2.7/bin/uwsgi /usr/bin/uwsgi
四、配置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安裝配置
一、安裝nginx
yum -y install nginx
二、配置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; } }
啓動站點
一、啓動nginx服務
/etc/init.d/nginx start (刪除默認的default.conf配置)
二、啓動uwsgi
/etc/init.d/cmdb start
排錯:
一、在實際操做中發現,啓動uwsgi服務後,訪問站點出現「502 Bad Gateway」的報錯,後來發現是在settings中設置了不容許訪問站點
ALLOWED_HOSTS = []
改爲
ALLOWED_HOSTS = [‘*’]
後問題解決。
二、因爲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
三、用pip安裝uwsgi時,必定要用pip2.7(用python2.7安裝的pip) 進行安裝
四、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