使用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
推荐阅读
-
从零开始搭建前后端分离的NetCore2.2(EF Core CodeFirst+Autofac)+Vue的项目框架之七使用JWT生成Token(个人见解)
-
从零开始搭建前后端分离的NetCore2.2(EF Core CodeFirst+Autofac)+Vue的项目框架之十一Swagger使用一
-
超简单!asp.net core前后端分离项目使用gitlab-ci持续集成到IIS
-
从壹开始前后端分离 41 || Nginx+Github+PM2 快速部署项目(一)
-
从零开始搭建前后端分离的NetCore2.2(EF Core CodeFirst+Autofac)+Vue的项目框架之六使用过滤器进行全局请求数据验证
-
从零开始搭建前后端分离的NetCore2.2(EF Core CodeFirst+Autofac)+Vue的项目框架之八MemoryCache与redis缓存的使用
-
记一次前后端分离项目的部署过程
-
ubuntu下java前后端分离项目部署
-
部署vue+Springboot前后端分离项目的步骤实现
-
ASP.NET Core 实战:使用 ASP.NET Core Web API 和 Vue.js 搭建前后端分离项目