국비학원에서 진행한 최종 프로젝트를 AWS EC2에 배포하면서 Docker(Nginx, Certbot) SSL 인증서를 적용했던 내용을 정리해보자.
필자는 호스팅케이알 에서 도메인을 구입했다.
인프런 - 클라우드 서비스 AWS (생활코딩) 을 참고하여 Ubuntu로 인스턴스를 시작한다. AWS의 탄력적 IP 주소로 고정 IP가 되도록 설정한다. 그리고 호스팅케이알에서 DNS 레코드 설정을 통해 AWS EC2와 연결한다.
Install Docker Engine on Ubuntu 를 참고하여 Ubuntu에 도커를 설치한다.
conf.d를 수정하고 nginx 컨테이너를 시작한다.
docker-compose.yaml
services:
nginx:
container_name: nginx
image: nginx
depends_on:
- application
ports:
- 80:80
- 443:443
volumes:
- nginx:/etc/nginx
- certbot:/etc/letsencrypt
- webroot:/var/www/html
volumes:
nginx:
certbot:
webroot:
conf.d
server {
listen 80;
location /.well-known/acme-challenge/ {
allow all;
root /var/www/html;
}
}
nginx 컨테이너가 실행되어있는 상태에서 certbot-certonly 컨테이너를 실행한다. SSL 인증서를 갱신해야 하면 certbot-renew 컨테이너를 실행한다.
docker-compose.yaml
services:
# SSL 인증서 발급
certbot-certonly:
container_name: certbot-certonly
image: certbot/certbot
volumes:
- certbot:/etc/letsencrypt
- webroot:/var/www/html
command: certonly --webroot --webroot-path=/var/www/html --non-interactive --agree-tos -m youremail@gmail.com -d www.yourdomain.com
# SSL 인증서 갱신
certbot-renew:
container_name: certbot-renew
image: certbot/certbot
volumes:
- certbot:/etc/letsencrypt
- webroot:/var/www/html
command: certonly renew
volumes:
certbot:
webroot: