フリーな認証局である「Let's Encrypt」を利用して、httpsサイトにします。
環境
$ cat /etc/almalinux-release
AlmaLinux release 9.5 (Teal Serval)
$ uname -r
5.14.0-503.14.1.el9_5.x86_64
certbotのインストール
Certbotは、Let's Encrypt証明書を自動的に使用してHTTPSを有効にするためのオープンソースソフトウェアツールです。
# dnf --enablerepo=epel install certbot
:
Installed:
certbot-2.11.0-1.el9.noarch fontawesome-fonts-1:4.7.0-13.el9.noarch
python-josepy-doc-1.14.0-1.el9.noarch python3-acme-2.11.0-1.el9.noarch
python3-certbot-2.11.0-1.el9.noarch python3-cffi-1.14.5-5.el9.x86_64
python3-configargparse-1.7-1.el9.noarch python3-configobj-5.0.6-25.el9.noarch
python3-cryptography-36.0.1-4.el9.x86_64 python3-importlib-metadata-4.12.0-2.el9.noarch
python3-josepy-1.14.0-1.el9.noarch python3-parsedatetime-2.6-5.el9.noarch
python3-ply-3.11-14.el9.noarch python3-pyOpenSSL-21.0.0-1.el9.noarch
python3-pycparser-2.20-6.el9.noarch python3-pyrfc3339-1.1-11.el9.noarch
python3-pytz-2021.1-5.el9.noarch python3-zipp-3.20.1-2.el9.noarch
Complete!
サーバ証明書の発行
# certbot certonly --agree-tos --non-interactive --webroot --webroot-path /wwwsite/orangetakam/html --domain orangetakam.com --domain www.orangetakam.com --email orange.takam@gmail.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for orangetakam.com and www.orangetakam.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/orangetakam.com/fullchain.pem ・・・ 証明書と中間証明書
Key is saved at: /etc/letsencrypt/live/orangetakam.com/privkey.pem ・・・ 秘密鍵
This certificate expires on 2025-03-27. ・・・ 有効期限が切れる日付
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ls -l /etc/letsencrypt/live/orangetakam.com
total 4
-rw-r--r--. 1 root root 692 Dec 28 06:31 README
lrwxrwxrwx. 1 root root 39 Dec 28 06:31 cert.pem -> ../../archive/orangetakam.com/cert1.pem
lrwxrwxrwx. 1 root root 40 Dec 28 06:31 chain.pem -> ../../archive/orangetakam.com/chain1.pem
lrwxrwxrwx. 1 root root 44 Dec 28 06:31 fullchain.pem -> ../../archive/orangetakam.com/fullchain1.pem
lrwxrwxrwx. 1 root root 42 Dec 28 06:31 privkey.pem -> ../../archive/orangetakam.com/privkey1.pem
Apache HTTP サーバでサーバ証明書を使うようにする
Apache HTTP サーバのSSLモジュールをインストールする
# dnf install mod_ssl
:
Installed:
mod_ssl-1:2.4.62-1.el9.x86_64
Complete!
サーバ証明書と秘密鍵があるディレクトリーへシンボリックリンクを作成する。
# cd /etc/httpd
# ln -s /etc/letsencrypt/live live
# ls -l live
lrwxrwxrwx. 1 root root 21 Dec 28 07:13 live -> /etc/letsencrypt/live
サーバ証明書(サーバ証明書+中間CA証明書)と秘密鍵がある場所を指定します。
# cp -p /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/BK-ssl.conf.original
# vi ssl.conf
# diff /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/BK-ssl.conf.original
85c85
< SSLCertificateFile live/orangetakam.com/fullchain.pem
---
> SSLCertificateFile /etc/pki/tls/certs/localhost.crt
93c93
< SSLCertificateKeyFile live/orangetakam.com/privkey.pem
---
> SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
すべてをhttpsで通信をするように設定します。
# vi rewrite.conf
<IfModule rewrite_module>
RewriteEngine On
LogLevel alert rewrite:trace3
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !(^/.well-known/)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
マシンリブートをします。
# systemctl restart httpd
サーバ証明書の自動更新
Let's Encryptが発行する証明書の有効期限は、90日間までですので、自動で更新するようにします。
シェルスクリプトの用意
# vi /res1/letsencrypt/letsencrypt_update.bash
#!/bin/bash
certbot renew -q --no-self-upgrade --post-hook "systemctl restart httpd.service"
SELinuxのアクセス制御対応
# ls -dZ /res1/letsencrypt
unconfined_u:object_r:default_t:s0 /res1/letsencrypt
# chcon -u system_u -r object_r -t bin_t /res1/letsencrypt
# ls -dZ /res1/letsencrypt
system_u:object_r:bin_t:s0 /res1/letsencrypt
# ls -dZ /res1/letsencrypt/letsencrypt_update.bash
unconfined_u:object_r:default_t:s0 /res1/letsencrypt/letsencrypt_update.bash
# chcon -u system_u -r object_r -t bin_t /res1/letsencrypt/letsencrypt_update.bash
# ls -dZ /res1/letsencrypt/letsencrypt_update.bash
system_u:object_r:bin_t:s0 /res1/letsencrypt/letsencrypt_update.bash
systemdの設定
# vi /etc/systemd/system/letsencrypt.service
[Unit]
Description=Let's Encrypt Update
[Service]
Type=oneshot
User=root
ExecStart=/res1/letsencrypt/letsencrypt_update.bash
[Install]
WantedBy=multi-user.target
# vi /etc/systemd/system/letsencrypt.timer
[Unit]
Description=Let's Encrypt Update
[Timer]
OnCalendar=*-*-* 13:00:00
Persistent=true
[Install]
WantedBy=timers.target
# systemctl enable letsencrypt.timer
Created symlink /etc/systemd/system/timers.target.wants/letsencrypt.timer
→ /etc/systemd/system/letsencrypt.timer.
# systemctl start letsencrypt.timer