본문 바로가기

개발자는 오늘도 달립니다.

[보안 인증서] Let's Encrypt 인증서 발급 방법 with Apache 본문

보안

[보안 인증서] Let's Encrypt 인증서 발급 방법 with Apache

✍21시간 2021. 9. 1. 17:09

Let's Encrypt 란?


비영리 인터넷 보안 연구 그룹 인 ISRG(Internet Security Research Group) 에서 개발한 무료,
자동 및 공개 인증 기관입니다.

 

 

Internet Security Research Group

ISRG’s mission is to reduce financial, technological, and educational barriers to secure communication over the Internet.

www.abetterinternet.org

 

 

인증서를 발급하기 전에 Let's Encrypt 는 도메인 소유권을 확인합니다.
호스트에서 실행되는 Let's Encrypt 클라이언트는 필요한 정보가 포함된 임시 파일(토큰)을 생성합니다.
그런 다음 Let's Encrypt 유효성 검사 서버는 HTTP 요청을 만들어 파일을 검색하고 토큰의 유효성을 검사하여 도메인의 DNS 레코드가 Let's Encrypt 클라이언트를 실행하는 서버로 확인되는지 확인합니다.
Let's Encrypt 에서 발급한 인증서는 발급일로부터 90일 동안 유효하며 오늘날 모든 주요 브라우저에서 신뢰합니다.

 

 

Let's Encrypt를 시작하기 전에 아래 몇 가지 조건들을 확인하세요.

필요 조건

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

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


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


4. 포트 80 및 443은 외부 접속 가능 상태 (방화벽 해제)


5. SSL 암호화 웹 서버에 필요한 다음 패키지를 설치합니다. [ yum install mod_ssl openssl ]

 

 

1. Certbot 설치

Certbot은 수동으로 관리되는 웹사이트에서 Let's Encrypt 인증서를 자동으로 사용하여 HTTPS를 활성화하는 무료 오픈 소스 소프트웨어 도구입니다.

 

다음을 입력하여 certbot 패키지를 설치합니다.

sudo yum install certbot

 

(인증서 봇 패키지는 EPEL에서 설치할 수 있습니다. EPEL 레퍼지토리가 시스템에 설치되어 있지 않은 경우 다음 명령으로 설치를 해줍니다.)

sudo yum install epel-release

 

 

 

2. Strong Dh(Diffie-Hellman) 그룹 생성

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

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

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

 

 

 

3. SSL 암호화 인증서 획득

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

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

 

 

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

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

 

 

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

# /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://를 사용하여 웹 사이트를 열 수 있으며 녹색 잠금 아이콘이 표시됩니다.

이제 아래 링크 사이트에서 도메인을 테스트하면 아래와 같이 A+ 등급을 받을 수 있습니다.

https://www.ssllabs.com/ssltest/

Screenshot of the final report on my server

 

 

 

 

부록) 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

 

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

 

 

'보안' 카테고리의 다른 글

[openssl] openssl 빌드 on unix / linux / windows  (0) 2019.07.15
Comments