- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2021-06-07T01:20:21+00:00","default:mizutu","mizutu")
#author("2021-06-27T06:59:34+00:00","default:mizutu","mizutu")
[[忘備録]] > サイトSSL化
* 自宅サーバー構築 サイトSSL化 [#d6be8327]
| Linuxによるサーバー構築メモ サイトSSL化|
Ubuntu 20.04LTS で構築した WwbサーバーをLet's Encryptのサーバ証明書を利用して SSL化する。~
#contents
RIGHT:&size(12){※ 最終更新:2021/06/07 };
* Ubuntu 20.04LTS サーバー [#qefe5ddf]
Ubuntu 20.04 LTSのサーバー版インストールイメージ、「ライブインストーラー版」~
モジュール名:ubuntu-20.04.2-live-server-amd64.iso~
** Let's EncryptのSSL/TSLサーバ証明書の取得と更新設定 [#lf37ea19]
*** 事前準備 [#c9238688]
- certbot をインストール~
#codeprettify(){{
$ sudo apt update
:
$ sudo apt install certbot
Reading package lists... Done
:
Setting up certbot (0.40.0-1ubuntu0.1) ...
Created symlink /etc/systemd/system/timers.target.wants/certbot.timer → /lib/systemd/system/certbot.timer.
Processing triggers for man-db (2.9.1-1) ...
}}
*** SSL/TLSサーバ証明書の取得 [#b857be04]
- certbot の基本コマンド
#codeprettify(){{
$ sudo certbot certonly --webroot -w /var/www/html -d izutsu.aa0.netvolante.jp/
$ sudo certbot certonly --webroot -w /var/www/html -d izutsu.aa0.netvolante.jp
}}
-- certonly はサブコマンドのひとつで、証明書の取得のみ。何も書かない場合はデフォルトのrunで動作し、証明書の取得と同時にサーバソフトウェアへのインストールまで実行する。~
-- webroot オプションはサーバを動かしたままの状態で証明書を取得する~
-- w オプションはルートディレクトリを指定する。~
-- d オプションはフルドメインを指定する。~
- 証明書の取得~
#codeprettify(){{
$ sudo certbot certonly --webroot -w /var/www/html -d izutsu.aa0.netvolante.jp/
$ sudo certbot certonly --webroot -w /var/www/html -d izutsu.aa0.netvolante.jp
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): izutsum@venus.dti.ne.jp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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)gree/(C)ancel: a
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing 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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for izutsu.aa0.netvolante.jp
Using the webroot path /var/www/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/izutsu.aa0.netvolante.jp/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/izutsu.aa0.netvolante.jp/privkey.pem
Your cert will expire on 2021-09-04. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot 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
}}
-- 途中で使用可能なメールアドレスの入力と利用条件への同意をする。~
-- /etc/letsencrypt/live/hoge.example.com/以下に証明書が配置される。~
・ 途中で使用可能なメールアドレスの入力と利用条件への同意をする。~
・ /etc/letsencrypt/live/hoge.example.com/以下に証明書が配置される。~
・cert.pem…公開鍵を含むSSLサーバ証明書
・chain.pem…中間証明書
・fullchain.pem…cert.pemとchain.pemが結合されたファイル
・privkey.pem…公開鍵に対応する秘密鍵
#codeprettify(){{
$ sudo ls -l -s /etc/letsencrypt/live/izutsu.aa0.netvolante.jp
total 4
0 lrwxrwxrwx 1 root root 48 Jun 6 05:00 cert.pem -> ../../archive/izutsu.aa0.netvolante.jp/cert1.pem
0 lrwxrwxrwx 1 root root 49 Jun 6 05:00 chain.pem -> ../../archive/izutsu.aa0.netvolante.jp/chain1.pem
0 lrwxrwxrwx 1 root root 53 Jun 6 05:00 fullchain.pem -> ../../archive/izutsu.aa0.netvolante.jp/fullchain1.pem
0 lrwxrwxrwx 1 root root 51 Jun 6 05:00 privkey.pem -> ../../archive/izutsu.aa0.netvolante.jp/privkey1.pem
4 -rw-r--r-- 1 root root 692 Jun 6 05:00 README
}}
- 証明書のバックアップ
#codeprettify(){{
$ sudo cp -r /etc/letsencrypt/ /etc/letsencrypt_cp/
}}
*** Let's Encryptサーバ証明書の自動更新 [#t1c5afc5]
- 自動更新のテスト
#codeprettify(){{
$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/izutsu.aa0.netvolante.jp.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for izutsu.aa0.netvolante.jp
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/izutsu.aa0.netvolante.jp/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/izutsu.aa0.netvolante.jp/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- 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.
}}
- Let's Encryptより発行されるサーバ証明書の有効期間は90日間なので自動更新を設定しておく。~
- crontab で毎日0:00に更新を実行するように設定。~
&color(darkorange){※ 期限切れの直前に更新するようにとの記述があるサイトもあるが、certbot内部でも証明書の有効期限をチェックして、更新不要であれば更新作業を行わないようにしているので、Let's Encryptが毎日実行するという一番シンプルな方法が推奨されている。};~
#codeprettify(){{
$ sudo crontab -e
no crontab for root - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /bin/ed
Choose 1-4 [1]: 2
エディタを選んで最下行に下記1行追加
0 0 * * * certbot renew
:wq で書き込み終了
crontab: installing new crontab
$ sudo service cron restart
$ sudo ls -l /var/spool/cron/crontabs
total 4
-rw------- 1 root crontab 1114 Jun 6 06:00 root
}}
- 後日、ログから実行結果を確認しておく。~
#codeprettify(){{
$ sudo ls -ltr /var/log/letsencrypt
total 92
-rw-r--r-- 1 root root 88423 Jun 6 05:56 letsencrypt.log
$ sudo cat /var/log/letsencrypt/letsencrypt.log
:
:
-----END CERTIFICATE-----
2021-06-06 05:56:28,589:DEBUG:acme.client:Storing nonce: 00032l2Sk6PPE37AKxLX5LgNs2Cil9rOm2m5frrWUxBoAOY
2021-06-06 05:56:28,591:DEBUG:certbot.renewal:Dry run: skipping updating lineage at /etc/letsencrypt/live/izutsu.aa0.netvolante.jp
2021-06-06 05:56:28,595:DEBUG:certbot.updater:Skipping updaters in dry-run mode.
2021-06-06 05:56:28,596:DEBUG:certbot.renewal:no renewal failures
2021-06-06 13:38:00,597:DEBUG:certbot.main:certbot version: 0.40.0
2021-06-06 13:38:00,598:DEBUG:certbot.main:Arguments: ['-q']
2021-06-06 13:38:00,598:DEBUG:certbot.main:Discovered plugins: PluginsRegistry(PluginEntryPoint#manual,PluginEntryPoint#null,PluginEntryPoint#standalone,PluginEntryPoint#webroot)
2021-06-06 13:38:00,614:DEBUG:certbot.log:Root logging level set at 30
2021-06-06 13:38:00,614:INFO:certbot.log:Saving debug log to /var/log/letsencrypt/letsencrypt.log
2021-06-06 13:38:00,628:DEBUG:certbot.plugins.selection:Requested authenticator <certbot.cli._Default object at 0x7fcc04921d00> and installer <certbot.cli._Default object at 0x7fcc04921d00>
2021-06-06 13:38:00,642:INFO:certbot.renewal:Cert not yet due for renewal
2021-06-06 13:38:00,643:DEBUG:certbot.plugins.selection:Requested authenticator webroot and installer None
2021-06-06 13:38:00,643:DEBUG:certbot.renewal:no renewal failures
}}
** サイトの SSL化 [#d0bdf38f]
*** Apache2 の設定 [#o741fe9b]
+ SSLに関するモジュールを有効化する~
#codeprettify(){{
$ sudo a2enmod ssl
}}
+ 設定ファイルを編集 (以下修正箇所)~
#codeprettify(){{
$ sudo vi /etc/apache2/sites-available/default-ssl.conf
(管理者のメールアドレスへ修正)
ServerAdmin = izutsum@venus.dti.ne.jp
(ルートディレクトリとして公開するディレクトリのパスへ修正)
DocumentRoot = /var/www/html
(取得したサーバ証明書と公開鍵のパスに変更)
SSLCertificateFile /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/cert.pem
(取得した秘密鍵のパスに変更)
SSLCertificateKeyFile /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/privkey.pem
(コメント解除して取得した中間証明書のパスに変更)
SSLCertificateChainFile /etc/letsencrypt/live/izutsu.aa0.netvolante.jp/chain.pem
}}
+ サイト設定を有効化する~
#codeprettify(){{
$ sudo a2ensite default-ssl
}}
+ Apache2 の再起動~
#codeprettify(){{
$ sudo systemctl restart apache2
}}
+ ブラウザより「https://izutsu.aa0.netvolante.jp」に接続。WEBページが表示されれば完了~
*** HTTP接続に対してHTTPSへのリダイレクトをかける [#h08e3432]
+ Apache2のRewriteEngine はオプションなので、有効にしないと動作しない~
-- 現在の状態を確認する~
~
● 現在の状態を確認する~
#codeprettify(){{
$ sudo ls /etc/apache2/mods-enabled
access_compat.load authn_file.load autoindex.load env.load mpm_prefork.load reqtimeout.conf ssl.conf
alias.conf authz_core.load deflate.conf filter.load negotiation.conf reqtimeout.load ssl.load
alias.load authz_host.load deflate.load mime.conf negotiation.load setenvif.conf status.conf
auth_basic.load authz_user.load dir.conf mime.load php7.4.conf setenvif.load status.load
authn_core.load autoindex.conf dir.load mpm_prefork.conf php7.4.load socache_shmcb.load
}}
-- mod_rewrite を有効にする~
● mod_rewrite を有効にする~
#codeprettify(){{
$ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
}}
-- Apache2の再起動~
● Apache2の再起動~
#codeprettify(){{
$ sudo systemctl restart apache2
}}
-- 有効化を確認する~
● 有効化を確認する~
#codeprettify(){{
$ sudo ls /etc/apache2/mods-enabled
access_compat.load authn_file.load autoindex.load env.load mpm_prefork.load reqtimeout.conf socache_shmcb.load
alias.conf authz_core.load deflate.conf filter.load negotiation.conf reqtimeout.load ssl.conf
alias.load authz_host.load deflate.load mime.conf negotiation.load rewrite.load ssl.load
auth_basic.load authz_user.load dir.conf mime.load php7.4.conf setenvif.conf status.conf
authn_core.load autoindex.conf dir.load mpm_prefork.conf php7.4.load setenvif.load status.load
}
}}
+ 設定ファイルに下記のように 3行を Virtualhost 内最下部に追記~
#codeprettify(){{
$ sudo vi /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
:
:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
}}
+ Apache2の再起動~
#codeprettify(){{
$ sudo systemctl restart apache2
}}
*** サイト内コンテンツの修正 [#bf38bab6]
- サイト内で自身のページへのアクセスが 「http://izutsu.aa0.netvolante.jp」となっている記述があれば「https://izutsu.aa0.netvolante.jp」に変更。~
- ブラウザのアドレス欄に鍵マークが出るようになれば完了。~
** 更新履歴 [#q43750f5]
- 2021/06/07 初版
#br
* 参考資料 [#y2001537]
- [[UbuntuでLet's Encryptの無料SSL/TLSサーバ証明書を取得する>+https://chee-s.net/ubuntu%E3%81%A7lets-encrypt%E3%81%AE%E7%84%A1%E6%96%99ssl-tls%E3%82%B5%E3%83%BC%E3%83%90%E8%A8%BC%E6%98%8E%E6%9B%B8%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B]]~
- [[RewriteEngine On にならない場合は、mod_rewrite を有効にする>+https://qiita.com/YAJIMA/items/68de1bdeb71a921a718d]]~
- [[無料SSL証明書を利用して簡単な設定で自動的にサイトをHTTPS化>+https://hexgleam.com/jp/blog/enable-https-with-lets-encrypt-certbot/]]~
- [[SSL Checker>+https://www.sslshopper.com/ssl-checker.html]]~
#br