2021年4月27日 06:06 by wst
服务部署为了对物理机产生的影响最小,这里所有项目的部署全部使用docker实现。
django项目的常规部署需要考虑三部分:
1. django本身的启动;
2. nginx转发;
3. nginx和django之间的转发,这里使用uwsgi;
这里没有采用多docker实例的方法,而是所有服务全部在一个docker中实现。
接下来重点就是怎么编写dockerfile和配置文件;
这里直接贴上作者的方法。
FROM python:3.7.10
#定义时区参数
ENV TZ=Asia/Shanghai
# 设置编码
ENV LC_ALL C.UTF-8
#设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone
# 更换系统下载源
# 设置工作目录
WORKDIR /code
COPY . /code
RUN cat /code/source.txt > /etc/apt/sources.list
# 安装nginx
RUN apt-get update \
&& apt-get -y dist-upgrade \
&& apt-get install -y nginx
# 安装uwsgi
RUN pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple
RUN pip install uwsgi -i https://mirrors.aliyun.com/pypi/simple
# 定义外部参数
ARG build_env
# 调试标记
ENV DEBUG {build_env}
# 设置工作目录
WORKDIR /code
COPY . /code
# 安装依赖
RUN pip install -r /code/requirements.txt -i https://mirrors.aliyun.com/pypi/simple
# 收集静态文件
RUN python manage.py collectstatic --noinput
# nginx configuration
RUN if [ "$build_env" = "true" ] ; \
then cp nginx-config/business-test.conf /etc/nginx/conf.d; \
else cp nginx-config/business.conf /etc/nginx/conf.d; fi
# 启动服务
COPY ./entrypoint.sh /code
RUN mkdir -p /data/logs
RUN chmod +x /code/entrypoint.sh
CMD ["bash", "/code/entrypoint.sh"]
1. /code/source.txt
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
2. /code/requirements.txt
asgiref==3.3.4
bcrypt==3.1.4
cffi==1.14.5
Django==3.1.7
django-filter==2.4.0
djangorestframework==3.12.4
djangorestframework-jwt==1.11.0
djangorestframework-simplejwt==4.4.0
et-xmlfile==1.0.1
mysqlclient==2.0.3
numpy==1.19.5
openpyxl==3.0.7
pandas==1.1.5
pycparser==2.20
PyJWT==1.7.1
PyMySQL==1.0.2
python-dateutil==2.8.1
pytz==2021.1
six==1.15.0
SQLAlchemy==1.3.2
sqlparse==0.4.1
typing-extensions==3.7.4.3
xlrd==1.2.0
3. nginx-config/business-test.conf
server {
listen 80;
server_name admin-test.xxx.com.cn;
root /code;
access_log /data/logs/access.log;
error_log /data/logs/error.log;
location /favicon.ico { access_log off; log_not_found off; }
location /static/ {
#如果你的static目录不在root里,可以配置 alias /path/to/your/mysite/static;
alias /code/static/;
}
location / {
include uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass 127.0.0.1:5556;
}
}
nginx-config/business.conf 内容同上,不同的地方为:第三行的是:server_name admin.xxx.com.cn;
4. entrypoint.sh
#/bin/bash
echo "uwsgi running..."
uwsgi --ini /code/mange.ini
echo "nginx starting..."
service nginx start
nginx -t
nginx -s reload
python -m http.server 1111 --bind 0.0.0.0
5. /code/mange.ini
[uwsgi]
# 项目根目录路径(full path)
chdir = /code
pythonpath = /code
# Django的 wsgi 文件
wsgi-file = /code/business_management_system/wsgi.py
# 自动载入
py-autoreload=1
# 设置日志
daemonize = %(chdir)/run.log
log-maxsize = 5000000
master = true
pidfile=/tmp/business-master.pid
# 最大工作进程数(CPU密集型建议设为CPU核心数,IO密集型建议设为CPU核心数的两倍)
processes = 5
# unix套接字文
socket = 127.0.0.1:5556
# socket文件权限
# 退出时清空环境
vacuum = true
有难度的地方在于这些所有过程在dockerfile中怎么编排。
不过还好,终于搞定,项目顺利上线。
如有问题,欢迎评论!