서론
Docker와 Docker-Compose를 이용하여 Nginx와 Certbot을 구성하는 방법은 웹 애플리케이션의 배포 및 관리를 간소화합니다. 이 방법을 사용하면, SSL/TLS 인증서의 설치와 갱신을 자동화하여, 보다 안전하게 웹 애플리케이션을 운영할 수 있습니다. 본 글에서는 Docker-Compose를 사용하여 Nginx 서버에 Certbot을 설치하고 HTTPS를 활성화하는 과정을 단계별로 설명하겠습니다.
필요 조건
- Docker 및 Docker-Compose가 설치된 시스템
- 유효한 도메인 이름과 해당 도메인의 DNS 설정이 완료된 상태
Docker-Compose 파일 설정
먼저, docker-compose.yml 파일을 생성하고 Nginx와 Certbot 서비스를 정의합니다. 아래는 간단한 예제입니다:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
depends_on:
- certbot
certbot:
image: certbot/certbot
container_name: certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
Nginx 설정 파일(default.conf)을 ./data/nginx 디렉토리에 생성합니다. 이 파일은 HTTP 요청을 HTTPS로 리다이렉트하고, HTTPS 요청을 처리하는 설정을 포함합니다.
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
return 301 https://$host$request_uri;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf; # Managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # Managed by Certbot
location / {
root /usr/share/nginx/html;
index index.html;
}
}
인증서 발급
Certbot 컨테이너를 처음 실행할 때, 다음 명령을 사용하여 인증서를 수동으로 발급받습니다:
docker-compose run --rm certbot certbot certonly --webroot --webroot-path=/var/www/certbot --email your-email@example.com --agree-tos --no-eff-email --staging --expand -d yourdomain.com -d www.yourdomain.com
SSL 인증서 갱신 자동화
Docker-Compose 파일에서 Certbot 서비스의 entrypoint는 이미 SSL 인증서의 자동 갱신을 처리하도록 설정되어 있습니다. 이 설정은 12시간마다 인증서 갱신을 시도합니다.
결론
Docker-Compose를 사용하여 Nginx와 Certbot을 설정하는 방법은 웹 애플리케이션의 배포를 간편하게 하고, SSL/TLS 인증서 관리를 자동화합니다. 이 구성을 통해 보안이 강화된 웹 애플리케이션을 쉽게 운영할 수 있습니다.
'IT > Deploy' 카테고리의 다른 글
nginx는 왜 필요할까? nginx.conf 예시를 통해 알아보자 (0) | 2024.04.18 |
---|---|
Django 프로젝트 Docker를 사용하여 배포하기 (0) | 2024.04.16 |
Github Action를 사용한 배포 자동화, CI/CD 파이프라인 구축 (0) | 2024.04.12 |
nginx, root와 alias 차이와 사용법(예시 포함) (0) | 2024.04.12 |
letsencrypt를 통해 nginx에 SSL/TLS인증서 적용하기 (0) | 2024.04.08 |