django+uwsgi+nginx部署

1、nginx安裝部署

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

安裝當前穩定版本:1.12.2
python

安裝步驟:nginx

#!/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:django

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

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

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

安裝:centos

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

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

命令行啓動:能夠使用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配置文件啓動:app

經常使用配置參數socket

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

 

3、配置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
相關文章
相關標籤/搜索