0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon Linux 2023 Let's Encrypt でSSL証明書の取得と自動更新

Last updated at Posted at 2025-05-31

🔰 はじめに

Amazon Linux 2023でSSL証明書を設定する際、certbotを使ってLet's Encryptの無料証明書を取得する方は多いと思います。
私はこれまでcrontabで証明書の自動更新をしていましたが、調べていくうちにcertbot-renew.timerというsystemdベースの方法があることを知りました。

今回は、

  • Nginx のインストール
  • certbot での証明書取得
  • 証明書更新の自動化
  • crontab と systemd の違い
    についてまとめました。

🧱 Nginx のインストール

Amazon Linux 2023ではFedoraをベースに派生したOSのため、Nginxは dnf コマンドでインストール可能です。

$ dnf install -y nginx

バージョン確認

$ nginx -version

nginx.confを編集

$ vim /etc/nginx/conf.d/default.conf
server {
    server_name example.com    # ドメイン名を記載
    ~省略~
}

最後にサービスを有効化して起動します

$ systemctl enable --now nginx

:writing_hand: certbot での証明書取得

Amazon Linux 2023のリポジトリにはcertbotが含まれているのでまずはインストール

$ dnf install certbot

バージョン確認

$ certbot --version

証明書の取得

$ certbot --nginx

上記コマンドを実行することで

  • 証明書の取得
  • certbotが取得した証明書の情報を/etc/nginx/default.confに自動的に編集
  • HTTPSアクセスを有効化

をしてくれます!

:clock1: 証明書更新の自動化

Amazon Linux 2023はデフォルトで以下のUnitファイルが用意されています。

/usr/lib/systemd/system/certbot-renew.service
certbot-renew.service
// 証明書の更新をするサービスユニット
[Unit]
Description=This service automatically renews any certbot certificates found

[Service]
EnvironmentFile=/etc/sysconfig/certbot
Type=oneshot           # 一度だけ実行する
ExecStart=/usr/bin/certbot renew --noninteractive --no-random-sleep-on-renew $PRE_HOOK $POST_HOOK $RENEW_HOOK $DEPLOY_HOOK $CERTBOT_ARGS    # 実行するコマンド
/usr/lib/systemd/system/certbot-renew.timer
certbot-renew.timer
// certbot-renew.serviceの`ExecStart`をいつ実行するか設定するサービスユニット
[Unit]
Description=This is the timer to set the schedule for automated renewals

[Timer]
OnCalendar=*-*-* 00/12:00:00    # 毎日 0時、12時に1回実行(2回/日)
RandomizedDelaySec=12hours      # 実行タイミングをランダムに最大12時間ずらす(負荷分散のため)
Persistent=true                 # 前回電源がオフだったら、次回起動時に即実行する

[Install]
WantedBy=timers.target

systemdでは「ユニット名の連携ルール」によって
○○.serviceと○○.timerの○○が同じ名前であれば自動で関連付けされます。
なので○○.timer内に○○.serviceを明示的に指定しなくても指定時間に実行される仕組みです。

参考記事

certbot-renew.timerを有効化し自動更新されるようにする

$ systemctl enable --now certbot-renew.timer

Nginxの再起動を追加するためcertbot-renew.serviceをオーバーライドし設定を追加します

Nginxを再起動する理由

Nginxは以下のような設定でSSL/TLS 証明書を指定します

ssl_certificate     /etc/letsencrypt/live/your-domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain/privkey.pem;

これらのファイルは、Nginxの起動時や reload 時にだけ読み込まれます
証明書ファイル(*.pem)がディスク上で更新されても、nginx は自動ではその変更を検知せず、古い証明書を使い続けるため証明書を更新した際はNginxもreloadする必要があります

// 私はvim以外触れないので、nanoが触れる人はEDITOR指定なしでOK
$ EDITOR=vim systemctl edit certbot-renew.service
override.conf
[Service]
ExecStartPost=/bin/systemctl reload nginx  # ExecStart= で指定されたコマンドが正常に呼び出された後にのみ実行

すると /etc/systemd/system/certbot-renew.service.d/override.conf が作成されます

/usr/lib/systemd/system/ と /etc/systemd/system/ の違い
ディレクトリ 役割 編集対象
/usr/lib/systemd/system OSやパッケージがインストール時に配置する ❌(自分で編集すべきではない)
/etc/systemd/system/ 管理者が上書き・カスタマイズ用に使う場所 ✅(ここに配置するのが推奨)

certbot-renewのステータス確認

$ systemctl status certbot-renew
○ certbot-renew.service - This service automatically renews any certbot certificates found
     Loaded: loaded (/usr/lib/systemd/system/certbot-renew.service; static)
    Drop-In: /etc/systemd/system/certbot-renew.service.d
             └─override.conf  // ✅ 読み込まれていることを確認
     Active: inactive (dead)
TriggeredBy: ● certbot-renew.timer

設定変更の反映

$ systemctl daemon-reload

🏁 おわりに

この記事では、Amazon Linux 2023環境におけるNginx のインストールから、certbotを使ったSSL証明書の取得、さらに systemdタイマーによる証明書の自動更新までを一通り解説しました。

従来はcrontabを使って証明書の更新を自動化しているケースも多かったと思いますが、Amazon Linux 2023のようなsystemdベースのモダンなOSでは、certbot-renew.timerを活用することでより確実で柔軟な運用が可能になります。

また、証明書を更新しただけではNginx側には自動で反映されないという点は意外と見落とされがちなので、ExecStartPost=/bin/systemctl reload nginxの設定もぜひ併せて行っておきましょう。

今後も AWS上での構成や証明書管理の運用を改善していくうえで、本記事が少しでも参考になれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?