1
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?

【web】SSL/TLS証明書の管理の自動化

Last updated at Posted at 2025-06-15

はじめに

SSL/TLS証明書を手動管理するとミスる場合があります。
管理対象サイトが増えてくると手動で管理するのは困難です。
Let's encrypt のツール certbot で自動更新するとよいです。

動作環境

AlmaLinux 9.5
Apache 2.4.62

前提条件

ドメイン名がDNS登録されていること
Firewallで 80/443 ポートが開いていること
サーバーに十分なリソース(CPU,Memory,Disk)の空きがあること

手順

  1. webサーバー(apache)に証明書受領用の設定をしておく (nginxの場合は読み替えて)

    /etc/httpd/conf/httpd.conf

    NameVirtualHost 999.999.999.999:80
    
    <VirtualHost 999.999.999.999:80>
        ServerAdmin your.domain.com
        DocumentRoot /var/www/your.domain.com
        ServerName your.domain.com
        ErrorLog logs/errorlog.log
        CustomLog logs/access_log.log common
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =your.domain.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    </VirtualHost>
    

    (999.999.999.999はあり得ないIPアドレスです。実際のサーバーのIPアドレスに置き換えます)

  2. snapd をインストールする

    root権限で

    dnf install epel-release
    dnf upgrade
    dnf install snapd
    

    パスを通す
    ln -s /var/lib/snapd/snap /snap

  3. certbot をインストールする

    既にcertbotが入っている場合は削除する
    dnf remove certbot

    snap install --classic certbot

    パスを通す
    ln -s /snap/bin/certbot /usr/bin/certbot

  4. webサーバー(apache)用にサーバー証明書をインストールする

    certbot --apache

結果を確認する

ブラウザで https://your.domain.com にアクセスして証明書ビューワーで見る

curl -kv http://your.domain.com

設定ファイル httpd.conf に次の行が追加されるはず
Include /etc/httpd/conf/httpd-le-ssl.conf

証明書の場所
/etc/letsencrypt/live/ドメイン名/

ログの場所
/var/log/letsencrypt

証明書更新を自動化する

crontab

0 3 1,15 * * /usr/bin/certbot renew --dry-run || /usr/bin/certbot renew

(毎月1日と15日の午前3時に実行する例)

運用コマンド

証明書更新テスト
certbot renew --dry-run

証明書更新実行
certbot renew

証明書情報表示
certbot certificates

ヘルプ
certbot --help

おわりに

都合でLet's encrypt が使えない場合は、CAが提供しているAPIを使用して certbot 様の自動化スクリプトを作ればよいです。

非公開のサイトは自己署名証明書、オレオレ認証局で充分です。
可能な限り長期間発行しておけば良いと思います。

可能ならクラウドでCSP(Cloud Service Provider)に任せるのが一番です。

参考リンク

Let’s Encrypt Getting Started

certbot公式

1
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
1
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?