[TIL_Carrotww] 80 - 22/12/26

μœ ν˜•μ„Β·2022λ…„ 12μ›” 28일
0

TIL

λͺ©λ‘ 보기
95/138
post-thumbnail

πŸ“Carrotww의 μ½”λ”© 기둝μž₯

🧲 Daphne μ»¨ν…Œμ΄λ„ˆ ν•˜λ‚˜λ‘œ 배포

πŸ” λ“œλ””μ–΄ ν•΄λƒˆλ‹€ μ§„μ§œ γ… γ…  λ„ˆλ¬΄ νž˜λ“€μ—ˆμ§€λ§Œ uvicorn으둜 λ°°ν¬ν•˜μ—¬ ν”„λ‘œμ„ΈμŠ€λ₯Ό ν•˜λ‚˜λ§Œ μ“°λŠ”κ²Œ λ„ˆλ¬΄ λ©‹ 없어보이고 λ§ˆμŒμ— μ•ˆλ“€μ–΄ 계속 μ‹œλ„ν•˜μ—¬ μ„±κ³΅ν–ˆλ‹€.
계속 μ‹œλ„ν•˜λ©΄μ„œ nginx docker μ„€μ •λ“€κ³Ό μž‘λ™ 방식에 λŒ€ν•΄ 많이 μ•Œκ²Œλ˜μ—ˆκ³  μžμ‹ κ°λ„ λΆ™κ³  λ„ˆλ¬΄ ν–‰λ³΅ν•˜λ‹€ γ…Žγ…Ž

docker compose

version: '3.8'

volumes:
  postgres: {}
  django_media: {}
  django_static: {}

services:
  postgres:
    container_name: postgres
    image: postgres:14.5
    volumes:
      - postgres:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=P@ssw0rd
      - POSTGRES_DB=django
    restart: always
    networks:
      carrot:
        ipv4_address: 192.168.16.2

  redis:
    image: redis
    ports:
      - "6379:6379"
    networks:
      carrot:
        ipv4_address: 192.168.16.3

  backend:
    container_name: backend
    build: ./backend/
    command: sh -c "python manage.py collectstatic --no-input && python manage.py migrate && daphne -b 0.0.0.0 -p 8000 Togeduck.asgi:application"
    volumes:
      - ./backend/django/:/app/
      - /etc/localtime:/etc/localtime:ro
      - django_media:/app/media/
      - django_static:/app/static/
    environment: # djangoμ—μ„œ μ‚¬μš©ν•  섀정듀을 μ§€μ •ν•΄μ€λ‹ˆλ‹€.
      - DEBUG=1
      - POSTGRES_DB=django
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=P@ssw0rd
      - POSTGRES_HOST=postgres
      - POSTGRES_PORT=5432
    depends_on:
      - postgres
      - redis
    links:
      - redis
    restart: always
    hostname: backend
    ports:
      - 8000:8000
    networks:
      carrot:
        ipv4_address: 192.168.16.4

  nginx:
    container_name : nginx
    image: nginx:1.23.2
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - django_media:/media/
      - django_static:/static/
    depends_on:
      - backend
    restart: always
    networks:
      carrot:
        ipv4_address: 192.168.16.6

networks:
  carrot:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 192.168.16.0/16

nginx

upstream backend {
    server backend:8000;
    # backend 8000 으둜 μ²˜μŒμ— μ•„μ˜ˆ λ°”κΎΈμ–΄μ€Œ
}
server {
  listen 80;
  server_name www.carrotww.shop; # λ„λ©”μΈμœΌλ‘œ λ“€μ–΄μ˜€λŠ” μš”μ²­μ„ 처리
  location / {
        proxy_pass http://backend;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
	client_max_body_size 0;
  }

  location /static/ {
    alias /static/;
  }

  location /media/ {
    alias /media/;
  }

  location /ws {
	proxy_http_version 1.1;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "upgrade";
	proxy_pass http://backend;
  }
}
server {
  listen 80;
  server_name carrotww.shop; # wwwκ°€ μ—†λŠ” url둜 μš”μ²­ ν–ˆμ„ λ•Œ
  return 301 http://www.carrotww.shop$request_uri; # wwwλ₯Ό 뢙인 url둜 redirection
}

Docker file

# python 3.10.8버전 이미지λ₯Ό μ‚¬μš©ν•΄ λΉŒλ“œ
FROM python:3.8.10

# .pyc νŒŒμΌμ„ μƒμ„±ν•˜μ§€ μ•Šλ„λ‘ μ„€μ •
ENV PYTHONDONTWRITEBYTECODE 1

# 파이썬 λ‘œκ·Έκ°€ 버퍼링 없이 μ¦‰κ°μ μœΌλ‘œ 좜λ ₯ν•˜λ„λ‘ μ„€μ •
ENV PYTHONUNBUFFERED 1

# /app/ 디렉토리λ₯Ό 생성
RUN mkdir /app/

# /app/ 경둜λ₯Ό μž‘μ—… λ””λ ‰ν† λ¦¬λ‘œ μ„€μ •
WORKDIR /app/

# requirments.txtλ₯Ό μž‘μ—… 디렉토리(/app/) 경둜둜 볡사
COPY ./django/requirements.txt .

RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

# for django-crontab
RUN chmod -R 777 /app #κΆŒν•œ μ„€μ •
RUN apt-get update
RUN apt-get install -y cron && touch /score.log #crontab μ„€μΉ˜ 및 둜그파일 생성

# daphne μ„€μΉ˜
RUN pip install daphne psycopg2

🧲 ν›„κΈ°

πŸ” daphne에 λŒ€ν•œ 정보가 많이 λΆ€μ‘±ν•˜μ—¬ 이것저것 λ‹€ ν•΄λ³΄λ©΄μ„œ μ‹œλ„ν•œ κ²°κ³Όκ°€ μ„±κ³΅μ΄μ—¬μ„œ 닀행이닀. λ˜ν•œ νŠœν„°λ‹˜μ΄ μ•ˆλœλ‹€κ³  ν–ˆλ˜κ±Έ λ‚΄κ°€ ν•΄λ‚΄λ‹ˆκΉŒ 기뢄이 λ„ˆλ¬΄ μ’‹λ‹€ γ…Žγ…Žγ…Ž
ν”„λ‘œμ νŠΈμ— λ“€μ–΄μ˜¨ ν”Όλ“œλ°±μ΄λ‚˜ κΈ°λŠ₯ μˆ˜μ •μ„ ν•˜λ©° ν‹ˆν‹ˆνžˆ 미련을 λͺ»λ²„리고 λͺ‡λ²ˆμ”© μΌμ£ΌμΌλ™μ•ˆ μ‹œλ„ν•΄λ³΄κ³  μ„±κ³΅ν•΄μ„œ λ”μš± κΈ°μœκ²ƒ κ°™λ‹€.
이제 λ°°ν¬λŠ” μ§„μ§œ 끄읏...

profile
Carrot_hyeong

0개의 λŒ“κΈ€