当前位置: 移动技术网 > IT编程>脚本编程>Python > Django + Gunicorn + Nginx 部署之路

Django + Gunicorn + Nginx 部署之路

2019年09月11日  | 移动技术网IT编程  | 我要评论

keen官网,帝国总裁的冲喜新娘,降糖神茶

前言

最近,我已经成功将我的个人网站从 flask 迁移到 django 了,最早接触 django 的时候大概是在 4 年前,我记得那个时候 django 中的路由配置使用 正则 来进行的,但是我有特别烦这个东西,所以就果断弃坑了。然后今年年初的时候,我用 flask 写了一个我的个人网站,刚开始的时候功能还是比较简单,看着路由配置和部署规则都很方便,就果断采用了。但是后来我想添加的功能越来越多的时候,我发现我已经越来越难掌控它了,正好最近我稍微看了一下 django 这几年的变化,最新的 2.2 版本还是很不错的,路由规则和 flask 已经一致了,所以我就重新入坑了。

目前我的个人网站基本功能已经迁移完毕。但是在部署的时候,我遇到了一些问题,在网上看了一些解决方法,要么太乱,要么太旧,个人觉得都已经不太适用了。所以在这里记录一下我的部署过程。

部署

网上有很多都是用 uwsgi 的方式来部署,但是我个人比较喜欢 gunicorn,所以以下内容我只是记录了 django + gunicorn + nginxubuntu 上的部署方式相关内容。

步骤一

上传网站源码至目标服务器

由于我的源码是用 github 来托管的,所以我直接执行下述命令来克隆我的网站源码到服务器即可。

git clone https://github.com/your-name/repo-name.git

# 进入项目目录
cd repo-name

# 创建并激活虚拟环境
python3 -m virtualenv venv
source venv/bin/activate

# 安装项目依赖
pip install -r requirements.txt

目前我的网站采用的相关依赖包如下:

autopep8
django
django-bootstrap4
django-ckeditor
gunicorn
markdown
pillow
python-slugify
requests

这里有个坑需要注意,如果你使用了 awesome-slugify,请尝试使用 python-slugify,因为有的服务器可能无法正常安装 awesome-slugify,具体 bug 可参考:clashes with python-slugify package

步骤二

修改项目相关配置,并进行静态资源收集

由于我需要将我的网站部署到生产环境,所以我需要关闭 django 的调试模式,并修改静态资源相关配置,示例配置如下所示:

  • settings.py
secret_key = os.environ.get('django_secret_key')

debug = os.environ.get('django_debug', false)

template_debug = os.environ.get('django_template_debug', false)

allowed_hosts = ["*"]

templates = [
    {
        'backend': 'django.template.backends.django.djangotemplates',
        'dirs': [os.path.join(base_dir, 'templates')],
        'app_dirs': true,
        'options': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

static_url = '/static/'
static_root = os.path.join(base_dir, 'staticfiles')
staticfiles_dirs = [
    os.path.join(base_dir, 'static'),
]

media_url = '/media/'
media_root = os.path.join(base_dir, 'media')

然后执行如下命令进行静态资源收集:

python manage.py collectstatic

之后,我还需要创建一个 gunicorn 进程的相关配置,示例配置如下所示:

  • gunicorn.conf.py
# 安装
# sudo pip3 install gunicorn

import sys
import os
import logging
import logging.handlers
from logging.handlers import watchedfilehandler
import multiprocessing

base_dir = '/home/hippie/hippiezhou.fun/src'
sys.path.append(base_dir)

log_dir = os.path.join(base_dir, 'log')
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

# 绑定的ip与端口
bind = "0.0.0.0:8000"

# 以守护进程的形式后台运行
daemon = true

# 最大挂起的连接数,64-2048
backlog = 512

# 超时
timeout = 30

# 调试状态
debug = false

# gunicorn要切换到的目的工作目录
chdir = base_dir

# 工作进程类型(默认的是 sync 模式,还包括 eventlet, gevent, or tornado, gthread, gaiohttp)
worker_class = 'sync'

# 工作进程数
workers = multiprocessing.cpu_count()

# 指定每个工作进程开启的线程数
threads = multiprocessing.cpu_count() * 2

# 日志级别,这个日志级别指的是错误日志的级别(debug、info、warning、error、critical),而访问日志的级别无法设置
loglevel = 'info'

# 日志格式
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(l)s %(b)s %(f)s" "%(a)s"'
# 其每个选项的含义如下:
'''
h          remote address
l          '-'
u          currently '-', may be user name in future releases
t          date of the request
r          status line (e.g. ``get / http/1.1``)
s          status
b          response length or '-'
f          referer
a          user agent
t          request time in seconds
d          request time in microseconds
l          request time in decimal seconds
p          process id
'''

# 访问日志文件
accesslog = os.path.join(log_dir, 'gunicorn_access.log')
# 错误日志文件
errorlog = os.path.join(log_dir, 'gunicorn_error.log')
# pid 文件
pidfile = os.path.join(log_dir, 'gunicorn_error.pid')

# 访问日志文件,"-" 表示标准输出
accesslog = "-"
# 错误日志文件,"-" 表示标准输出
errorlog = "-"

# 进程名
proc_name = 'hippiezhou_fun.pid'

# 更多配置请执行:gunicorn -h 进行查看

之后可用通过如下方式启动我们的网站:

# 启动方式(首先需要切换到项目根目录,即和 manage.py 在同级目录下):

gunicorn -c gunicorn.conf.py website.wsgi:application

# 或
gunicorn website.wsgi:application -b 0.0.0.0:8000 -w 4 -k gthread

# 或
gunicorn website.wsgi:application -b 0.0.0.0:8000 -w 4 -k gthread  --thread 40 --max-requests 4096 --max-requests-jitter 512

# 查看进程
ps aux | grep gunicorn

步骤三

配置 nginx

通过前两步,我们可以成功将我们的网站跑起来,但是目前还只能在内部访问,所以我们需要通过 nginx 来做反向代理,供外网访问。

执行下述命令进行安装和配置

sudo apt-get install nginx

sudo service nginx start

# 备份默认配置
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

# 启动 vim 修改我们的网站配置
sudo vim /etc/nginx/sites-available/default

示例配置如下所示:

server{
        ...
        server_name hippiezhou.fun *.hippiezhou.fun;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        ...

        location / {
                # first attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                proxy_pass         http://127.0.0.1:8000; #此处要和你 gunicore 的 ip 和端口保持一致
                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;
                proxy_set_header   x-forwarded-proto    $scheme;
        }

        location /static {
                alias /root/hippiezhou.fun/src/staticfiles; # 此次需要配置为你的网站对应的静态资源的绝对路径
        }

        location /media {
                alias /root/hipiezhou.fun/src/media; # 如果你的网站有上传功能,需要配置该结点并指向目标路径
        }

        ...
}

配置完成后执行下述操作即可将我们的网站运行起来

# 若网站未启动执行该命令
gunicorn -c gunicorn.conf.py website.wsgi:application

sudo nginx -t
sudo service nginx restart

如果不出意外,网站应该是可以正常访问,如果静态资源依然不能访问,打开网站的 开发者工具看一下是什么错误。

  • 如果是 404 的问题,请确保你的 settings 相关配置和我上面列出来的是一致的;
  • 如果是 403 的问题,应该是 nginx 无权访问你指定的静态资源,你需要修改 nginx 的用户类型,亲执行下述命令
sudo vim /etc/nginx/nginx.conf

user 后面的值修改为 root,然后重启 nginx 即可。

最后,关于如何配置 https,这里就不过多介绍了,直接列出相关示例脚本:

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python-certbot-nginx

sudo certbot --nginx

# sudo certbot renew --dry-run

sudo ufw allow https

sudo systemctl restart nginx

总结

在部署的过程中,其实遇到最多的问题就是关于静态资源无法问题的问题,但是看到网上很多文章,都不一样,并且有的写的还是错误的。所以这里就总结一些。还好,一切顺利。算是填了 4 年前的一个坑吧。

最后,打个广告,欢迎访问我的个人网站:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网