1. Gabia에서 도메인 구매
2. AWS Route 53(도메인을 AWS의 Route 53에서 호스팅)
2-1 호스팅 영역 생성
구매한 도메인에 맞게 호스팅 영역 생성
2-2 레코드
- 기본 생성된 레코드 확인
- NS(Name Server)의 값/트래픽 라우팅 대상을 Gabia에 등록
→ 1차 ~ 4차에 NS에 해당하는 값/트래픽 라우팅 대상의 4개의 값 넣기(뒤에 . 제외)
- EC2 주소에 맞게 유형 A 레코드 생성
3. SSL 인증 기본 세팅
3-1 디렉터리 구조
/home/ec2-user/app
├── docker-compose-dev.yml
├── certbot/
│ ├── conf/ # Certbot 인증서 파일
│ └── www/ # Certbot이 사용할 웹 루트
├── nginx/conf
│ ├── nginx.conf # Nginx 기본 설정 파일
│ └── conf/
│ └── mody.server.${domain name}.conf # Nginx 서브도메인 SSL 설정
# 새로 만든 폴더, 파일에 쓰기 권한 주기
# 그래야지 Certbot 컨테이너가 실행 중 인증서를 저장하거나 파일을 수정할 수 있음
3-2 디렉토리 권한 변경
certbot/conf
와 certbot/www
디렉토의 소유자와 권한을 Certbot 컨테이너가 접근 가능하도록 수정
# 현재 사용자를 디렉토리 소유자로 변경
sudo chown -R $USER:$USER ./certbot/conf ./certbot/www ./nginx/conf
# 디렉토리에 쓰기 권한 추가
sudo chmod -R 755 ./certbot/conf ./certbot/www ./nginx/conf
권한 확인
ls -ld ./certbot/conf ./certbot/www ./nginx/conf ./nginx/conf/nginx.conf ./nginx/conf/portainer.mody.server.${domain name}.conf
출력
drwxr-xr-x 3 ec2-user ec2-user 4096 Jan 23 10:01 ./certbot/conf
drwxr-xr-x 2 ec2-user ec2-user 4096 Jan 23 09:22 ./certbot/www
3-3 Docker Compose 파일 작성
docker-compose.yml
파일을 생성하여 아래 내용을 작성:
services: # 컨테이너 설정
backend:
container_name: mody-server-dev
image: mody-server:dev #ecr에 올린 이미지로 수정 필요
environment:
- SPRING_PROFILES_ACTIVE=dev
ports:
- 8000:8000
nginx: # Nginx 컨테이너 설정 (certbot은 인증서 발급 시에만 포트를 사용하게 설정)
image: nginx
container_name: nginx
restart: unless-stopped
ports:
- "880:80"
- "8443:443"
volumes:
- ./certbot/conf:/etc/letsencrypt # 파일 설정을 nginx에 마운트 시킴
- ./certbot/www:/var/www/certbot
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf/portainer.mody.server.${domain name}.conf:/etc/nginx/conf.d/portainer.mody.server.${domain name}.conf
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
image: certbot/certbot
container_name: certbot
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; certbot certonly --manual --preferred-challenges dns --email ${email} -d ${domain name} -d *.${domain name} --agree-tos --no-eff-email; exit;'"
4. Nginx 설정
4-1 nginx.conf 파일 작성
기본 Nginx 설정 파일 /nginx/nginx.conf
에 아래 내용을 작성
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf; // portainer.mody.server.${domain name}.conf 불러오기
}
4-2 서브 도메인 설정 파일 작성
서브 도메인 설정 파일 portainer.mody.server.${domain name}.conf
을 /nginx/conf/
경로에 생성
server {
listen 80;
server_name mody.server.${domain name};
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name mody.server.${domain name};
# ssl 인증서 설정
ssl_certificate /etc/letsencrypt/live/${domain name}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/${domain name}/privkey.pem;
# 서브도메인 요청을 8000번 포트로 프록시
location / {
proxy_pass http://mody-server-dev:8000; #컨테이너 참조
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
5. SSL 인증서 발급 (Certbot)
5-1 Certbot 실행 스크립트 생성
→ 다음 2가지 방법 중 하나 실행
- 서버에서 직접 컨테이너 실행
docker-compose -f /home/ec2-user/app/docker-compose-dev.yml run --rm --entrypoint "\
certbot certonly \
-d ${domain name} \
-d *.${domain name} \
--email ${email} \
--manual --preferred-challenges dns \
--server https://acme-v02.api.letsencrypt.org/directory \
--force-renewal" certbot
- Certbot 컨테이너에 들어가서 Certbot 명령어 실행
실행 중인 Certbot 컨테이너에 접속하고, 내부에서 Certbot 명령어를 실행하여 직접 테스트합니다:
certbot certonly \
--manual \
--preferred-challenges dns \
--email ${email} \
-d ${domain name} \
-d *.${domain name} \
--agree-tos \
--no-eff-email \
--debug
5-2 DNS TXT 레코드 설정
Certbot의 요청에 따라 DNS TXT 레코드 추가
Certbot 실행 시 출력되는 _acme-challenge 값을 DNS에 추가:
- DNS 관리 콘솔로 이동.
- TXT 레코드 추가:
- 유형: TXT
- 이름:
_acme-challenge
- 값: Certbot이 제공한 값. (cert.sh 실행하면 나옴)
_acme-challenge.${domain name} = [Certbot 제공 값]
- AWS Route 53 호스팅 영역 레코드에 추가
- 최종 레코드
DNS 설정 적용 후 Certbot 실행 완료:
- Certbot이 "Press Enter to Continue" 메시지를 표시하면, Enter를 눌러 인증서를 발급.
인증서 파일 확인:
인증서가
/home/ec2-user/app/certbot/conf/live/mody.server.${domain name}/
경로에 생성되었는지 확인:ls -l ./certbot/conf/live/${domain name}/
6. Nginx 재시작
6-1 Docker Compose로 Nginx 재시작
docker-compose -f docker-compose-dev.yml up -d nginx
6-2 Nginx 상태 확인
docker logs nginx
'mody' 카테고리의 다른 글
스프링에서 핀터레스트 이미지 크롤링하기 (0) | 2025.03.09 |
---|---|
S3 파일 업로드 CORS 해결 (0) | 2025.03.09 |
스프링에서 S3 이미지 삭제 deleteObject() 403 Access Denied - AWSCompromisedKeyQuarantineV3 정책 (0) | 2025.03.09 |
Spring Boot에서 OpenAI API 사용에 프롬프트 최적화하기 (0) | 2025.03.09 |
mody - 당신의 AI 스타일 친구 (0) | 2025.03.09 |