django+nginx+uwsgi部署web網站

來源:互聯網
上載者:User

標籤: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:8088chdir=/opt/cmdbwsgi-file = cmdb/wsgi.pypidfile = /var/run/uwsgi.piddaemonize = /var/log/uwsgi.logperl-auto-reload = 2#buffer-size = 102400master = trueprocesses = 2threads = 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 -ePATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC="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" instart)    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-develyum -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網站

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.