1. 程式人生 > >django+nginx+uwsgi部署web站點

django+nginx+uwsgi部署web站點

uwsgi+nginx+django

環境:

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

4、配置uwsgi.ini

路徑: /opt/cmdb/uwsgi.ini

文件內容:

[[email protected] 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腳本

#!/bin/bash
# Comments to support chkconfig on Linux
# chkconfig: 35 85 15
# description: uwsgi is an HTTP(S) server, HTTP(S) reverse
#
# author     [email protected]
/* */ # # chmod +x /etc/rc.d/init.d/uwsgi # chkconfig --add uwsgi # chkconfig --level 2345 uwsgi on # # Change History: # date author note # 2016/11/16 [email protected] 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

[[email protected] 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、在實際操作中發現,啟動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






本文出自 “zengestudy” 博客,請務必保留此出處http://zengestudy.blog.51cto.com/1702365/1930346

django+nginx+uwsgi部署web站點