글 개요
기존에 EB로 배포된 웹에 SSH로 접속하여 Nginx 설정을 수정하고 SSL 인증서를 수동으로 적용했던 과정을 자동화된 방식으로 전환하였습니다. AWS Elastic Beanstalk에서 SSL 인증서를 자동으로 설정하는 방법과 그 과정에서의 시행착오, 해결 방법을 정리했습니다.
기존 방식
0. 다운로드 받은 pem 키 권한 부여 (SSH초기 접속시)
% chmod 400 YourPemKeyName.pem
1. SSH 접속
% ssh -i ~/Downloads/YourPemkeyName.pem ec2-user@Your-IP
2. SSL 수동 설정
% sudo nano /etc/nginx/conf.d/ssl.conf
server {
listen 443 ssl;
server_name YourSeverName;
ssl_certificate /etc/letsencrypt/live/YourSeverName/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/YourSeverName/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
access_log /var/log/nginx/access.log main;
}
이 server 블록은 Nginx가 HTTPS(SSL)를 통해 요청을 수신하고 백엔드 서버로 전달(proxy) 하도록 설정하는 구성입니다.
접근 로그를 /var/log/nginx/access.log에 기록합니다.
sudo nano /etc/nginx/conf.d/redirect.conf
server {
listen 80;
server_name YourServerName;
return 301 https://$host$request_uri;
}
3. Nginx 문법 검사, 재시작
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl restart nginx
4. 포트 443이 열려 있고 Nginx(또는 다른 서비스)가 이 포트를 리슨 중인지 확인
sudo netstat -tulpn | grep :443
👀 Elastic Beanstalk는 배포할 때 EC2 인스턴스를 처음부터 다시 생성하거나 덮어쓰기 때문에 /etc/nginx/conf.d/ssl.conf 파일을 수동으로 만들고 저장해도 다음 배포 때 사라집니다.
그래서 .platform/hooks/postdeploy/ 스크립트를 사용하여 배포했습니다.
이 스크립트는 배포가 끝난 뒤에 SSL 설정 파일을 /etc/nginx/conf.d/로 다시 복사하고 nginx를 재시작하게 해주는 역할입니다.
개선 방식
0. 파일 구조
/project-backend-root
├── .platform
│ ├── hooks
│ │ └── postdeploy
│ │ └── 01_reload_nginx.sh
│ └── nginx
│ └── conf.d
│ ├── ssl.conf
│ └── redirect.conf
1. 01_reload_nginx.sh 스크립트
#!/bin/bash
cp .platform/nginx/conf.d/*.conf /etc/nginx/conf.d/
nginx -t && systemctl reload nginx
🔍 각 줄 설명
#!/bin/bash
//이 스크립트가 bash 셸에서 실행되어야 한다는 것을 명시합니다 (리눅스 스크립트의 표준 시작 줄)
cp .platform/nginx/conf.d/*.conf /etc/nginx/conf.d/
• .platform/nginx/conf.d/ 디렉터리에 있는 .conf 파일들을 Nginx의 설정 디렉터리인 /etc/nginx/conf.d/로 복사합니다.
• 즉, ssl.conf, redirect.conf 같은 Nginx 설정 파일들을 실제 서버 설정 위치에 반영하는 과정입니다.
nginx -t && systemctl reload nginx
• nginx -t: Nginx 설정 파일이 문법적으로 올바른지 테스트합니다.
• &&: 앞의 명령어가 성공했을 때만 뒤 명령을 실행합니다.
• systemctl reload nginx: 설정이 문제 없으면 Nginx를 재시작하지 않고 리로드해서 설정을 적용합니다 (서비스 중단 없음).
2. ssl.conf, redirect.conf 파일 안에 기존과 동일하게 sever 내용을 작성해주면 됩니다.
3. 해당 스크립트 파일에 실행권한을 부여
chmod +x .platform/hooks/postdeploy/01_reload_nginx.sh
chmod +x .platform/hooks/postdeploy/01_reload_nginx.sh는 Elastic Beanstalk 인스턴스 내에서 이 스크립트를 실행 가능하도록 권한을 부여하는 명령어. 로컬에서 압축 전 한번만 실행해도 됩니다.
이제 수동으로 SSH 접속하여 설정하지 않아도 돼서 훨씬 편하네요.. 😉👍
더 좋은 의견이나 다른 견해가 있으시면 댓글 남겨주세요. 감사합니다!
'Server > AWS' 카테고리의 다른 글
| EC2 서버에서 SSH 접속 및 Let's Encrypt SSL 인증서 설치, 적용 (0) | 2025.03.31 |
|---|---|
| aws 배포 오류 <bad gateway 502> (0) | 2025.03.10 |
| 이미지 크기로 인한 업로드 실패 문제 <413 Request Entity Too Large> (0) | 2025.03.10 |