溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

django+nginx+uwsgi部署web站點(diǎn)

發(fā)布時(shí)間:2020-07-01 10:07:36 來(lái)源:網(wǎng)絡(luò) 閱讀:1513 作者:zenge_blog 欄目:建站服務(wù)器

環(huán)境:

django:1.8.16  

python:2.7.13

pip:2.7

uwsgi:2.0.15

project路徑: /opt/cmdb/


Uwsgi的安裝配置

1、安裝python2.7 (省略安裝過(guò)程)

2、安裝pip2.7 (省略安裝過(guò)程)

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目錄(如沒(méi)有運(yùn)行virtualenv虛擬環(huán)境,則無(wú)需設(shè)置)

  • socket 套接字文件或TCP套接字,例如:site1.uwsgi.sock 或 127.0.0.1:8000

  • uid 用戶id

  • gid 用戶組id

  • processes 工作進(jìn)程數(shù)

  • harakiri 進(jìn)程超過(guò)該時(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 開(kāi)啟TCP KEEPALIVE(unix套接字方式下無(wú)效)


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ù)后,訪問(wèn)站點(diǎn)出現(xiàn)“502 Bad Gateway”的報(bào)錯(cuò),后來(lái)發(fā)現(xiàn)是在settings中設(shè)置了不允許訪問(wèn)站點(diǎn)

ALLOWED_HOSTS = []

改成

ALLOWED_HOSTS = [‘*’]

后問(wèn)題解決。


2、由于python2.6 不支持django1.8 ,所以需要在服務(wù)器上安裝python2.7,并且在安裝之前,最好輸入以下命令,將可能用到的包都裝上,否則出現(xiàn)問(wè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ò)解決

在訪問(wèn)站點(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






向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI