SW/리눅스

Cent OS 7 : Let's Encrypt Apache 보호하는 방법, 예제, 명령어

얇은생각 2021. 1. 5. 07:30
반응형

Let Encryption은 ISRG(Internet Security Research Group)에서 개발한 무료, 자동화 및 개방형 인증 기관입니다. Let's Encrypt에서 발급한 인증서는 발급일로부터 90일 동안 유효하며 오늘날 모든 주요 브라우저에서 신뢰합니다.

무료 Let's Encrypt SSL 인증서를 CentOS 7 서버에서 Apache를 웹 서버로 실행하고 설치하는 데 필요한 단계를 설명합니다. certbot 유틸리티를 사용하여 Let's Encryption 인증서를 가져오고 갱신합니다.

 

 

 

필수 구성 요소

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

- 공용 서버 IP를 가리키는 도메인 이름이 있어야 합니다. example.com을 사용하겠습니다.

- Apache가 서버에 설치되어 실행되고 있습니다.

- 도메인의 Apache 가상 호스트가 있어야 합니다.

- 포트 80 및 443은 방화벽 에서 열립니다.

SSL 암호화 웹 서버에 필요한 다음 패키지를 설치합니다.

yum install mod_ssl openssl

 

 

 

Certbot 설치

Certbot은 서버에서 HTTPS 자동 활성화 및 암호화로 SSL 인증서를 가져오는 프로세스를 단순화하는 도구입니다.

인증서봇 패키지는 EPEL에서 설치할 수 있습니다. EPEL 리포지토리가 시스템에 설치되어 있지 않은 경우 다음 명령을 사용하여 설치할 수 있습니다.

sudo yum install epel-release

 

 

EPEL 리포지토리가 활성화되면 다음을 입력하여 certbot 패키지를 설치합니다.

sudo yum 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이 실행되는 서버로 결정되는지 확인합니다.

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

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

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

 

 

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

# /etc/httpd/conf.d/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/httpd/conf.d/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

 

 

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

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

sudo systemctl reload httpd

 

 

이제 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-12-07. 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

 

 

CentOS 7은 SSLOpenSSLConfCmd 지시어를 포함하지 않는 Apache 버전 2.4.6과 함께 제공됩니다. 이 지시어는 이후 Apache 2.4.8에서만 사용할 수 있으며 Diffie–와 같은 OpenSSL 매개 변수를 구성하는 데 사용됩니다.  

Let's Encrypt SSL certificate와 생성된 DH 파일을 사용하여 새로운 결합 파일을 생성해야 합니다. 이렇게 하려면 다음을 입력합니다.

cat /etc/letsencrypt/live/example.com/cert.pem /etc/ssl/certs/dhparam.pem >/etc/letsencrypt/live/example.com/cert.dh.pem

 

 

이제 모든 것이 설정되었으므로 다음과 같이 도메인 가상 호스트 구성을 편집합니다.

# /etc/httpd/conf.d/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

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

  DocumentRoot /var/www/example.com/public_html
  ErrorLog /var/log/httpd/example.com-error.log
  CustomLog /var/log/httpd/example.com-access.log combined

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

  # Other Apache Configuration

</VirtualHost>

 

 

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

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

sudo systemctl restart httpd

 

 

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

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

CentOS 7 :Let's Encrypt로 Apache 보호하는 방법, 예제, 명령어

 

 

 

SSL 인증서 자동 갱신

암호화 인증서는 90일 동안 유효합니다. 인증서가 만료되기 전에 자동으로 갱신되도록 하루에 두 번 실행되며 만료 30일 전에 인증서가 자동으로 갱신되는 cronjob을 만듭니다. 

crontab 명령을 실행하여 인증서를 갱신할 새 cronjob을 생성하고, DH 키를 포함한 새 결합된 파일을 생성하고, apache를 다시 시작합니다.

sudo crontab -e

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload httpd"

 

 

파일을 저장한 후 닫습니다.

갱신 프로세스를 테스트하려면 certbot 명령과 함께 --dry-run 스위치를 사용할 수 있습니다.

sudo certbot renew --dry-run

 

 

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

반응형