Nginx에서 Certbot을 사용하여 Let’s Encrypt SSL 인증서를 설정하는 방법

1. Certbot 및 플러그인 설치

먼저, Certbot과 Nginx 플러그인을 설치해야 합니다.

Ubuntu/Debian

sudo apt update
sudo apt install certbot python3-certbot-nginx -y

CentOS/RHEL

sudo yum install epel-release -y
sudo yum install certbot python3-certbot-nginx -y

2. Nginx 구성 확인

Let’s Encrypt가 도메인 소유권을 확인하려면, Nginx에서 해당 도메인을 처리할 수 있어야 합니다.

기본적인 Nginx 설정 예시

/etc/nginx/sites-enabled/example.conf 또는 /etc/nginx/conf.d/example.com.conf에 아래 설정을 추가:

server {
listen 80;
server_name example.com www.example.com;

location /.well-known/acme-challenge/ {
root /var/www/html;
}

location / {
return 301 https://$host$request_uri;
}
}

이후 설정을 적용:

sudo nginx -t  # 설정 검증
sudo systemctl reload nginx # Nginx 재시작

3. Let’s Encrypt SSL 인증서 발급

Nginx 플러그인을 사용하여 SSL 인증서를 자동으로 발급 및 설정할 수 있습니다.

sudo certbot --nginx -d example.com -d www.example.com
  • -d example.com -d www.example.com: 인증서를 적용할 도메인 지정
  • 실행 후 이메일 입력 및 이용 약관 동의가 필요함.

4. 인증서 자동 갱신 설정

Let’s Encrypt 인증서는 90일마다 갱신해야 하므로 자동 갱신을 설정합니다.

갱신 테스트:

sudo certbot renew --dry-run

자동 갱신을 위해 Cron 작업 추가 (보통 자동 등록됨):

sudo crontab -e

아래 줄 추가:

0 3 * * * certbot renew --quiet && systemctl reload nginx

이 설정은 매일 오전 3시에 인증서를 갱신하고, Nginx를 재시작합니다.


5. 발급된 인증서 확인

sudo certbot certificates

SSL 인증서가 /etc/letsencrypt/live/example.com/ 경로에 저장됩니다.


6. SSL 설정 적용

Nginx 설정을 변경하여 HTTPS를 적용합니다.

server {
listen 443 ssl;
server_name example.com www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

location / {
root /var/www/html;
index index.html;
}
}

server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}

Nginx 재시작:

sudo nginx -t  # 설정 검증
sudo systemctl restart nginx # 적용

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다