django+uwsgi+nginx部署


一、nginx安裝部署

官方文檔:http://nginx.org/

安裝當前穩定版本:1.12.2

安裝步驟:

#!/bin/bash
nginx_version=nginx-1.12.0
if [ -f "/usr/bin/wget" ];then
  echo "開始下載nginx...."
  wget http://nginx.org/download/$nginx_version.tar.gz
  if [ -f "./$nginx_version.tar.gz" ]; then
  echo "$nginx_version下載完畢!"
  fi
  echo "開始安裝nginx依賴包......"
  yum install g++ gcc openssl-devel pcre-devel zlib-devel -y
  echo "解壓nginx·......"
  tar xzvf $nginx_version.tar.gz
  echo "開始編譯........"
  cd $nginx_version
  ./configure  --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module
  make && make install
  echo "nginx 安裝完畢!"
else
  echo "wget is not found!"
fi

啟動與停止nginx:

####啟動
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

####停止
 /usr/local/nginx/sbin/nginx -s stop
二、uwsgi安裝配置

官方文檔:http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/Configuration.html

安裝:

###方法一
pip3 install uwsgi
###方法二:
python3 -m pip install uwsgi

 配置uwsgi:uwsgi可支持命令行啟動、json格式配置文件啟動、ini格式配置文件啟動、xml配置文件啟動

命令行啟動:可以使用uwsgi --help查看啟動選項

uwsgi --http :8000 --chdir /opt/app/devops --wsgi-file /opt/app/devops/devops/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

ini配置文件啟動:

常用配置參數

http : #協議類型和端口號

processes : #開啟的進程數量

workers : #開啟的進程數量,等同於processes(官網的說法是spawn the specified number ofworkers / processes)

chdir : #指定運行目錄(chdir to specified directory before apps loading)

wsgi-file : #載入wsgi-file(load .wsgi file)

stats : #在指定的地址上,開啟狀態服務(enable the stats server on the specified address)

threads : #運行線程。由於GIL的存在,我覺得這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)

master : #允許主進程存在(enable master process)

daemonize : #使進程在后台運行,並將日志打到指定的日志文件或者udp服務器(daemonize uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。

pidfile : #指定pid文件的位置,記錄主進程的pid號。

vacuum : #當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)
log-maxsize :#記錄日志配置最大多少,超過這個數會切割,單位kb
logto :#指定輸出日志到文件logto = /tmp/uwsgi.log

在django項目中與manage.py同級目錄創建配置文件,這里命名為uwsgi.ini

# uwsgi 配置文件
[uwsgi]
#端口
socket = :8000
# django項目絕對路徑
chdir = /opt/app/devops
# 模塊路徑(項目名稱.wsgi)可以理解為wsgi.py的位置
module = devops.wsgi
# 允許主進程
master = true
#最多進程數
processes  = 4
# 退出時候回收pid文件
vacuum = true
#日志大小配置500M
log-maxsize = 500000000 
#記錄日志配置
logto = /tmp/uwsgi.log

啟動uwsgi服務

 uwsgi --ini /opt/app/devops/uwsgi.ini --daemonize /tmp/uwsgi.log

 

三、配置nginx

nginx主要位置是代理轉發作用

nginx.conf

#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log  logs/access.log ;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
      }
    server {
    listen         80; 
    server_name    10.193.15.50;
    charset UTF-8;
    error_log       logs/devops_error.log;

    client_max_body_size 75M;

    location / { 
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 5;
    }   
    location /static {
        expires 30d;
        autoindex on; 
        add_header Cache-Control private;
        alias /opt/app/devops/static/;
     }
       }    
}

啟動nginx,訪問項目url若出現Exception Value: Invalid HTTP_HOST header異常 ,需要在settings.py中ALLOWED_HOSTS = ['*']

重啟腳本centos6.x

#!/bin/bash
if [ ! -n "$1" ]
then
    echo "Usages: sh uwsgiserver.sh [start|stop|restart]"
    exit 0
fi

if [ $1 = start ]
then
    psid=`ps aux | grep "uwsgi" | grep -v "grep" | wc -l`
    if [ $psid -gt 4 ]
    then
        echo "uwsgi is running!"
        exit 0
    else
        uwsgi /xxx/www/uwsgi.ini --daemonize /var/log/uwsgi.log --post-buffering 32768 --buffer-size 32768
        echo "Start uwsgi service [OK]"
    fi


elif [ $1 = stop ];then
    killall -9 uwsgi
    echo "Stop uwsgi service [OK]"
elif [ $1 = restart ];then
    killall -9 uwsgi
    uwsgi --ini /xxx/www/uwsgi.ini --daemonize /var/log/uwsgi.log  --post-buffering 32768 --buffer-size 32768 --touch-reload "/xxx/www/reload.set"
    echo "Restart uwsgi service [OK]"

else
    echo "Usages: sh uwsgiserver.sh [start|stop|restart]"
fi

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM