部署Django Application(一)使用Nginx + gunicorn + supervisord

初始化服務器環境

yum -y install vim git screen python-pip
curl -L https://github.com/robbyrussell/oh-my-    zsh/raw/master/tools/install.sh | sh
pip install virtualenvwrapper gunicorn django
mkdir /usr/local/virtualenv
mkvirtualenv python

配置gunicorn服務器

Gunicorn(gunicorn.org)是一個Python WSGI UNIX的HTTP服務器。,從Ruby的獨角獸(Unicorn)項目移植。

在Django項目下建立shell腳本

#!/bin/bash
set -e 
LOGFILE=guni.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=3  # cpu core nums * 2 + 1
USER=nobody
GROUP=nogroup
# WORKER=gevent # install python gevent
ADDRESS=127.0.0.1:8000
test -d $LOGDIR || mkdir -p $LOGDIR
exec gunicorn_django -w $NUM_WORKERS --bind=$ADDRESS \
 # -k $WORKER 
  --daemon \
  --user=$USER --group=$GROUP --log-level=error \
  --log-file=$LOGFILE 2>>$LOGFILE

安裝Nginx用作反向代理與靜態服務器

  • 使用http://lnmp.org/ 安裝Nginx

  • 用Lnmp一鍵安裝包帶的腳本新建一個虛擬主機

  • 更改主機的Nginx配置文件

      server {
    
          listen       80;
          server_name  nuptapi.nupter.org;
          access_log   /home/wwwlogs/nuptapi.nupter.org.log;
          error_log    /home/wwwlogs/nuptapi.nupter.org.error.log;
          root /home/wwwroot/nuptapi.nupter.org;
    
          location  /static/ {
              alias /home/wwwroot/nuptapi.nupter.org/static/;
          }
    
          location  / {
              proxy_pass            http://127.0.0.1:8000;
              proxy_redirect        off;
              proxy_set_header      Host             $host;
              proxy_set_header      X-Real-IP        $remote_addr;
              proxy_set_header      X-Forwarded-For  $proxy_add_x_forwarded_for;
              client_max_body_size  10m;
          }
    
      }
    

用Supervisord守護進程

  • echo_supervisord_conf > /etc/supervisord.conf
  • vim /etc/supervisord.conf

更改配置文件以下的內容

  • chown=lxy:lxy ; socket file uid:gid owner

  • [program:nuptapi]
    command=/home/wwwroot/django_helloworld/gunicorn_start.sh

  • Shell運行supervisord

  • 同時supervisord 默認會在9001端口打開一個HTTP服務器,可以在Nginx再配置一個反向代理來遠程登陸

其它

  • 配置DNS服務器,解析域名A記錄到服務器IP
  • 重啟Nginx
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容