SW/리눅스

Debian 9 : Let's Encrypt로 Apache 보안 설정하는 방법, 예제, 명령어

얇은생각 2021. 2. 14. 07:30
반응형

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

 Let's Encrypt에서 발급한 인증서는 발급일로부터 90일 동안 유효하며 현재 모든 주요 브라우저에서 신뢰됩니다.

이 튜토리얼은 데비안 9의 특정봇 도구를 사용하여 무료 Let's Encrypt를 얻는 과정을 안내합니다. 또한 새 SSL 인증서를 사용하고 HTTP/2를 사용하도록 Apache를 구성하는 방법도 설명합니다.

 

 

 

필수 조건

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

 - sudo 권한을 가진 사용자로 로그인합니다.

- 서버 공용 서버 IP를 가리키는 도메인 이름을 가집니다. example.com을 이용합니다.

- Apache가 설치되었습니다. 도메인에 대한 Apache 가상 호스트입니다.

 

다음 지시사항에 따라 작성 방법에 대한 자세한 내용을 볼 수 있습니다.

 

 

 

Certbot 설치

Certbot은 Let's Encrypt SSL 인증서를 가져오고 갱신하기 위한 작업을 자동화할 수 있는 모든 기능을 갖추고 사용하기 쉬운 도구입니다. 인증서봇 패키지는 기본 데비안 리포지토리에 포함되어 있습니다.

다음 명령을 사용하여 패키지 목록을 업데이트하고 certbot 패키지를 설치합니다.

sudo apt update
sudo apt install certbot 

 

 

 

강력한 Dh(Diffie-Hellman) 그룹 생성

DH(Diffie–Hellman Key Exchange)는 보안되지 않은 통신 채널을 통해 암호화 키를 안전하게 교환하는 방법입니다. 

새 2048비트 DH 파라미터 세트를 생성하려면 다음을 실행합니다.

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

 

 

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

 

 

 

Let's Encrypt SSL 인증서 획득

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

좀 더 간단하게 하기 위해 잘 알려진/acme-challenge에 대한 모든 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

SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder On
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
# Requires Apache >= 2.4
SSLCompression off
SSLUseStapling on
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
# Requires Apache >= 2.4.11
SSLSessionTickets Off

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

 

 

위의 설명에는 권장 칩이 포함되어 있으며, OCSP Stapling, HTTP Strict Transport Security(HSTS)를 활성화하고 보안 중심의 HTTP 헤더를 거의 적용하지 않습니다. 

구성 파일을 활성화하기 전에 다음을 실행하여 mod_ssl 및 mod_headers가 모두 활성화되었는지 확인하십시오.

sudo a2enmod ssl
sudo a2enmod headers

 

 

HTTP/2 모듈을 활성화하면 사이트가 더욱 빠르고 견고해집니다.

sudo a2enmod http2

 

 

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

sudo a2enconf letsencrypt
sudo a2enconf ssl-params

 

 

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

sudo systemctl reload apache2

 

 

웹루트 플러그인과 함께 Certbot 도구를 사용하여 SSL 인증서 파일을 가져옵니다.

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

 

 

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

# IMPORTANT NOTES:
#  - Congratulations! Your certificate and chain have been saved at
#    /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will
#    expire on 2019-01-17. 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 lose your account credentials, you can recover through
#    e-mails sent to admin@example.com.
#  - Your account credentials have been saved in your Certbot
#    configuration directory at /etc/letsencrypt. You should make a
#    secure backup of this folder now. This configuration directory will
#    also contain certificates and private keys obtained by Certbot so
#    making regular backups of this folder is ideal.
#  - 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에서 nonwww 버전으로 리디렉션합니다. 필요에 따라 자유롭게 구성을 조정할 수 있습니다.

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

sudo systemctl reload apache2

 

 

https://를 사용하여 웹 사이트를 열면 녹색 잠금 아이콘이 나타납니다.

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

 

Debian 9 : Let's Encrypt로 Apache 보안 설정하는 방법, 예제, 명령어

 

 

 

자동 갱신 SSL 인증서를 암호화

Lets Encrypt의 인증서는 90일 동안 유효합니다. 인증서가 만료되기 전에 자동으로 갱신하려면 하루에 두 번 실행되는 크론 작업을 생성하고 인증서가 만료되기 30일 전에 모든 인증서를 자동으로 갱신합니다.

 

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

# /etc/cron.d/certbot

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

 

 

갱신 프로세스를 테스트하려면 certbot --dry-run 스위치를 사용하십시오.

sudo certbot renew --dry-run

 

 

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

반응형