Let's EncryptによるSSLサーバ証明書の導入準備

一応httpd動いているので、Let's Encryptを利用してSSLサーバ証明書をいれる準備をしてみる。

OSはOracle Linux 8.2

Certbot導入

CertbotというACMEクライアントを使うのがおすすめです。」とのこと。

CentOS/RHEL8 -  Apacheでの導入手順書を参考にしつつ。

EPELの有効化

ol8_developer_EPELが有効かどうかを確認する

$ env LANG=C sudo dnf repolist all
repo id                                repo name                                                                                    status
ol8_UEKR6                              Latest Unbreakable Enterprise Kernel Release 6 for Oracle Linux 8 (x86_64)                   enabled
ol8_UEKR6_RDMA                         Oracle Linux 8 UEK6 RDMA (x86_64)                                                            disabled
ol8_addons                             Oracle Linux 8 Addons (x86_64)                                                               disabled
ol8_appstream                          Oracle Linux 8 Application Stream (x86_64)                                                   enabled
ol8_baseos_latest                      Oracle Linux 8 BaseOS Latest (x86_64)                                                        enabled
ol8_codeready_builder                  Oracle Linux 8 CodeReady Builder (x86_64) - Unsupported                                      enabled
ol8_kvm_appstream                      Oracle Linux 8 KVM Application Stream (x86_64)                                               disabled
ol8_u0_baseos_base                     Oracle Linux 8 BaseOS GA (x86_64)                                                            disabled
ol8_u1_baseos_base                     Oracle Linux 8.1 BaseOS (x86_64)                                                             disabled
ol8_u2_baseos_base                     Oracle Linux 8.2 BaseOS (x86_64)                                                             disabled
$

そもそも存在してないので、まずはリポジトリのインストール

sudo dnf install oracle-epel-release-el8

再度確認

$ env LANG=C sudo dnf repolist all
repo id                                repo name                                                                                    status
ol8_UEKR6                              Latest Unbreakable Enterprise Kernel Release 6 for Oracle Linux 8 (x86_64)                   enabled
ol8_UEKR6_RDMA                         Oracle Linux 8 UEK6 RDMA (x86_64)                                                            disabled
ol8_addons                             Oracle Linux 8 Addons (x86_64)                                                               disabled
ol8_appstream                          Oracle Linux 8 Application Stream (x86_64)                                                   enabled
ol8_baseos_latest                      Oracle Linux 8 BaseOS Latest (x86_64)                                                        enabled
ol8_codeready_builder                  Oracle Linux 8 CodeReady Builder (x86_64) - Unsupported                                      enabled
ol8_developer_EPEL                     Oracle Linux 8 EPEL Packages for Development (x86_64)                                        enabled
ol8_kvm_appstream                      Oracle Linux 8 KVM Application Stream (x86_64)                                               disabled
ol8_u0_baseos_base                     Oracle Linux 8 BaseOS GA (x86_64)                                                            disabled
ol8_u1_baseos_base                     Oracle Linux 8.1 BaseOS (x86_64)                                                             disabled
ol8_u2_baseos_base                     Oracle Linux 8.2 BaseOS (x86_64)                                                             disabled
$

snapdインストール

続いて、dnf install snapdでsnapdをインストール。

インストール後、snapdを最新にする

$ sudo snap install core
Setup snap "core" (11420) security profiles                                                                                                -
2021-08-09T13:16:49+09:00 INFO Waiting for automatic snapd restart...
core 16-2.51.3 from Canonical? installed
$ sudo snap refresh core
snap "core" has no updates available
$

Certbotインストール

snapコマンドでCertbotをインストールする

 sudo snap install --classic certbot
certbot 1.18.0 from Certbot Project (certbot-eff?) installed
$ sudo snap list
Name     Version    Rev    Tracking       Publisher     Notes
certbot  1.18.0     1343   latest/stable  certbot-eff?  classic
core     16-2.51.3  11420  latest/stable  canonical?    core
core20   20210702   1081   latest/stable  canonical?    base
$ ls -la /snap/bin/certbot
lrwxrwxrwx. 1 root root 13  8月  9 13:21 /snap/bin/certbot -> /usr/bin/snap
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
$ ls -la /snap/bin/certbot /usr/bin/certbot
lrwxrwxrwx. 1 root root 13  8月  9 13:21 /snap/bin/certbot -> /usr/bin/snap
lrwxrwxrwx. 1 root root 17  8月  9 13:22 /usr/bin/certbot -> /snap/bin/certbot
$

ほい

SSLサーバー証明書インストール

事前に、証明書をインストールするホストにhttp/httpsでアクセスできることを確認しておく。

certbot --apacheを実行することで、証明書の取得・conf追加・reloadまでやってくれる。

SSLサーバー証明書の自動更新

Snap版だと、自動更新機能もインストールされる模様

# /bin/systemctl list-timers
NEXT                         LEFT       LAST                         PASSED       UNIT                         ACTIVATES
Tue 2021-08-10 13:29:15 JST  23min left Tue 2021-08-10 12:29:14 JST  36min ago    dnf-makecache.timer          dnf-makecache.service
Tue 2021-08-10 20:24:00 JST  7h left    Tue 2021-08-10 10:21:05 JST  2h 44min ago snap.certbot.renew.timer     snap.certbot.renew.service
Wed 2021-08-11 00:00:00 JST  10h left   Tue 2021-08-10 00:00:09 JST  13h ago      unbound-anchor.timer         unbound-anchor.service
Wed 2021-08-11 12:59:20 JST  23h left   Tue 2021-08-10 12:59:20 JST  6min ago     systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.servi>

4 timers listed.
Pass --all to see loaded but inactive timers, too.
#

unitfileを確認してみる

# cat /etc/systemd/system/snap.certbot.renew.timer
[Unit]
# Auto-generated, DO NOT EDIT
Description=Timer renew for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-1343.mount
After=var-lib-snapd-snap-certbot-1343.mount
X-Snappy=yes

[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 10:21
OnCalendar=*-*-* 20:24

[Install]
WantedBy=timers.target
#
# cat /etc/systemd/system/snap.certbot.renew.service
[Unit]
# Auto-generated, DO NOT EDIT
Description=Service for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-1343.mount
Wants=network.target
After=var-lib-snapd-snap-certbot-1343.mount network.target snapd.apparmor.service
X-Snappy=yes

[Service]
EnvironmentFile=-/etc/environment
ExecStart=/usr/bin/snap run --timer="00:00~24:00/2" certbot.renew
SyslogIdentifier=certbot.renew
Restart=no
WorkingDirectory=/var/snap/certbot/1343
TimeoutStopSec=30
Type=oneshot
#

ただし、証明書更新後のhttpd再起動は実施されないので、個別に設定しておく必要がある。/etc/letsencrypt/renewal-hooks/post/以下にスクリプトを置けばよい。

# cat /etc/letsencrypt/renewal-hooks/post/restart_httpd.sh
#!/bin/sh

/bin/systemctl restart httpd
#

これで毎日 10:21と20:24にSSLサーバ証明書の更新を試み、更新されたらhttpdが再起動されるはず。