鲜花网站建设目的,珠宝网站策划书,WordPress主题中文主题,个人社保网上服务平台文章目录 1 前端2 后端2.1 修改 settings.py 文件关于静态文件2.2 关于用户上传的文件图片 3 Nginx4 镜像制作4.1 nginx4.3 Django镜像4.3.1 构建 5 docker-compose 文件内容 1 前端
进入前端项目的根目录#xff0c;运行如下命令进行构建
npm run build构建完成后#xff… 文章目录 1 前端2 后端2.1 修改 settings.py 文件关于静态文件2.2 关于用户上传的文件图片 3 Nginx4 镜像制作4.1 nginx4.3 Django镜像4.3.1 构建 5 docker-compose 文件内容 1 前端
进入前端项目的根目录运行如下命令进行构建
npm run build构建完成后项目根目录下会出现打包后的目录 dist 这个 dist 目录需要给到 nginx 具体配置见第 3 章节的 Nginx
2 后端
2.1 修改 settings.py 文件
关于静态文件
说明读写分离项目 Django中是没有静态文件的这里的静态文件是 Djngo 中集成的后台管理的 admin 和 api 文档应用使用的静态文件。
DEBUG False # 修改为 FalseALLOWED_HOSTS [*] # 允许任何主机访问# 告诉项目访问所有静态文件的 URL 前缀
# 也就是都需要从 /statics/ 开头这个需要和 nginx 中 location 后面的值一致
STATIC_URL /statics/# 静态文件收录的位置生产部署时使用配置完成后
# 执行如下命令完成静态文件的收录, django 会把所有应用使用到的静态文件拷贝到这个目录里一份
# python manage.py collectstatic
# 注意不包含用户通过页面上传的文件具体见面的配置说明
STATIC_ROOT Path.joinpath(BASE_DIR, statics)
2.2 关于用户上传的文件图片
这个是临时使用做好自己编写视图实现。 修改 setings.py 文件
# 用户通过页面上传的文件使用这个 URL 访问,这个需要在nginx上配置为 location 的值
MEDIA_URL /media/# 存储文件的路径
MEDIA_ROOT Path.joinpath(BASE_DIR, media)3 Nginx
说明 配置文件中 sharkplat 是 程序的后端主机名。
server {listen 80;listen [::]:80;server_name localhost;#access_log /var/log/nginx/host.access.log main;# 这个就是前端了location / {root /usr/share/nginx/html;try_files $uri $uri/ /index.html last;index index.html index.htm;}# 这里就是后端用到的静态文件注意是后端应用admin和 DRF 自带的 api 文档的静态文件。location /statics/{root /usr/share/nginx;}# 这里就是用户通过页面上传的图片location /media {root /usr/share/nginx;}# 这个是后台管理location /admin {proxy_pass http://sharkplat/admin;}# 这里是后端数据接口location /v1 {proxy_pass_header Server;proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Scheme $scheme;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection upgrade;proxy_pass http://sharkplat/v1;}error_page 500 502 503 504 /50x.html;location /50x.html {root /usr/share/nginx/html;}
}
4 镜像制作
4.1 nginx
镜像使用的是 nginx:1.20.2-alpine
4.3 Django镜像
4.3.1 构建
Dockerfile
FROM alpine:latest
COPY requirements.txt /root/requirements.txt
RUN sed -i s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g /etc/apk/repositories \apk add --update --no-cache \gcc mysql-dev musl-dev curl \jq py3-configobj py3-pip \py3-setuptools python3 python3-dev \mkdir /root/.pip \echo [global] /root/.pip/pip.conf \echo index-urlhttps://mirrors.aliyun.com/pypi/simple /root/.pip/pip.conf \pip install --upgrade pip \pip install -r /root/requirements.txt
requirements.txt 在 DRF 项目根目录环境下执行如下命令获取. pip3 freeze requirements.txt
asgiref3.7.2
asttokens2.2.1
attrs23.1.0
backcall0.2.0
certifi2023.7.22
charset-normalizer3.2.0
coreapi2.3.3
coreschema0.0.4
decorator5.1.1
Django4.2
django-filter23.2
django-guardian2.4.0
django-phonenumber-field7.1.0
django-simpleui2023.8.28
djangorestframework3.14.0
djangorestframework-simplejwt5.3.1
drf-spectacular0.26.5
executing1.2.0
idna3.4
importlib-metadata6.8.0
inflection0.5.1
ipython8.14.0
itypes1.2.0
jedi0.19.0
Jinja23.1.2
jsonschema4.20.0
jsonschema-specifications2023.11.2
Markdown3.4.4
MarkupSafe2.1.3
matplotlib-inline0.1.6
mysqlclient2.2.0
parso0.8.3
pexpect4.8.0
phonenumberslite8.13.17
pickleshare0.7.5
Pillow10.0.0
prompt-toolkit3.0.39
ptyprocess0.7.0
pure-eval0.2.2
Pygments2.15.1
PyJWT1.7.1
pytz2023.3
PyYAML6.0.1
referencing0.31.1
requests2.31.0
rpds-py0.13.2
six1.16.0
sqlparse0.4.4
stack-data0.6.2
traitlets5.9.0
typing_extensions4.7.1
uritemplate4.1.1
urllib32.0.4
wcwidth0.2.6
zipp3.16.2
click8.1.7
gunicorn21.2.0
h110.14.0
uvicorn0.25.05 docker-compose 文件内容
version: 3.9
services:web:image: nginx:1.20.2-sharkvolumes:- ./dist:/usr/share/nginx/html- ./conf.d:/etc/nginx/conf.d- ./statics:/usr/share/nginx/statics- ./sharkplat/media:/usr/share/nginx/mediarestart: alwaysports:- 9900:80sharkplat:image: sharkplat:1.0command: sh /sharkplat/start.shrestart: alwaysports:- 8010:80volumes:- ./sharkplat:/sharkplat