SW/리눅스

Ubuntu 18.04 : Let's Encrypt로 아파치를 보호하는 방법, 예제, 명령어

얇은생각 2020. 12. 10. 07:30
반응형

Let's Encryption은 ISRG(Internet Security Research Group)에서 만든 인증 기관입니다. 수동 인증서 생성, 유효성 검사, 설치 및 갱신을 제거하도록 설계된 완전히 자동화된 프로세스를 통해 무료 SSL 인증서를 제공합니다. 

Let's Encrypt에서 발급한 인증서는 현재 모든 주요 브라우저에서 신뢰됩니다. 

이 튜토리얼에서는 Ubuntu 18.04의 certbot 도구를 사용하여 Let's Encryption으로 Apache를 보호하는 방법에 대한 단계별 지침을 제공합니다.

 

 

Ubuntu 18.04 : Let's Encrypt로 아파치를 보호하는 방법, 예제, 명령어

 

 

필수 구성 요소

이 튜토리얼을 계속하기 전에 다음 필수 구성 요소를 충족했는지 확인합니다.

- 도메인 이름이 공용 서버 IP를 가리킵니다. 우리는 example.com을 사용할 것입니다.

- 도메인의 Apache 가상 호스트와 함께 Apache가 설치되어 있습니다.

 

 

 

Certbot 설치

Certbot은 Let's SSL 인증서 획득 및 갱신과 웹 서버 구성을 위한 작업을 자동화할 수 있는 완전한 기능을 갖춘 사용하기 쉬운 도구입니다. certbot 패키지는 기본 Ubuntu 리포지토리에 포함되어 있습니다.

패키지 목록을 업데이트하고 certbot 패키지를 설치합니다.

sudo apt update
sudo apt install certbot

 

 

 

Strong Dh(Diffie-Hellman) 그룹 생성 

DH(Diffie-Hellman 키 교환)는 보안되지 않은 통신 채널을 통해 암호화 키를 안전하게 교환하는 방법입니다. 보안을 강화하기 위해 새로운 2048비트 DH 매개 변수를 생성할 예정입니다.

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

 

원하는 경우 크기를 최대 4096비트까지 변경할 수 있지만, 이 경우 시스템 엔트로피에 따라 생성 시간이 30분 이상 걸릴 수 있습니다.

 

 

 

SSL 암호화 인증서 가져오기

도메인에 대한 SSL 인증서를 가져오려면 ${webroot-path}/.well-known/acme-challenge 디렉토리에 요청된 도메인의 유효성을 검사하기 위한 임시 파일을 생성하여 작동하는 Webroot 플러그인을 사용합니다. Let's Encryption 서버는 임시 파일에 HTTP 요청을 하여 요청한 도메인이 certbot이 실행되는 서버로 결정되는지 확인합니다. 

보다 간단하게 하기 위해 .well-known/acme-challerge에 대한 모든 HTTP 요청을 단일 디렉토리 /var/lib/lets 암호화로 매핑합니다.

다음 명령은 디렉토리를 생성하고 Apache 서버에 대해 쓰기 가능으로 설정합니다.

sudo mkdir -p /var/lib/letsencrypt/.well-known
sudo chgrp www-data /var/lib/letsencrypt
sudo chmod g+s /var/lib/letsencrypt

 

코드가 중복되지 않도록 다음 두 가지 구성 스니펫을 만듭니다.

# /etc/apache2/conf-available/letsencrypt.conf

Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/"
<Directory "/var/lib/letsencrypt/">
    AllowOverride None
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
</Directory>

 

# /etc/apache2/conf-available/ssl-params.conf

SSLProtocol             all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite          ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
SSLHonorCipherOrder     off
SSLSessionTickets       off

SSLUseStapling On
SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"

Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Header always set X-Frame-Options SAMEORIGIN
Header always set X-Content-Type-Options nosniff

SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"

 

위의 조각은 Mozilla에서 권장하는 지퍼를 사용하고 있으며 OCSP Stapling, HTTP Strict Transport Security(HSTS)를 사용하도록 설정하고 보안 중심 HTTP 헤더를 거의 적용하지 않습니다. 

구성 파일을 활성화하기 전에 mod_ssl 및 mod_header를 모두 활성화해야 합니다.

sudo a2enmod ssl
sudo a2enmod headers

 

그런 다음 다음 다음 명령을 실행하여 SSL 구성 파일을 사용하도록 설정합니다.

sudo a2enconf letsencrypt
sudo a2enconf ssl-params

 

HTTP/2 모듈을 활성화하면 사이트를 더 빠르고 강력하게 만들 수 있습니다.

sudo a2enmod http2

 

변경 내용을 적용하려면 Apache 구성을 다시 로드합니다.

sudo systemctl reload apache2

 

이제 Webroot 플러그인으로 Certbot 도구를 실행하고 다음을 입력하여 SSL 인증서 파일을 가져올 수 있습니다.

sudo certbot certonly --agree-tos --email admin@example.com --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com

 

SSL 인증서를 성공적으로 획득한 경우 certbot은 다음 메시지를 인쇄합니다.

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2018-10-28. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 

이제 인증서 파일이 생겼으므로 다음과 같이 도메인 가상 호스트 구성을 편집합니다.

# /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80> 
  ServerName example.com
  ServerAlias www.example.com

  Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName example.com
  ServerAlias www.example.com

  Protocols h2 http/1.1

  <If "%{HTTP_HOST} == 'www.example.com'">
    Redirect permanent / https://example.com/
  </If>

  DocumentRoot /var/www/example.com/public_html
  ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
  CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

  # Other Apache Configuration

</VirtualHost>

  

위 구성에서는 HTTPS를 강제 적용하고 www에서 nonww 버전으로 리디렉션합니다. 필요에 따라 구성을 자유롭게 조정할 수 있습니다.

변경 내용을 적용하려면 Apache 서비스를 다시 로드합니다.

sudo systemctl reload apache2

 

이제 https://를 사용하여 웹 사이트를 열 수 있으며 녹색 잠금 아이콘이 표시됩니다.

SSL Labs Server Test(SSL Labs 서버 테스트)를 사용하여 도메인을 테스트하면 아래와 같이 A+ 등급을 받게 됩니다.

 

Ubuntu 18.04 : Let's Encrypt로 아파치를 보호하는 방법, 예제, 명령어 2

 

 

 

SSL 인증서를 자동 갱신

암호화 인증서는 90일 동안 유효합니다. 인증서 만료 전에 자동으로 인증서를 갱신하려면 인증서봇 패키지는 하루에 두 번 실행되는 크론 작업을 생성하고 만료 30일 전에 인증서를 자동으로 갱신합니다.

인증서가 갱신되면 Apache 서비스도 다시 로드해야 합니다. 다음과 같이 보이도록 "systemctl reload apache2"를 /etc/cron.d/certbot 파일에 추가합니다.

# /etc/cron.d/certbot

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && 

 

갱신 프로세스를 테스트하기 위해 certbot --dry-run 스위치를 사용할 수 있습니다.

sudo certbot renew --dry-run

 

오류가 없으면 갱신 프로세스가 성공했음을 의미합니다.

반응형