example.com にサブドメイン blog を追加して blog.example.com を SSL/TLS 化します。

参照: https://letsencrypt.org/ja/

Certbot を使用した TLS / SSL 化

Snap(snapd)を使用して Certbot をインストールします。

certbot instructions

手動作業手順(記載順序どおりに実行)

Certbot の完全自動設定を使わず、Webroot 方式などで手動設定する場合の標準手順です。

  1. DNSの設定: サブドメイン blog を CNAME(またはAレコード)に登録
  2. ディレクトリを作成: .well-known/acme-challenge ディレクトリを作成
  3. Apache設定ファイルを作成: ポート80用の設定
  4. 設定の有効化: a2ensite でサイトを有効化
  5. 証明書の取得: Let’s Encrypt から証明書を取得
  6. Apache設定ファイル(SSL版)の追記: ポート443の設定およびリダイレクト追記
  7. 設定の反映: Apache をリロード
  8. 自動更新のテスト: certbot renew --dry-run を実行

1. DNS設定

DNS管理画面にて、サブドメイン blog を CNAME(または A レコード)に登録します。

2. ディレクトリ作成

mkdir -p /var/www/html/blog.example.com/current/web/.well-known/acme-challenge

3. Apache設定ファイル(HTTP / SSLなし版)

/etc/apache2/sites-available/blog.example.com.conf

<Directory />
    AllowOverride None
</Directory>

<Directory "/var/www/html/blog.example.com/current/web">
    AllowOverride All
    Options -Indexes +FollowSymLinks
</Directory>

<!-- ACME認証用ディレクトリの設定(Apache 2.4向け) -->
<Directory "/var/www/html/blog.example.com/current/web/.well-known/acme-challenge">
    Require all granted
</Directory>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

<VirtualHost *:80>
    ServerName blog.example.com
    DocumentRoot /var/www/html/blog.example.com/current/web
</VirtualHost>

4. 設定を有効化

sudo a2ensite blog.example.com.conf
sudo systemctl reload apache2

5. Let’s Encrypt で証明書を取得

Webroot 方式(証明書の発行のみを行う場合):

sudo certbot certonly --agree-tos --webroot -w /var/www/html/blog.example.com/current/web -d blog.example.com -m info@example.com

-w--webroot-path)でドキュメントルートを指定します。

Apache プラグインによる自動設定の場合(参考):

sudo certbot --apache --agree-tos -w /var/www/html/blog.example.com/current/web -d blog.example.com -m info@example.com

6. Apache設定ファイルを修正(HTTPS / SSL版)

/etc/apache2/sites-available/blog.example.com.conf

<Directory />
    AllowOverride None
</Directory>

<Directory "/var/www/html/blog.example.com/current/web">
    AllowOverride All
    Options -Indexes +FollowSymLinks
</Directory>

<Directory "/var/www/html/blog.example.com/current/web/.well-known/acme-challenge">
-   Allow from all
-   Satisfy any
+   Require all granted
</Directory>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

+ <IfModule mod_ssl.c>
+     <VirtualHost *:443>
+         ServerName blog.example.com
+         DocumentRoot /var/www/html/blog.example.com/current/web
+ 
+         SSLEngine on
+         SSLCertificateFile /etc/letsencrypt/live/blog.example.com/fullchain.pem
+         SSLCertificateKeyFile /etc/letsencrypt/live/blog.example.com/privkey.pem
+ 
+         <FilesMatch \.php$>
+             SSLOptions +StdEnvVars
+         </FilesMatch>
+ 
+         ErrorLog ${APACHE_LOG_DIR}/blog.example.com.error.log
+         CustomLog ${APACHE_LOG_DIR}/blog.example.com.access.log common
+     </VirtualHost>
+ </IfModule>

<VirtualHost *:80>
    ServerName blog.example.com
-   DocumentRoot /var/www/html/blog.example.com/current/web
+   RedirectPermanent / https://blog.example.com/
</VirtualHost>

7. Apache設定ファイルの変更を反映

設定ファイルの文法を事前にチェックしてからリロードします。

sudo apachectl configtest
sudo systemctl reload apache2

8. 自動更新の確認

Snap 版 Certbot では systemd timer により自動更新(1日2回)がデフォルトで有効化されています。以下で更新テスト(疑似実行)が可能です。

sudo certbot renew --dry-run