朝花夕拾|勿忘初心 朝花夕拾|勿忘初心

部署Django

in 杂七杂八 read (302) 583汉字 站长Lucifaer 文章转载请注明来源!

接上一篇。

这边说一句,git hooks配合supervisor可以完成远程代码实时更新,对于开发人员来说是非常方便的开发环境。当然对于我的wiki来说,我会随时添加一些新的功能,需要频繁的修改代码,git hooks+supervisor可以完美的符合我的需求。

0x00 简介

在官方文档中搜索deploy官方文档会告诉你,Django私有的部署平台是WSGI,而官方对于WSGI服务的部署给出了以下几个方法,我们这次试用的是gunicorn

0x01 Get Start

首先安装gunicorn包:

pip install gunicorn

接下来检查是否存在<project_name>/wsgi.py文件(如果试用命令startproject来创建的项目,会默认存在)。若存在,就可以在项目根目录来执行:

gunicorn myproject.wsgi

访问127.0.0.1:8000来查看服务是否正常,如果没问题,我们来写个shell脚本,方便我们利用gunicorn来启动服务:

#!/bin/bash

NAME="D0g3_wiki"                                  # Name of the application
DJANGODIR=/home/lucifaer/D0g3_wiki             # Django project directory
SOCKFILE=/home/lucifaer/D0g3_wiki/run/gunicorn.sock  # we will communicte using this unix socket
USER=lucifaer                                     # the user to run as
GROUP=lucifaer                                 # the group to run as
NUM_WORKERS=3                                     # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=D0g3_wiki.settings             # which settings file should Django use
DJANGO_WSGI_MODULE=D0g3_wiki.wsgi                     # WSGI module name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
export PATH=/srv/http/.gem/ruby/2.4.0/bin:$PATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /home/lucifaer/.pyenv/versions/3.6.3/bin/gunicorn -b 0.0.0.0:8000 ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
#  --bind=unix:$SOCKFILE \
  --log-level=debug \
  --reload \
  --log-file=-

PS:这是我的配置文件,请根据自己的目录修改变量。

之后更改该文件的权限为可执行。

最后一步就是把我们Django项目中的settings.py做如下更改:

即关闭debug模式,并允许所有的host访问。

以上就完成了最基本的Django的部署,接下来我们要设置supervisor,来将这个进程设置为守护进程。

0x02 Supervisor

Superviosr是一个进程监管的工具。简而言之,Superviosr可以保证你的程序在服务器开机时自动启动以及程序意外终止时重新启动。通过下面的命令即可安装:

sudo apt install supervisor

接下来就是配置Supervisor的配置文件,以达成监管目的。一般配置文件都放在/etc/supervisor/conf.d目录下。我们可以在该目录下创建自定义的配置文件,下面是我的配置文件:

[program:D0g3_wiki]
command = /home/lucifaer/D0g3_wiki/gunicorn_start ; Command to start app
directory = /home/lucifaer/D0g3_wiki/
user = lucifaer ; User to run as
stdout_logfile = /home/lucifaer/logs/gunicorn_supervisor.log ; Where to write log messages
redirect_stderr = true ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8 ; Set UTF-8 as default encoding
autostart=true
autorestart=true

日志文件需要手动创建:

$ touch /home/lucifaer/logs/gunicorn_supervisor.log

接下来用supervisorctl来启动设置:

$ sudo supervisorctl reread 
D0g3_wiki: available 
$ sudo supervisorctl update 
D0g3_wiki: added process group

之后就可以开启进程了:

$ sudo supervisorctl start D0g3_wiki

0x03 Nginx

Nginx安装步骤就省略了,不要太简单。

之后我们需要的就是为Django项目创建一个虚拟sever配置。

每个Nginx的虚拟sever都由/etc/nginx/sites-available路径下的一个配置文件来表示,而将其链接到的/etc/nginx/sites-enabled路径下则可以启用对应的站点。

我们在/etc/nginx/sites-available为Django项目创建一个配置文件,内容如下:

upstream D0g3_wiki_app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).

server unix:/home/lucifaer/D0g3_wiki/D0g3_wiki/run/gunicorn.sock fail_timeout=0;
}

server {

listen 80;
server_name example.com;

client_max_body_size 4G;


location /static/ {
alias /home/lucifaer/D0g3_wiki/static/;
}


location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;

# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;

# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;

# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;

# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass 你的代理ip,也就是你的ip(或域名)+8000端口;
break;
}
}

# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/lucifaer/D0g3_wiki/static/;
}

将这个文件链接到site_enabled目录下:

$ sudo ln -s /etc/nginx/sites-available/D0g3_wiki.conf /etc/nginx/sites-enabled/D0g3_wiki.conf

然后重启Nginx就好了

$ sudo service nginx restart
python3Django
最后由Lucifaer修改于2018-03-02 17:45

此处评论已关闭

博客已萌萌哒运行
© 2018 由 Typecho 强力驱动.Theme by Yodu
PREVIOUS NEXT
雷姆
拉姆