基于docker部署前后端不分离项目--->docker+Dockerfile+nginx+uwsgi+socket+django+负载均衡

本文详细介绍了如何使用Docker部署Django项目,包括配置uwsgi和Dockerfile,收集静态文件,以及设置nginx作为反向代理。通过这个过程,读者可以学习到将web应用部署到云服务器的基本步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0 介绍

  • 需要一个云服务器(为了外网可以访问),一个码云/GitHub账号,我这边用的码云做测试
  • 安装docker

1. 项目基础配置

1.1 下载项目到opt里面

git clone https://gitee.com/yqmc/u_text.git

1.2 进入项目 查看位置

# 进入项目
cd u_text/

# 查看位置(配置uwsgi.ini用)
pwd
# 输出
/opt/u_text

1.3 按照自己的配置修改uwsgi

1.3.2 vim uwsgi.ini(一定要与manage.py同级)

1.3.1 uwsgi.ini

[uwsgi]
# 用socket无法直接访问页面,必须用nginx做反向代理
socket=0.0.0.0:8080
# 可以直接访问,不安全 用ab直接可以把服务器搞崩
;http = 0.0.0.0:8080
# 项目根目录(你的项目根目录,刚才进入opt pwd查看的路径)
chdir = /opt/u_text
# wsgi文件所在的路径(相对于根目录找到wsgi.py)
wsgi-file = u_text/wsgi.py
# 进程数
processes = 4
# 线程数
threads = 2
# uwsgi服务器的角色
master = True
# 存放进程编号的文件
# pidfile = uwsgi.pid
# 日志输出(坑,解开的话,会后台运行,导致docker无法启动****,最好不要解开)
# daemonize = uwsgi.log

1.4 按照自己的配置修改Dockerfile

1.4.1 vim Dockerfile(与manage.py同级)

1.4.2 Dockerfile

# 基于基础镜像,默认会去宿主机里找,没有会去hub上拉取。在没有,报错
FROM python:3.6
# 制作者
MAINTAINER ymq
# 暴露端口(可以不写) -p 映射,但最好留着
EXPOSE 8080
# 宿主机文件requirement.txt copy到容器内home路径下
ADD ./requirement.txt /home/
# 构建镜像执行执行命令
RUN pip install -r /home/requirement.txt -i https://pypi.douban.com/simple/
RUN pip install uwsgi -i https://pypi.douban.com/simple/
# 用来保存数据,防止容器挂掉,数据丢失 可以不写 -v 映射,但最好留着
VOLUME ["/home"]
# 工作路径,WORKDIR --> cd 
WORKDIR /opt/u_text
# 执行的命令,当容器启动的时候,会自动执行使django以uwsgi启动
CMD ["uwsgi", "--ini", "uwsgi.ini"]
 

1.5 django静态文件的收集

1.5.1 进入settings.py,解开倒数第二行的注释

vim u_text/settings.py

# 解开注释(django静态文件存放的路径)
STATIC_ROOT='/opt/static'

1.5.2 执行命令

python3 manage.py collectstatic

2. 镜像

2.1 没有安装docker,安装一个

docker安装https://blog.csdn.net/qq_52385631/article/details/122927023?spm=1001.2014.3001.5502

2.2 构建镜像

2.2.1 在Dockerfile所在的位置

2.2.2 执行命令

docker build -t='起一个镜像名称' .

2.2.3 案例

docker build -t='u_t' .

3. nginx配置

3.1 容器跑的nginx,主要文件的目录

Nginx配置路径(日志)     /etc/nginx/nginx.conf
Nginx配置路径(server)  /etc/nginx/conf.d/default.conf
错误日志              /var/log/nginx/error.log
访问日志             /var/log/nginx/access.log
默认站点目录        /usr/share/nginx/html
PID目录           /var/run/nginx.pid

容器的nginx的配置文件,进行了解耦合,日志的是nginx.conf,主机的是efault.conf

 3.2 创建文件夹及文件做目录挂载

mkdir -p /opt/nginx/conf  /opt/nginx/logs
touch /opt/nginx/conf/nginx.conf
touch /opt/nginx/conf/default.conf
touch /opt/nginx/logs/error.log
touch /opt/nginx/logs/access.log

3.3 nginx启动文件配置 

3.3.1 nginx.conf(日志)

3.3.1.1 打开nginx.conf

vim /opt/nginx/conf/nginx.conf

3.3.1.2 nginx.conf配置(一般不用修改,复制粘贴即可)

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

3.3.2 default.conf (server主机) 

3.3.2.1 根据自己的云服务器修改监听的IP

vim /opt/nginx/conf/default.conf

3.3.2.1 default.conf(用的时候,最好把注释全部清掉)

# 负载均衡的配置
upstream u_text {
        server 106.14.42.253:8081;
    	#server 106.14.42.253:8082;
    }
server {
    listen 80;
    # 云服务器的IP,这里可以填写域名或IP,填写自己的
    server_name www.0528.ltd;
    # 当监听到当前IP端口80发来请求时,进行转发
    location / {
        uwsgi_pass u_text;
        # uwsgi与nginx通信的模块
        include /etc/nginx/uwsgi_params;
    }
    # 当请求是www.0528.ltd:80/static/xxx时 去/opt/static路径下找静态文件
    location /static {
        # alias 重命名
        alias /opt/static;
    }
}

4. 起容器

4.1 起django容器

4.1.1 格式

docker run -di --name=容器名称 -v /opt/宿主机项目根路径/:/opt/映射的路径/ -p 8081:8080 镜像名称 

4.1.2 案例 

docker run -di --name=t1 -v /opt/u_text/:/opt/u_text -p 8081:8080 u_t

4.1 起nginx容器 

docker run --name=nginx -id -p 80:80 -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf -v /opt/nginx/logs/error.log:/var/log/nginx/error.log -v /opt/nginx/logs/access.log:/var/log/nginx/access.log -v /opt/static:/opt/static nginx

4.2 命令介绍

# 反向代理 正向代理 静态文件代理
docker run --name=nginx 
-id 
-p 80:80 
# nginx日志配置文件映射
-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 
# nginx主机配置文件映射
-v /opt/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf
# nginx错误日志映射
-v /opt/nginx/logs/error.log:/var/log/nginx/error.log 
# nginx访问日志映射
-v /opt/nginx/logs/access.log:/var/log/nginx/access.log
# django静态文件
-v /opt/static:/opt/static nginx

5. ps 原生容器nginx文件配置

5.1 nginx.conf(/etc/nginx/nginx.conf )

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

 5.2 default.conf(/etc/nginx/conf.d/default.conf)

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑台风走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值