Let’s Encryptについて
サーバーとドメインさえあれば無料のSSL証明書を発行できるサービスです。
2016年4月に正式サービスが開始されて商用利用も可能なのです!
▼公式WEB
https://letsencrypt.org/
構築環境
- CentOS Linux release 8.2.2004 (Core)
- httpd 2.4.37-21
- mod_ssl x86_64 1:2.4.37-21
- ルータで「ポート 443」を開放
ルータのポート開放でSSL通信を受け入れる
SSL証明書の発行と組み込みを行う前に、サーバー側NWのルータのポートを開放して、外部からの通信がサーバーに届くようにします。
SSL通信で使われるポート番号は「443」になります。
ポートの設定方法はルータによって違いますが、概ね下記のような感じです。
プロトコル | TCP / UDP |
開始ポート番号 | 443 |
終了ポート番号 | 443 |
サーバーのローカルアドレス | 192,168.x.x |
▼通信イメージ
▼ルータの設定イメージ
certbot-autoコマンドの導入
Let's EncryptでSSL証明書を取得するための「certbot-auto」をcurlコマンドを用いて導入します。
▼SSLモジュールをDLするリポジトリを追加
command
# dnf install epel-release
▼ApacheにSSLモジュールをインストール
command
# dnf install mod_ssl
[/st-mybox]
▼certbotのDL
command # dnf install python3-certbot-apache
SSL証明書の取得
▼Let's Encryptの有効期限
証明書の有効期限は90日です。90日毎に更新作業を再度実施する必要があります。
▼SSL証明書取得コマンドのパラメータ
--webroot | 稼働中 Web サーバーの公開ディレクトリ配下を認証用の一時領域に使用 |
-w | ドキュメントルート |
-d | 証明書を取得したい FQDN |
FQDN (Fully Qualified Domain Name) | ホスト名.ドメイン名を省略なしで表記 |
- ドキュメントルートはバーチャルホストで複数のホスト定義がある場合、該当のホスト定義を指定。
- 証明書を取得したい FQDN が複数ある場合は、-d [証明書を取得したい FQDN] を複数指定。
- 例 : srv.world/dlp.srv.world の二つについて取得する場合。
⇒ [-d srv.world -d dlp.srv.world]
▼ターミナルから取得コマンド実行(初回目)
command # certbot-auto certonly --webroot -w /home/username/public_html -d example.com Bootstrapping dependencies for RedHat-based OSes that will use Python3... (you can skip this with --no-bootstrap) dnf は /usr/bin/dnf です dnf はハッシュされています (/usr/bin/dnf) ▼現在の環境に応じてパッケージが自動でインストールされます メタデータの期限切れの最終確認: 0:19:09 時間前の 2020年10月12日 01時10分50秒 に実施しました。 パッケージ gcc-8.3.1-5.el8.0.2.x86_64 はすでにインストールされています。 パッケージ openssl-1:1.1.1c-15.el8.x86_64 はすでにインストールされています。 パッケージ openssl-devel-1:1.1.1c-15.el8.x86_64 はすでにインストールされています 。 パッケージ redhat-rpm-config-122-1.el8.noarch はすでにインストールされています。 パッケージ ca-certificates-2019.2.32-80.0.el8_1.noarch はすでにインストールされています。 パッケージ python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 はすでにインストールされています。 パッケージ python36-devel-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 はすでにインストールされています。 パッケージ mod_ssl-1:2.4.37-21.module_el8.2.0+494+1df74eae.x86_64 はすでにインストールされています。 依存関係が解決しました。 ================================================================================ パッケージ Arch バージョン Repo サイズ ================================================================================ インストール中: augeas-libs x86_64 1.12.0-5.el8 BaseOS 436 k libffi-devel x86_64 3.1-21.el8 BaseOS 29 k python3-virtualenv noarch 15.1.0-19.module_el8.1.0+245+c39af44f AppStream 100 k アップグレード中: ca-certificates noarch 2020.2.41-80.0.el8_2 BaseOS 391 k 依存関係のインストール中: python3-wheel-wheel noarch 1:0.31.1-2.module_el8.1.0+245+c39af44f AppStream 44 k トランザクションの概要 ================================================================================ インストール 4 パッケージ アップグレード 1 パッケージ ダウンロードサイズの合計: 1.0 M これでよろしいですか? [y/N]: y パッケージのダウンロード: (1/5): python3-wheel-wheel-0.31.1-2.module_el8. 335 kB/s | 44 kB 00:00 (2/5): libffi-devel-3.1-21.el8.x86_64.rpm 933 kB/s | 29 kB 00:00 (3/5): augeas-libs-1.12.0-5.el8.x86_64.rpm 2.5 MB/s | 436 kB 00:00 (4/5): ca-certificates-2020.2.41-80.0.el8_2.noa 10 MB/s | 391 kB 00:00 (5/5): python3-virtualenv-15.1.0-19.module_el8. 448 kB/s | 100 kB 00:00 -------------------------------------------------------------------------------- 合計 736 kB/s | 1.0 MB 00:01 トランザクションの確認を実行中 トランザクションの確認に成功しました。 トランザクションのテストを実行中 トランザクションのテストに成功しました。 トランザクションを実行中 準備 : 1/1 インストール中 : python3-wheel-wheel-1:0.31.1-2.module_el8.1.0+245+c 1/6 インストール中 : python3-virtualenv-15.1.0-19.module_el8.1.0+245+c39 2/6 scriptlet の実行中: python3-virtualenv-15.1.0-19.module_el8.1.0+245+c39 2/6 scriptlet の実行中: ca-certificates-2020.2.41-80.0.el8_2.noarch 3/6 アップグレード中 : ca-certificates-2020.2.41-80.0.el8_2.noarch 3/6 scriptlet の実行中: ca-certificates-2020.2.41-80.0.el8_2.noarch 3/6 インストール中 : libffi-devel-3.1-21.el8.x86_64 4/6 scriptlet の実行中: libffi-devel-3.1-21.el8.x86_64 4/6 インストール中 : augeas-libs-1.12.0-5.el8.x86_64 5/6 scriptlet の実行中: augeas-libs-1.12.0-5.el8.x86_64 5/6 整理 : ca-certificates-2019.2.32-80.0.el8_1.noarch 6/6 scriptlet の実行中: ca-certificates-2020.2.41-80.0.el8_2.noarch 6/6 scriptlet の実行中: ca-certificates-2019.2.32-80.0.el8_1.noarch 6/6 検証 : python3-virtualenv-15.1.0-19.module_el8.1.0+245+c39 1/6 検証 : python3-wheel-wheel-1:0.31.1-2.module_el8.1.0+245+c 2/6 検証 : augeas-libs-1.12.0-5.el8.x86_64 3/6 検証 : libffi-devel-3.1-21.el8.x86_64 4/6 検証 : ca-certificates-2020.2.41-80.0.el8_2.noarch 5/6 検証 : ca-certificates-2019.2.32-80.0.el8_1.noarch 6/6 アップグレード済み: ca-certificates-2020.2.41-80.0.el8_2.noarch インストール済み: augeas-libs-1.12.0-5.el8.x86_64 libffi-devel-3.1-21.el8.x86_64 python3-virtualenv-15.1.0-19.module_el8.1.0+245+c39af44f.noarch python3-wheel-wheel-1:0.31.1-2.module_el8.1.0+245+c39af44f.noarch 完了しました! Creating virtual environment... Installing Python packages... Installation succeeded. Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator webroot, Installer None ▼初回目はメールアドレスの登録が必要です。 Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): user@example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ▼「A」で利用規約に同意 (A)gree/(C)ancel: A ▼質問される [st-mybox title="" fontawesome="" color="#757575" bordercolor="#ccc" bgcolor="#ffffff" borderwidth="2" borderradius="2" titleweight="bold" fontsize="" myclass="st-mybox-class" margin="25px 0 25px 0"] 最初の証明書が正常に発行されたら、次のことを希望しますか。 あなたのメールアドレスを創設者であるElectronicFrontierFoundationと共有してください。 Let’sEncryptプロジェクトとその非営利団体のパートナーCertbotを開発しますか? ウェブを暗号化する私たちの仕事についてあなたにメールを送りたい。 Would you be willing, once your first certificate is successfully issued, to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. [/st-mybox] ▼必要ないので「no」を指定 (Y)es/(N)o: no Obtaining a new certificate Performing the following challenges: http-01 challenge for ilia.jp Using the webroot path /home/ilia/public_html for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/ilia.jp/privkey.pem Your cert will expire on 2021-01-09. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *all* of your certificates, run "certbot-auto renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - 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
- 「IMPORTANT NOTES」の下に「Congratulations」(おめでとう)表示で完了。
- 「/etc/letsencrypt/live/example.com/」配下に証明書が取得されているとアナウンスが表示。
- cert.pem ⇒ SSLサーバー証明書(公開鍵含む)
- chain.pem ⇒ 中間証明書
- fullchain.pem ⇒ cert.pem と chain.pem が結合されたファイル
- privkey.pem ⇒ 公開鍵に対する秘密鍵
取得したSSL証明書をサーバーに更新
- 有効期限が 30日未満の証明書を全て更新
- 有効期限の残り日数に関わらず更新したい場合は「--force-renew」を合わせて指定
▼更新処理を実高
command # certbot-auto renew --force-renew Saving debug log to /var/log/letsencrypt/letsencrypt.log (デバッグログを/var/log/letsencrypt/letsencrypt.logに保存します) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/ilia.jp.conf (/etc/letsencrypt/renewal/ilia.jp.confを処理しています) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Plugins selected: Authenticator webroot, Installer None (選択したプラグイン:Authenticator webroot、Installerなし既存の証明書の更新) Renewing an existing certificate (既存の証明書の更新) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - new certificate deployed without reload, fullchain is /etc/letsencrypt/live/ilia.jp/fullchain.pem (新しい証明書はリロードなしで展開され、フルチェーンは/etc/letsencrypt/live/ilia.jp/fullchain.pem) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations, all renewals succeeded. The following certs have been renewed: (おめでとうございます、すべての更新は成功しました。次の証明書が更新されました。) /etc/letsencrypt/live/ilia.jp/fullchain.pem (success) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
最後に「success」となれば完了です。
SSLを有効にするライブラリのインストール
command # dnf -y install mod_ssl
SSL証明書取得コマンドを実行(2回目以降)
2回目以降は特に何も聞かれずに完了します。
▼SSL証明書の取得コマンド実行
command # certbot-auto certonly --webroot -w /home/username/public_html -d example.com Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator webroot, Installer None Obtaining a new certificate Performing the following challenges: http-01 challenge for example.com Using the webroot path /home/www/public_html for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2021-01-09. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *all* of your certificates, run "certbot-auto renew" - 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
▼上記取得済のSSLをサーバーに更新
command # certbot-auto renew
Apacheコンフィグ(ssl.conf)の修正
httpd.conf でバーチャルドメインの設定しているときは、下記 ssl.conf で管理すると、複数のファイルで管理することになり煩雑になるため管理がし辛くなります。
ssl.conf からバーチャルドメイン関連の設定をコメントアウトして無効にした後、httpd.conf側にSSLのバーチャルドメインの設定を記述します。
command # cp /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/BK_ssl.conf # vi /etc/httpd/conf.d/ssl.conf
ssl.conf
▼下記をコメントアウト
Listen 443 https
↓
#Listen 443 https
▼さらに下記の間の設定も全てコメントアウト
#<VirtualHost _default_:443>
:
:
#</VirtualHost>
Apacheコンフィグ(httpd.conf)の修正
command vi /etc/httpd/conf/httpd.conf
メモ
▼下記は追記済みの場合
NameVirtualHost 192.168.0.10
ServerAdmin info@example.com
DocumentRoot /home/username/public_html
ServerName example.com
ErrorLog logs/error_log
CustomLog logs/access_log common
SetEnvIf Request_URI "\.(gif)|(jpeg)|(jpg)|(png)|(ico)$" no_log
▼https用の設定を追記
pemファイル等は先ほど「certbot」コマンドで作成したフォルダパスを指定。
ServerAdmin info@example.com
DocumentRoot "/home/username/public_html"
ServerName ilia.jp:443
ErrorLog logs/ssl-ilia.jp.com-error_log
CustomLog logs/ssl-ilia.jp-access_log combined env=!no_log
SSLCertificateFile /etc/letsencrypt/username/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/username/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/username/example.com/chain.pem
SSLProtocol all -SSLv2 -SSLv3
SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
SSLHonorCipherOrder on
Header always set Strict-Transport-Security "max-age=15768000"
▼コンフィグの反映
command # systemctl restart httpd
状態確認
▼mod_sslがApacheに登録されているか確認
command # httpd -M | grep ssl AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/httpd/conf/httpd.conf:391 ssl_module (shared)
ssl_moduleが表示されていれば問題なし。
▼SSL証明書の有効期限を調べる
command # openssl s_client -connect example.com:443 | openssl x509 -noout -enddate depth=2 O = Digital Signature Trust Co., CN = DST Root CA X3 verify return:1 depth=1 C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3 verify return:1 depth=0 CN = ilia.jp verify return:1 notAfter=Jan 9 17:33:45 2021 GMT
「2021年1月9日」まで有効。
▼ブラウザから下記で問題なくアクセスできることを確認
・http://example.com/
・https://example.com/
httpdを起動時にエラーが表示された場合
エラー例1
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::2:76ff:fe1b:26f5. Set the 'ServerName' directive globally to suppress this message
ssl_module (shared)
この場合は、「httpd.conf」にて「ServerName」が設定されていない時に表示。
エラー例2
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/httpd/conf/httpd.conf:391
ssl_module (shared)
次のApacheのリリースでVirtualHostは書かなくても動作するという意味。
今回の場合だとhttpd.confの記述は下記が該当。
「NameVirtualHost 192.168.0.50」
cronでSSL証明書の自動更新
2回目以降のSSL証明書の更新は、コマンド1行で実行後にhttpdを再起動するだけで簡単に更新が行えます。
しかし、更新を忘れる事が多いと思うので、cronに登録して自動化したいと思います。
cronに登録する処理
- 毎月1日の05:00にcronを実行
- SSL証明書の自動更新
- Apacheの再起動
- cronの実行結果をメールで送信
▼cronファイルの編集
command # vi /etc/crontab
crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
▼結果をメールで送信
MAILTO="info@example.com"
▼毎月1日の05:00にcronを実行
00 5 1 * * root /usr/local/bin/certbot-auto renew && systemctl restart httpd
もっと詳しく
・certbot-auto renew コマンドで更新
・&& で最初のコマンドが正常なら次のコマンドを実行
・systemctl restart httpd でApacheを再起動
-
-
CentOS 8にてcron を利用して処理の時限設定を行う
概要 cron(クローン)はLinuxやMac等のUNIX系のOSに入っているプログラムです。 「この日時に、このプログラムを動かして!」と指示を出しておくと、その時間になったときに指定しておいたプロ ...
続きを見る
メールでcronの実行結果を確認
certbot-auto renew の実行後は設定したメールアドレスにログが出力されます。
実際に更新が完了した際は下記のように success と表示されます。
SSL証明書更新完了
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/example.com/fullchain.pem (success)
更新直後に再度 certbot-auto renew を実行した際などは下記のように skipped となり表示され、更新は行われません。
尚、certbot-auto renew のコマンド自体は正常に走っているので、「&&」でつないだ2つ目のコマンドであるApacheの再起動も実行されます。
SSL証明書更新スキップ
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certs are not due for renewal yet:
/etc/letsencrypt/live/aki-e.com/example.com expires on 2021-04-12 (skipped)
No renewals were attempted.