欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用docker+django+nginx+uwsgi部署前后端分离项目

程序员文章站 2022-03-04 13:34:39
...

使用docker nginx uwsgi 部署前后端分离的django项目脚手架

创建或使用已有django项目

django startproject app

app 可以设置为你的项目名称 ,以下项目名称均已app为例

django配置

app/app/settings.py

基础镜像默认采用东八区时区

ALLOWED_HOSTS = ["*"] # 允许主机

LANGUAGE_CODE = 'zh-hans' # 语言

TIME_ZONE = 'Asia/Shanghai' # 时区

STATIC_ROOT = os.path.join(BASE_DIR, 'static') # 静态资源路径

uwsgi配置

config/uwsgi.ini

[uwsgi]
socket = 127.0.0.1:8001
; chdir:change app to yourproject name
chdir = /home/django/app/ 
; module:change app to yourproject name
module = app.wsgi:application
; module:change app to yourproject name
master = True
; pidfile:change app to yourproject name
pidfile = /home/django/app/uwsgi.pid
vacuum = True
max-requests = 5000
; daemonize:change app to yourproject name
daemonize = /home/django/app/uwsgi.log
env = LANG=en_US.UTF-8

nginx配置

config/nginx-app.conf

设置nginx 静态资源路径

# nginx-app.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:/home/django/app/app.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on, default_server indicates that this server block
    # is the block to use if no blocks match the server_name
    listen      80 default_server;

    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media 
    # app change to yourproject name
    location /media  {
        alias /home/django/app/media;  # your Django project's media files - amend as required
    }
    
	# app change to yourproject name
    location /static {
        alias /home/django/app/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/django/uwsgi_params; # the uwsgi_params file you installed
    }
}

收集静态资源

django manage collectstatic

上传django项目及配置文件

django 代码目录 app => home/django

配置文件目录 config => home/django

运行镜像

自己构建镜像
docker build -t base-django:0.0.1 . && docker run -p 28000:80 -v /home/django/app:/home/django/app -v /home/django/config:/home/django/config --name base-django base-django:1.0.0

使用构建好的腾讯云镜像
docker run -p 28000:80 -v /home/django/app:/home/django/app -v /home/django/config:/home/django/config --name base-django ccr.ccs.tencentyun.com/selfservice/base-django:1.0.0