어제와 똑같이 살면서 다른 미래를 기대하지 말자

[CentOS9] Let's Encrypt 다중도메인 인증서 발급 및 자동갱신 (standalone) 본문

IT관심분야/Linux

[CentOS9] Let's Encrypt 다중도메인 인증서 발급 및 자동갱신 (standalone)

플랜액터 2024. 3. 20. 01:25

1. Let's Encrypt 패키지 설치 (certbot)

# dnf install certbot python3-certbot-apache

 

2. 웹서비스 중지

# systemctl stop httpd

 

3. 도메인 인증서 발급/수정/삭제

  도메인 인증서 발급

# certbot certonly --standalone --email 이메일 --cert-name 체인명 -d 도메인 --agree-tos

# certbot certonly --standalone --email admin@domain.com --cert-name domain.com -d dev.domain.com --agree-tos

 

  옵션설명

certonly : 인증서만 얻어옴

--standalone : standalone 모드로 실행

--email : 관리자 이메일

-d : 인증서 생성할 도메인 지정

--cert-name : 인성서 체인명 지정

--agree-tos : 대화형모드 yes/no에서 무조건 yes 처리

 

※ 인증서 발급확인

# ll /etc/letsencrypt/live/domain.com(체인명)/
total 4
lrwxrwxrwx 1 root root  35 Mar 19 13:25 cert.pem -> ../../archive/domain.com/cert1.pem
lrwxrwxrwx 1 root root  36 Mar 19 13:25 chain.pem -> ../../archive/domain.com/chain1.pem
lrwxrwxrwx 1 root root  40 Mar 19 13:25 fullchain.pem -> ../../archive/domain.com/fullchain1.pem
lrwxrwxrwx 1 root root  38 Mar 19 13:25 privkey.pem -> ../../archive/domain.com/privkey1.pem
-rw-r--r-- 1 root root 692 Mar 19 13:25 README

 

  도메인 인증서 추가 및 삭제 명령어

# certbot --cert-name 체인명 -d domain.com -d www.domain.com -d dev.domain.com 

- 기존 인증서에 domain.com, www.domain.com  추가되며, dev.domain.com을 뺄경우 삭제됨

 

  체인삭제 명령어

# certbot delete --cert-name 체인명

- 체인 삭제 전 Apache 도메인별 SSL 설정 삭제해야 웹서버 및 인증서 갱신 등 정상 작동

 

4. Apache SSL 설정

# vi /etc/httpd/conf.d/ssl.conf
<VirtualHost _default_:443>
    ServerAdmin admin@domain.com
    ServerName dev.domain.com
    DocumentRoot /home/domain/public

    SSLEngine on
    SetEnv APPLICATION_ENV "development"
#    SetEnv APPLICATION_ENV "production"
    <Location />
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^.*$ /index.php [NC,L]
    </Location>

    #SSLProtocol all -SSLv3
    #SSLProxyProtocol all -SSLv3

    SSLHonorCipherOrder on
    SSLCipherSuite PROFILE=SYSTEM
    SSLProxyCipherSuite PROFILE=SYSTEM

    SSLCertificateFile /etc/letsencrypt/live/domain.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/domain.com/chain.pem
    SSLCACertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
         SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory "/var/www/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>

    LogLevel warn
    ErrorLog logs/domain.com.ssl_error_log
    TransferLog logs/domain.com.ssl_access_log
    CustomLog logs/domain.com.ssl_request_log \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>

 

5. 웹서비스 시작

# systemctl start httpd

 

6. 인증서 자동갱신 설정

  인증서 갱신 테스트

# certbot renew --pre-hook "systemctl stop httpd" --post-hook "systemctl start httpd" --dry-run

 

  옵션설명

--pre-hook "명령어" : 갱신 전 실행 명령어

--post-hook "명령어" : 갱신 후 실행 명령어

--dry-run : 인증서 저장없이 renew or certonly 테스트

 

  크론등록 (자동갱신)

# crontab -e
# 매월 10,25일 let's encrypt 인증서 갱신
00 04 10,25 * * /usr/bin/certbot renew --pre-hook "systemctl stop httpd" --post-hook "systemctl start httpd"

 

7. http > https 자동 redirect 설정

# certbot --redirect --uir

/etc/httpd/conf.d/ 폴더로 이동

기존 http로 설정해놓은 virtualhost.conf 과 같은 파일에 들어가 보면 redirect 설정으로 변경됨

(기존 설정된 파일이 없는 경우 새로 생성됨)

 

참고문서

https://www.epari.net/@architecture/vlink/b/cj

https://www.fntec.net/article.php?no=216

Comments