[Linux] Ubuntu 웹 서버에 HTTPS 설정

[Linux] Ubuntu 웹 서버에 HTTPS 설정


Ubuntu Nginx Certbot Linux

HTTPS

  • HTTPS는 HTTP의 보안 버전입니다.
  • HTTP는 평문으로 데이터를 송수신하기 때문에, 스니핑의 위험이 있어 보안에 취약합니다.
  • 현재는 SSL(Secure Socket Layer)을 사용하지 않고, TLS(Transport Layer Security)를 사용합니다.
  • 아무튼 HTTP 위에 보안 계층을 추가해 암호화 통신을 하는 프로토콜이 HTTPS입니다.

  • 지금은 인증서를 발급받지 않았기 때문에 HTTP 통신을 하고 있습니다.
  • 이 웹 사이트는 민감한 정보를 주고받지는 않지만, 그냥 하고싶기 때문에 해보겠습니다.

nginx.conf 파일 수정

  • 인증서 발급 시 Nginx 설정 파일의 Server name으로 자동 매칭하기 때문에 해당 필드에 도메인을 명시합니다.
> sudo vi /etc/nginx/nginx.conf
worker_processes 1;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {
  server {
    listen 80;
    server_name baelog.site;
	
    root /usr/share/nginx/html;
    index index.html index.htm;
    include /etc/nginx/mime.types;
	
    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
	
    error_page 404 /404.html;
    location = /404.html {
            root /usr/share/nginx/html;
            internal;
    }
	
    location / {
            try_files $uri $uri/index.html =404;
    }
  }
}

Certbot을 이용해 인증서 발급 받기

Certbot 공식 문서 설치 가이드

  • 공식 문서에서는 Snap 패키지 관리자를 통한 설치를 권장하고 있습니다.

Certbot 스냅은 x86_64, ARMv7 및 ARMv8 아키텍처를 지원합니다. 대부분의 사용자는 스냅을 통해 Certbot을 설치할 것을 강력히 권장하지만 여기에서 대체 설치 지침을 찾을 수 있습니다.

  • Snap 패키지부터 설치합니다.
> sudo apt update
> sudo apt install snapd
  • 혹시 기존에 Certbot이 설치되어 있다면, 제거합니다.
> sudo apt remove certbot
  • Snap으로 Certbot을 설치합니다.
> sudo snap install --classic certbot
  • certbot 명령어를 사용하기 쉽게 심볼릭 링크를 생성합니다.
  • 이 명령을 통해 /usr/bin/certbot 이름의 심볼릭 링크가 생성됩니다.
> sudo ls -s /snap/bin/certbot /usr/bin/certbot
  • 인증서를 설치합니다.
> sudo certbot --nginx
  • 설치 후 Nginx 설정 파일을 열어보면, 인증서가 발급된 것을 볼 수 있습니다.
worker_processes 1;

events {
	worker_connections 1024;
	# multi_accept on;
}

http {
  server {
    server_name   baelog.site;
	
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    include /etc/nginx/mime.types;
	
    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
	
    error_page 404 /404.html;
    location = /404.html {
            root /usr/share/nginx/html;
            internal;
    }
	
    location / {
            try_files $uri $uri/index.html =404;
    }
	
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/baelog.site/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/baelog.site/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    }
	
    server {
    if ($host = baelog.site) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
	
    listen 80;
    server_name   baelog.site;
    return 404; # managed by Certbot
	}
}
  • 인스턴스의 443 포트를 개방합니다.
> sudo iptables -I INPUT 1 -p tcp --dport 443 -j ACCEPT

테스트

  • HTTPS가 적용되었습니다.

인증서 자동 갱신

  • Certbot은 60일마다 인증서를 갱신합니다.
  • Certbot 패키지는 인증서가 만료되기 전에 자동으로 인증서를 갱신하는 cron 작업이나 systemd 타이머를 제공합니다.
  • 인증서 자동 갱신을 테스트합니다.
> sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/baelog.site.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for baelog.site

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/baelog.site/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  • 공식 문서에는 Certbot을 갱신하는 명령이 다음 위치 중 하나에 설치된다고 합니다.

    • /etc/crontab/
    • /etc/cron.*/*
    • systemctl list-timers
  • 저는 여기서 확인할 수 있었습니다.

> systemctl list-timers
NEXT                        LEFT           LAST                        PASSED       UNIT                         ACTIVATES
Fri 2024-05-03 06:05:00 UTC 11min left     Thu 2024-05-02 08:00:49 UTC 21h ago      apt-daily-upgrade.timer      apt-daily-upgrade.service
Fri 2024-05-03 10:12:00 UTC 4h 18min left  n/a                         n/a          snap.certbot.renew.timer     snap.certbot.renew.service
Fri 2024-05-03 11:31:07 UTC 5h 37min left  Fri 2024-05-03 03:45:03 UTC 2h 8min ago  fwupd-refresh.timer          fwupd-refresh.service
Fri 2024-05-03 12:43:08 UTC 6h left        Thu 2024-05-02 12:43:08 UTC 17h ago      systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Fri 2024-05-03 12:49:22 UTC 6h left        Fri 2024-05-03 04:20:52 UTC 1h 33min ago apt-daily.timer              apt-daily.service
Fri 2024-05-03 20:57:28 UTC 15h left       Fri 2024-05-03 01:23:50 UTC 4h 30min ago motd-news.timer              motd-news.service
Sat 2024-05-04 00:00:00 UTC 18h left       Fri 2024-05-03 00:00:08 UTC 5h 53min ago logrotate.timer              logrotate.service
Sat 2024-05-04 00:00:00 UTC 18h left       Fri 2024-05-03 00:00:08 UTC 5h 53min ago man-db.timer                 man-db.service
Sun 2024-05-05 03:10:22 UTC 1 day 21h left Thu 2024-05-02 08:00:49 UTC 21h ago      e2scrub_all.timer            e2scrub_all.service
Mon 2024-05-06 00:00:00 UTC 2 days left    Thu 2024-05-02 08:00:49 UTC 21h ago      fstrim.timer                 fstrim.service

10 timers listed.
Pass --all to see loaded but inactive timers, too.
© 2024 Seungwon Bae 🇰🇷