SW개발/우분투 리눅스

[우분투 22.04] letsencrypt 인증서 재발급 방법 (Apache2 기준)

world class product 2022. 9. 18. 09:09

 

필자는 다양한 종류의 블로그와 웹페이지를 운영하고 있습니다. 네이버 블로그, 티스토리 블로그, 워드프레스 홈페이지, GitHub Pages 등 다양한 종류를 운영 중에 있습니다. 각 서비스마다 장단점을 가지고 있습니다. 

워드프레스로 운영하고 있는 홈페이지의 경우는 라즈베리파이에 우분투 OS, 웹서버, 워드프레스를 설치한 후 도메인을 연결해서 운영하고 있습니다. SSL 인증서는 letsencrypt를 사용 중인데, 최근에 인증서가 만료되는 불상사가 발생했습니다. SSL 인증서가 만료되더라도 웹서버는 계속 서비스되지만, 웹브라우저에서 안전하지 않은 연결로 감지되는 불편함이 있습니다. 

구글에서 검색해보면 letsencrypt를 이용해서 SSL 인증서를 획득하는 방법에 대한 수많은 자료들이 있습니다. 본 페이지에서는 letsencrypt 인증서를 갱신하는 방법에 대해서 설명드리겠습니다. 

 

1. certbot 패키지 설치

사실 이 글을 보고 계시는 분들은 이미 letsencrypt로 인증서를 받아보신 적이 있으신 분들일 것입니다. 이 때문에 certbot 패키지가 설치되어 있겠지만, 혹시라도 해당 패키지를 지우셨을 가능성이 있기 때문에 certbot 패키지 설치부터 설명드리겠습니다. 

$ sudo apt-get install certbot python3-certbot-apache

주로 사용하는 웹서버는 ngix와 apache2가 있습니다. 저는 아파치2를 활용하고 있기 때문에 Apache2 기준으로 설명을 드리고 있는 점 양해 부탁드립니다. 

 

2. 웹루트 경로와 도메인 네임으로 인증서 재발급

가장 손쉽게 인증서를 재발급하는 방법은 Apache2의 웹 루트와 도메인 네임으로 SSL 인증서를 재발급하는 방법입니다. 

$ sudo certbot certonly --webroot -w (웹 루트 경로) -d (도메인 네임)

위와 같은 형식으로 입력하시면 됩니다. 아래는 몇 가지 예제입니다. 

$ sudo certbot certonly --webroot -w /var/www/wordpress -d www.yourdomain.com
$ sudo certbot certonly --webroot -w /var/www/html -d www.mydomain.com

 

인증서가 무사히 재발급되면 아래와 같은 메시지가 출력됩니다. 

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Renewing an existing certificate for www.yourdomain.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/www.yourdomain.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/www.yourdomain.com/privkey.pem
This certificate expires on 2022-12-16.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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/letsencrypt/live/www.yourdomain.com/fullchain.pem 경로에 인증서가 발급되고, /etc/letsencrypt/live/www.yourdomain.com/privkey.pem 경로에 키가 생성된 것을 확인할 수 있습니다. 

 

3. 재발급된 인증서 경로 확인

새로 발급된 인증서는 /etc/letsencrypt/archive/www.yourdomain.com 경로에 저장이 됩니다. sudo 명령으로 해당 경로의 파일을 살펴봅니다. 

$ sudo ls -all /etc/letsencrypt/archive/www.yourdomain.com

 

위의 명령으로 확인해보면, 아래와 같이 인증서가 새로 생긴 것을 확인할 수 있습니다. 파란색으로 표기한 파일들이 이번에 새로 생성된 인증서들입니다. 

$ sudo ls -all /etc/letsencrypt/archive/www.yourdomain.com
-rw-r--r-- 1 root root 1854  5월 12 04:11 cert1.pem
-rw-r--r-- 1 root root 1850  9월 18 07:53 cert2.pem
-rw-r--r-- 1 root root 3749  5월 12 04:11 chain1.pem
-rw-r--r-- 1 root root 3749  9월 18 07:53 chain2.pem
-rw-r--r-- 1 root root 5603  5월 12 04:11 fullchain1.pem
-rw-r--r-- 1 root root 5599  9월 18 07:53 fullchain2.pem
-rw------- 1 root root 1704  5월 12 04:11 privkey1.pem
-rw------- 1 root root 1704  9월 18 07:53 privkey2.pem

 

그렇다면 생성시에 안내된 /etc/letsencrypt/live/www.yourdomain.com 경로에 인증서들은 무엇일까요? 역시 sudo ls 명령으로 해당 경로의 파일을 살펴봅니다. 

$ sudo ls -all /etc/letsencrypt/live/www.yourdomain.com

위와 같이 실행해보면, live에 위치한 파일들은 최신 인증서를 가리키는 링크라는 것을 눈치챌 수 있습니다. 

lrwxrwxrwx 1 root root   42  9월 18 07:53 cert.pem -> ../../archive/www.yourdomain.com/cert2.pem
lrwxrwxrwx 1 root root   43  9월 18 07:53 chain.pem -> ../../archive/www.yourdomain.com/chain2.pem
lrwxrwxrwx 1 root root   47  9월 18 07:53 fullchain.pem -> ../../archive/www.yourdomain.com/fullchain2.pem
lrwxrwxrwx 1 root root   45  9월 18 07:53 privkey.pem -> ../../archive/www.yourdomain.com/privkey2.pem

 

※ 여기까지 살펴보셨다면 아래와 같은 결론에 이를 수 있습니다.
/etc/letsencrypt/archive/www.yourdomain.com 디렉토리에는 지금까지 발급된 모든 인증서가 존재하고, 
/etc/letsencrypt/live/www.yourdomain.com 디렉토리에는 가장 최근에 발급된 인증서만 링크가 걸려 있습니다. 
따라서, SSL 인증서 위치를 설정할 때는 live 디렉토리의 심볼릭 링크를 활용하는 게 좋습니다. 그렇지 않으면, 인증서를 재발급할 때마다 아파치 설정 파일의 인증서 위치를 매번 변경해야 하는 수고로움이 있습니다. 

 

4. 아파치 웹서버 설정 확인

/etc/apache2/sites-available/default-ssl.conf 파일을 열어서 인증서 설정 부분을 살펴봅니다. 해당 파일은 기존에 아파치 설정을 어떻게 했느냐에 따라서 달라질 수 있습니다. 

$ sudo vi /etc/apache2/sites-available/default-ssl.conf

default-ssl.conf 파일에서 SSLCertificateFile, SSLCertificateKeyFile, SSLCertificateChainFile 등의 설정값을 확인합니다. 

#   SSLCertificateFile directive is needed.
                SSLCertificateFile      /etc/letsencrypt/archive/www.yourdomain.com/cert1.pem
                SSLCertificateKeyFile /etc/letsencrypt/archive/www.yourdomain.com/privkey1.pem
                SSLCertificateChainFile /etc/letsencrypt/archive/www.yourdomain.com/chain1.pem

 

위의 설정값을 아래와 같이 설정해주면 됩니다.

#   SSLCertificateFile directive is needed.
                SSLCertificateFile      /etc/letsencrypt/archive/www.yourdomain.com/cert2.pem
                SSLCertificateKeyFile /etc/letsencrypt/archive/www.yourdomain.com/privkey2.pem
                SSLCertificateChainFile /etc/letsencrypt/archive/www.yourdomain.com/chain2.pem

 

하지만 위에서 언급한 바와 같이, 인증서를 재발급할 때마다 cert2.pem -> cert3.pem -> cert4.pem으로 설정 파일을 변경해줘야 하는 번거로움이 있으므로, 아래와 같이 최신 인증서를 가리키는 아래 심볼릭 링크를 사용하시는 게 편리합니다. 

#   SSLCertificateFile directive is needed.
                SSLCertificateFile      /etc/letsencrypt/live/www.yourdomain.com/cert.pem
                SSLCertificateKeyFile /etc/letsencrypt/live/www.yourdomain.com/privkey.pem
                SSLCertificateChainFile /etc/letsencrypt/live/www.yourdomain.com/chain.pem

 

위와 같이 설정을 변경해주면 앞으로는 SSL 인증서만 재발급하면 되기 때문에 편리하게 인증서를 재발급할 수 있습니다. 

 

5. 아파치 웹서버 재시작 

위와 같이 설정 파일을 변경해줬다면, 아파치 웹서버를 재시작해야 합니다. 

$ sudo service apache2 restart

 

이상입니다.