業務用エアコン関連の技術情報、エラーコード、環境問題対策に関する別サイト「エアコンの安全な修理・適切なフロン回収」

AlmaLinux 9.1 : SSL証明書取得( Let's Encrypt ) , ApacheのSSL , Mail サーバーSSL/TLS

1.SSL証明書を取得する ( Let's Encrypt )

最新のopen sslをインストールしておく

# dnf install openssl-devel

1.1 事前準備

1.パッケージ管理システムSnappyインストール
Let’s EncryptのSSL証明書発行ツール「certbot」は2021年以降は「snap」を利用したインストールが推奨されていますので、まずSnapdをインストールします。(dnfまたはyumでの従来の方法でもインストールできます)

# dnf install epel-release
# dnf upgrade
# dnf install snapd

メインのスナップ通信ソケットを管理するsystemdユニットを有効化する

# systemctl enable --now snapd.socket
Created symlink /etc/systemd/system/sockets.target.wants/snapd.socket → /usr/lib/systemd/system/snapd.socket.

クラシックスナップのサポートを有効にする

# ln -s /var/lib/snapd/snap /snap

バージョン確認

# snap --version
snap       2.57.6-2.el9
snapd      2.57.6-2.el9
series     16
almalinux  9.1
kernel     5.14.0-162.6.1.el9_1.x86_64

ログアウトして再度ログインするか、システムを再起動して、snapのパスが正しく更新されていることを確認

2.certbot パッケージインストール

# snap install --classic certbot
certbot 1.32.2 from Certbot Project (certbot-eff?) installed

/snap/bin/certbotへのシンボリックリンクを作成

# ln -s /snap/bin/certbot /usr/bin/certbot

確認する

1.2 証明書の取得

# certbot certonly --webroot -w /var/www/html/[FQDN] -d [FQDN]
# 初回のみメールアドレスの登録と利用条件への同意が必要
# 受信可能なメールアドレスを指定
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): <mail address>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at

Page not found
You must agree in order to register with the ACME server. Do you agree? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing, once your first certificate is successfully issued, 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: y Account registered. Requesting a certificate for [FQDN] Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/[FQDN]/fullchain.pem Key is saved at: /etc/letsencrypt/live/[FQDN]/privkey.pem This certificate expires on 2023-04-05. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. We were unable to subscribe you the EFF mailing list because your e-mail address appears to be invalid. You can try again later by visiting https://act.eff.org. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

「Successfully received certificate.」と表示されれば成功
# メッセージ中に記載の通り [/etc/letsencrypt/live/<FQDN>/] 配下に次の証明書が取得されている

# cert.pem ⇒ SSLサーバー証明書(公開鍵含む)
# chain.pem ⇒ 中間証明書
# fullchain.pem ⇒ cert.pem と chain.pem が結合されたファイル
# privkey.pem ⇒ 公開鍵に対する秘密鍵

※ Webサーバーが稼働していない場合のLet's Encrypt証明書の取得

インターネット側から当作業を実施するサーバーの 80 ポート宛てにアクセス可能であることは前提となります

# [--standalone] 指定で 簡易 Webサーバー機能を使用
# -d [証明書を取得したいFQDN] # FQDN (Fully Qualified Domain Name) : ホスト名.ドメイン名を省略なしで表記
# 証明書を取得したいFQDNが複数ある場合は、-d [証明書を取得したいFQDN] を複数指定

# certbot certonly --standalone -d <FQDN>

取得済みの証明書を更新する
# 有効期限が 30日未満の証明書を全て更新
# 有効期限の残り日数に関わらず更新したい場合は [--force-renewal] を合わせて指定

# certbot  [--force-renewal]  renew

1.3 証明書を自動更新 (Let's Encrypt)

①登録前のテスト
まず以下の--dry-runオプションを使って自動更新をテストしてみる。このオプションでは、証明書は更新されずに動作確認のみ実施されるため、証明書の取得回数制限に引っかかる心配もない

# certbot renew --dry-run

②Snap版certbotをインストールすると、証明書自動更新機能も合わせてインストールされます

# systemctl list-timers | less
NEXT                        LEFT          LAST                        PASSED       UNIT                         ACTIVATES
Thu 2023-01-05 13:58:10 JST 11min left    Thu 2023-01-05 13:28:14 JST 18min ago    pmie_farm_check.timer        pmie_farm
_check.service
Thu 2023-01-05 14:10:19 JST 24min left    n/a                         n/a          dnf-makecache.timer          dnf-makecache.service
Thu 2023-01-05 14:55:00 JST 1h 8min left  n/a                         n/a          snap.certbot.renew.timer     snap.certbot.renew.service
Fri 2023-01-06 00:00:00 JST 10h left      Thu 2023-01-05 08:34:24 JST 5h 11min ago logrotate.timer              logrotate.service
Fri 2023-01-06 00:00:00 JST 10h left      Thu 2023-01-05 08:34:24 JST 5h 11min ago mlocate-updatedb.timer       mlocate-updatedb.service
Fri 2023-01-06 00:00:00 JST 10h left      n/a                         n/a          sa-update.timer              sa-update.service
Fri 2023-01-06 00:00:00 JST 10h left      Thu 2023-01-05 08:34:24 JST 5h 11min ago unbound-anchor.timer         unbound-anchor.service
Fri 2023-01-06 00:07:00 JST 10h left      n/a                         n/a          sysstat-summary.timer        sysstat-summary.service
Fri 2023-01-06 00:08:00 JST 10h left      Thu 2023-01-05 08:34:39 JST 5h 11min ago pmie_daily.timer             pmie_daily.service
Fri 2023-01-06 00:10:00 JST 10h left      Thu 2023-01-05 08:34:41 JST 5h 11min ago pmlogger_daily.timer         pmlogger_daily.service
Fri 2023-01-06 13:30:31 JST 23h left      Thu 2023-01-05 13:30:31 JST 15min ago    systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service

snap.certbot.renew.timer が登録されています

snap.certbot.renew.timer のユニットファイルを確認

# vi /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-2414.mount
After=var-lib-snapd-snap-certbot-2414.mount
X-Snappy=yes

[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 04:49
OnCalendar=*-*-* 15:16

[Install]
WantedBy=timers.target

上記設定によるとOnCalenderパラメータで指定されている毎日4時49分と15時16分に更新を試みます(ただし、更新ごとにランダムに設定時刻が変更します)

snap.certbot.renew.serviceのユニットファイルを確認

# vi /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-2414.mount
Wants=network.target
After=var-lib-snapd-snap-certbot-2414.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/2414
TimeoutStopSec=30
Type=oneshot

ただし、証明書を使用するWebサーバーの再起動は行われませんので、更新後に自動的に実行されるスクリプトを設定します

# vim /etc/letsencrypt/renewal-hooks/post/web_restart.sh

#!/bin/bash
systemctl reload httpd
# chmod 755 /etc/letsencrypt/renewal-hooks/post/web_restart.sh

2. Apache のhttps 化

念のため、下記をインストールしておく

# dnf -y install mod_ssl

2.1 ssl.conf ファイルの編集

Apache を再起動します。
# systemctl restart httpd
Firewallでhttpsを許可する
# firewall-cmd --add-service=https --permanent
success
# firewall-cmd --reload
success

2.2 HTTP 通信を HTTPS へリダイレクト

.htaccessを/var/www/html/[FQDN]/ 配下に作成
.htaccessの内容
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

3. Mail サーバーにSSL/TLS(Let's Encrypt) の設定

3.1 メールサーバー用証明書の取得

メールサーバー用の証明書を取得するが上記と同様の方法では取得できないので「--standalone」オプションをつけて下記のようにしても失敗する。

一度web サーバーを止めてから行うと下記のとおり成功する

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for mail.<domain name>

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/mail.<domain name>/fullchain.pem
Key is saved at: /etc/letsencrypt/live/mail.<domain name>/privkey.pem
This certificate expires on 2023-04-05.
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

3.2 Postfixの設定

# vi /etc/postfix/master.cf
● 19-22行目 : コメント解除
submission inet n - n - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
● 31-34行目 : コメント解除
smtps inet n - n - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes

3.3 Dovecotの設定

firewallでPort 587を許可する

# firewall-cmd --add-port=587/tcp --permanent
# firewall-cmd --reload
# systemctl restart postfix dovecot

3.4 Thunderbirdの設定

受信サーバー

Port  :  143
Connection security   :  STARTTLS
Authentication method  :  Normal password

送信サーバー

Port   :  587
Connection security   :  STARTTLS
Authentication method  :  Normal password

タイトルとURLをコピーしました