環境
OracleLinux9.3
1.登録前のテスト
以下の–dry-runオプションを使って自動更新をテストします。
# certbot renew --dry-run
# certbot renew --dry-run
# certbot renew --dry-run
2.Snap版certbotをインストールする
証明書自動更新機能がインストールされます
# systemctl list-timers | less
3.snap.certbot.renew.timer のユニットファイルを確認する
# vi /etc/systemd/system/snap.certbot.renew.timer
# Auto-generated, DO NOT EDIT
Description=Timer renew for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-3700.mount
After=var-lib-snapd-snap-certbot-3700.mount
Unit=snap.certbot.renew.service
[Unit]
# Auto-generated, DO NOT EDIT
Description=Timer renew for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-3700.mount
After=var-lib-snapd-snap-certbot-3700.mount
X-Snappy=yes
[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 08:25
OnCalendar=*-*-* 21:27
[Install]
WantedBy=timers.target
[Unit]
# Auto-generated, DO NOT EDIT
Description=Timer renew for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-3700.mount
After=var-lib-snapd-snap-certbot-3700.mount
X-Snappy=yes
[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 08:25
OnCalendar=*-*-* 21:27
[Install]
WantedBy=timers.target
上記設定によるとOnCalenderパラメータで指定されている
毎日8時25分と21時27分に更新を行います
4.snap.certbot.renew.serviceのユニットファイルを確認する
# vi /etc/systemd/system/snap.certbot.renew.service
# Auto-generated, DO NOT EDIT
Description=Service for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-3700.mount
After=var-lib-snapd-snap-certbot-3700.mount network.target snapd.apparmor.service
EnvironmentFile=-/etc/environment
ExecStart=/usr/bin/snap run --timer="00:00~24:00/2" certbot.renew
SyslogIdentifier=certbot.renew
WorkingDirectory=/var/snap/certbot/3700
[Unit]
# Auto-generated, DO NOT EDIT
Description=Service for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-3700.mount
Wants=network.target
After=var-lib-snapd-snap-certbot-3700.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/3700
TimeoutStopSec=30
Type=oneshot
[Unit]
# Auto-generated, DO NOT EDIT
Description=Service for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-3700.mount
Wants=network.target
After=var-lib-snapd-snap-certbot-3700.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/3700
TimeoutStopSec=30
Type=oneshot
5.証明書を使用するWebサーバーの再起動は行われませんので、更新後に自動的に実行されるスクリプトを設定します
# vi /etc/letsencrypt/renewal-hooks/post/web_restart.sh
下記内容を記述
#!/bin/bash
systemctl reload httpd
#!/bin/bash
systemctl reload httpd
# chmod 755 /etc/letsencrypt/renewal-hooks/post/web_restart.sh
6. mod_sslをインストールします
# dnf -y install mod_ssl
ssl.conf ファイルを編集します
# vi /etc/httpd/conf.d/ssl.conf
DocumentRoot "/var/www/html/[FQDN]"
# SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/letsencrypt/live/[FQDN]/cert.pem
# SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/letsencrypt/live/[FQDN]/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/[FQDN]/chain.pem
#43 行目 コメント解除して変更
DocumentRoot "/var/www/html/[FQDN]"
#44 行目 コメント解除して変更
ServerName [FQDN]:443
#85行目 コメントにしてその下に追加
# SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/letsencrypt/live/[FQDN]/cert.pem
#93行目 コメントにしてその下に追加
# SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/letsencrypt/live/[FQDN]/privkey.pem
#103行目 追加
SSLCertificateChainFile /etc/letsencrypt/live/[FQDN]/chain.pem
#43 行目 コメント解除して変更
DocumentRoot "/var/www/html/[FQDN]"
#44 行目 コメント解除して変更
ServerName [FQDN]:443
#85行目 コメントにしてその下に追加
# SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/letsencrypt/live/[FQDN]/cert.pem
#93行目 コメントにしてその下に追加
# SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/letsencrypt/live/[FQDN]/privkey.pem
#103行目 追加
SSLCertificateChainFile /etc/letsencrypt/live/[FQDN]/chain.pem
Apacheを再起動します
# systemctl restart httpd
Firewallでhttpsを許可する
# firewall-cmd --add-service=https --permanent
# firewall-cmd --add-service=https --permanent
success
# firewall-cmd --reload
success
# firewall-cmd --add-service=https --permanent
success
# firewall-cmd --reload
success
7.HTTP 通信を HTTPS へリダイレクトする
.htaccessを/var/www/html/[FQDN]/ 配下に作成する
# vi /var/www/html/[FQDN]/.htaccess
.htaccessに以下の内容を記載する
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]