CentOS7 Apache2에 Let's Encrypt으로 SSL 인증서 무료 발급 받기

따봉도관절

·

2022. 11. 5. 02:34

개인정보보호 관련 법률로 개인정보를 취급하는 웹 사이트는 SSL 인증서를 필수로 설치하여 데이터 통신 간 암호화를 하도록 되어있으며 이를 위반하고 개인정보를 분실, 도난, 누출 등 훼손한 자는 3년 이하의 징역 또는 3천만 원 이하의 벌금에 처한다고 합니다.

 

무료 SSL/TLS 인증서 발급 기관 Letsencrypt를 통하여 HTTPS 적용해보도록 하겠습니다.


서버 환경

 

  • Centos7
  • Apache2

 

필수 조건

 


 

[STEP 1] /etc/httpd/conf 경로 httpd.conf 파일 수정 RewriteRule 옵션 추가 및 httpd-ssl.conf Include

 

$ cd /etc/httpd/conf
$ vi httpd.conf

 

<VirtualHost *:80>
	DocumentRoot /var/www/html/test
  	ServerName gcpower.kr
  	ServerAlias gcpower.kr
  	ServerAdmin gcpower@gmail.com
  
  	CustomLog logs/access.log common
  	ErrorLog logs/error.log
	
  	RewriteEngine on
  	RewriteCond %{SERVER_NAME} =gcpower.kr
  	RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

# 맨 밑에 httpd-ssl.conf Include
Include /etc/httpd/conf/httpd-ssl.conf

 

http(80)으로 접근 시 RewriteRule에 의해 https로 재설정해준다.

httpd-ssl.conf는 인증서 발급 이후 작성할 예정이며 DocumentRootgcpower로 작성된 부분은 수정이 필요하다.

 

[STEP 2] Certbot 설치하기 위한 EPEL 저장소 활성화

 

$ yum install epel-release

 

[STEP 3] Certbot, Certbot-Apache, mod_ssl 설치

 

$ yum install certbot python2-certbot-apache mod_ssl

 

  • No package certbot available. 같은 문구가 나오면서 설치가 정상적으로 되지 않는다면 아래 명령어 순서대로 입력

 

$ yum remove epel-release
$ yum clean all
$ yum install epel-release
$ yum install certbot python2-certbot-apache mod_ssl

 

[STEP 4] Let's Encrypt 인증서 발급

 

$ certbot --apache certonly

 

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel):

 

인증서에 사용될 올바른 이메일 주소를 입력해준다.

 

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel:

 

동의하고 인증서 생성을 진행한다.

 

Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:

 

이메일 주소를 공유하고, 관련 메일을 받아 보고 싶다면 동의한다.

 

 

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: gcpower.kr
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

 

발급받고자 하는 도메인에 해당하는 숫자 번호를 입력해주면 발급된다.
전체 도메인을 발급받고자 하는 경우 빈칸 상태에서 Enter키를 누르면 된다.
콤마(,) 또는 스페이스로 분리하여 입력하여 여러 도메인 동시 발급도 가능하다.


SSL 인증서 발급 시 생성되는 4개 파일

 

  • cert.pem → 발급받은 도메인의 인증서 (public key)
  • chain.pem → Lest’s Encrypt의 Intermediate 인증서
  • fullchain.pem → cert.pem, chain.pem 두 인증서가 합쳐진 파일.
  • privkey.pem → publick key에 대응하는 private key

 

SSL 인증키 파일은 /etc/letsencrypt/live/도메인 경로에 생성된다.


 

[STEP 5] /etc/httpd/conf 경로 httpd-ssl.conf 파일 생성 및 코드 작성

 

$ pwd 
/etc/httpd/conf
$ vi httpd-ssl.conf

# vim 설치가 안되어 있는 경우
$ apt-get update
$ apt-get install vim

 

<IfModule mod_ssl.c>
  <VirtualHost *:443>
    	DocumentRoot /var/www/html/test
    	ServerName gcpower.kr
    	ServerAlias gcpower.kr
    	ServerAdmin gcpower@gmail.com
  
    	ErrorLog logs/error_log
    	CustomLog logs/access_log common

    	SSLCertificateFile /etc/letsencrypt/live/gcpower.kr/cert.pem
    	SSLCertificateKeyFile /etc/letsencrypt/live/gcpower.kr/privkey.pem
    	SSLCertificateChainFile /etc/letsencrypt/live/gcpower.kr/chain.pem

    	Include /etc/letsencrypt/options-ssl-apache.conf
  </VirtualHost>
</IfModule>

 

  • Tomcat 가동 중인 경우는 해당 포트로 ProxyPass 해준다.

 

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        DocumentRoot /var/www/html
        ServerAdmin gcpower@gmail.com
        ServerName gcpower.kr
        ServerAlias gcpower.kr

        ErrorLog logs/error_log
        CustomLog logs/access_log common
        
        SSLProxyEngine On
        ProxyRequests off
        ProxyPreserveHost On
        ProxyPass / http://127.0.0.1:8081/
        ProxyPassReverse / http://127.0.0.1:8081/

        SSLCertificateFile /etc/letsencrypt/live/gcpower.kr/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/gcpower.kr/privkey.pem
        SSLCertificateChainFile /etc/letsencrypt/live/gcpower.kr/chain.pem

        Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>
</IfModule>

 

[STEP 6] 아파치 재시작 후 도메인 접근 후 https 적용 확인

 

$ systemctl restart httpd

 

[STEP 7] Crontab으로 SSL 인증서 자동 갱신 세팅

 

$ crontab -e
# ESC키 누른 뒤 :wq! 입력시 저장하고 나가기
# ESC키 누른 뒤 :qa! 입력시 저장하지 않고 나가기

 

00 03 1 ** /usr/bin/certbot renew --renew-hook="systemctl restart httpd"

 

인증서는 3개월에 한 번씩 갱신해주어야 한다. 해당 작업을 크론탭으로 자동 갱신되도록 한다.

매월 1일 새벽 3시에 인증서 재발급 후에 아파치를 재시작하는 작업을 추가한 뒤 저장하면 완료.