도메인의 A레코드를 해당 서버의 공인 IP로 등록

# dig A 도메인
# dig www.도메인

certbot 설치

# dnf install -y epel-release
# dnf install -y certbot python3-certbot-apache
curl 명령어를 통해 도메인이 정상적으로 작동하는지 확인

> Let's Encrypt webroot 인증 방식은 80번 포트 접근이 필수적

또한 해외망이 열려있어야 인증 가능

# dnf -y install mod_ssl

- Apache SSL 모듈 설치(HTTPS 서비스용)

> webroot 방식으로 인증서 발급 자체는 SSL 설정 없이 가능

HTTPS 서비스 제공을 위해서는 mod_ssl + 443 vhost 필요

vhost는 httpd.conf에 직접 넣는 것보다

새로운 디렉토리(/etc/httpd/conf.d/) 안에 파일로 분리 관리를 권장

ex)

예시: /etc/httpd/conf.d/도메인

<VirtualHost *:80>

ServerName www. 도메인

ServerAlias 도메인

DocumentRoot "/var/www/html"

ErrorLog /var/log/httpd/도메인-error.log

CustomLog /var/log/httpd/도메인-access.log combined

<Directory "/var/www/html">

AllowOverride None

Require all granted

</Directory>

</VirtualHost>

-복붙 시 문법 오류 발생

Apache 설정 문법 확인

# apachectl configtest

or

# httpd -t

설정한 파일에 문법 오류가 없다면

# systemctl reload httpd

or

# apache graceful
# systemctl reload httpd가 주로 쓰임

Let’s Encrypt 인증서 발급 (webroot 방식)

# certbot certonly --webroot -w /var/www/html -d 도메인 -d www. 도메인

-인증 완료 후

vhost 파일에 443 설정 추가

<VirtualHost *:443>

ServerName www. 도메인

ServerAlias 도메인

DocumentRoot /var/www/html

SSLEngine on

SSLCertificateFile /etc/letsencrypt/live/도메인/fullchain.pem

SSLCertificateKeyFile /etc/letsencrypt/live/도메인/privkey.pem

ErrorLog /var/log/httpd/도메인-ssl-error.log

CustomLog /var/log/httpd/도메인-ssl-access.log combined

</VirtualHost>

HTTP -> HTTPS 리다이렉트 설정

80번 vhost 안에 아래 내용 추가(<VirtualHsot> 박스 안에)

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

* RewriteEngine ON

-> mod_rewrite 기능 실행

(Apache에서 요청 URL을 규칙 기반으로 가공.변환 및 제어하는 모듈(요청의 흐름 자체를 바꾸는 엔진))

* RewrteCond %{HTTPS} off

-> 현재 요청이 HTTPS가 아닐 때만 아래 RewriteRule을 적용하라는 조건

* RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

-> ^: 모든 요청 URI에 대해

https:%{HTTP_HOST}%{REQUEST_URI}: 현재 도메인 유지, 요청 경로 및 파라미터 유지

[R=301,L]: 301 영구 리다이렉트 이 규칙이 실행되면 더 이상 다른 rewrite 규칙을 처리하지 않음

인증서 자동 갱신 설정(무중단)

cron 예시

cron 예시

0 3 * * 1,4 /usr/bin/certbot renew --quiet --deploy-hook "/bin/systemctl reload httpd"

or

0 3 * * 1,4 /usr/bin/certbot renew --quiet --deploy-hook "/usr/sbin/apachectl graceful"

(오전 3시 월.목에 해당 명령어 실행)

/usr/bin/cerbot renew --quiet

만료 30일 이내인 인증서만 자동 갱신 및 출력 최소화

deploy-hook

실제로 인증서가 갱신되었을 때만 실행