記事ページを表示中

HTTPS通信について|サーバー証明書にはLet’sEncryptが便利

記事No.87

更新日時2023年03月10日

今回は、HTTPS通信について勉強するよ!

スカーレット

バイオレットちゃん

HTTPS通信を理解すると、どういったメリットがあるのですか?

HTTPS通信はセキュリティに関する技術で、HTTPS通信を導入する事でユーザーが安心して私たちのWEBサイトを利用する事が出来るよ。

スカーレット

バイオレットちゃん

それって凄い!ユーザーは安心して利用できるので、リピーターにもなってくれやすいという事ですね。

HTTPS通信は、以下2つのプロトコルを使用して通信を行っています。

  1. SSL(SecureSocketLayer)/TLS(TransportLayerSecurity)プロトコル
  2. HTTPプロトコル

ウェブサイトのドメイン名の前に付いているhttps://は、HTTPS通信によりデータを送受信しているサイトである事を示しています。

以前までであれば、ウェブサイトの多くは、HTTPプロトコルのみを使用してデータを送受信していましたが、現在は、データ漏洩を防ぐ為、HTTPプロトコルとSSL/TLSプロトコルをあわせて使用したHTTPS通信をGoogleも推奨しています。

GoogleChromeやSafariなどのブラウザでHTTPプロトコルのみで通信を行っているサイトにアクセスすると、

「このサイトは安全ではありません」

といった文字が画面に表示されて、サイトにアクセスできません。

バイオレットちゃん

私も、この表示を見た事があるわ。安全では無いサイトには恐くてアクセスできないわ…

ネット検索をすると、まだHTTPプロトコルで通信しているWEBサイトがあるよね。

スカーレット

バイオレットちゃん

これから新しくサイトを作る時には、HTTPSプロトコルでの通信が必須になりますね。

SSL/TLSプロトコル

通信を安全に行う為のプロトコルとして、SSLプロトコルがありますが、現在主流となっているのは、TLSプロトコルです。

TLSプロトコルは、SSLプロトコルの後継として作成されたという事もあり、TLSプロトコルもSSLと総称されています。

ですので、SSL通信といった場合、正確にはTLSプロトコルを使用したSSL通信という事になります。

私たちがブラウザを介してサイトにアクセスすると、HTTP通信が行われる前に、SSL通信が行われます。

SSL通信をして、対象のサイトが可動しているウェブサーバーの安全性を確認します。

ウェブサーバーの安全性を証明するには各サーバーにサーバー証明書を設定します。

ウェブサーバーは、リクエストがあったブラウザにサーバー証明書を提示して、自身が安全である事を証明します。

コメントフォーム

サーバー証明書の取得

サーバー証明書は、認証局(CA:CertificationAuthorith)が発行しています。

サーバー証明書は基本的に有料で、取得方法も認証局によって異なります。

ただ、無料でサーバー証明書を発行してくれる認証局としてLet’sEncryptがあります。

通常、サーバー証明書を取得し、サーバーに設定するのには時間がかかりますが、Let’sEncryptのサービスを使用すると、簡単にサーバー証明書を設定する事ができます。

バイオレットちゃん

Let’sEncryptが無かったら、サイトを作るのに、もっと費用がかかっていたのね。

コメントフォーム

Let’sEncryptでサーバー証明書を取得する

Let’sEncryptでサーバー証明書を取得するのに役立つのが、Certbotと呼ばれるパッケージです。

Certbotがサーバー証明書の取得と設置を行ってくれます。

Certbotは、使用しているOSとウェブサーバーの組み合わせによって設定方法が異なりますが、Certbotの公式サイトで各々の設定手順についても解説されています。

今回、私が試したのは、Ubuntu20とNginxの組み合わせです。

まずは、ターミナルを開き、ubuntuのパッケージマネージャーより、certbotをインストールします。

$ sudo apt install certbot python3-certbot-nginx

次に、nginxの設定ファイルを編集します。

ここでは設定ファイルをdefaultとしていますが、実際はSSL化したいドメインの設定ファイルを編集します。

$ sudo vi /etc/nginx/sites-available/default

server_nameにsslを発行するドメイン名を指定します。

#server_name_;
server_name example.com;

記載内容がnginxのルールに抵触していないか確認します。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

「successful」が確認できました。記載内容に問題はないようです。nginxをリスタートします。

$ sudo service nginx restart

これでcertbotのインストールが完了しました。続いて、インストールしたcertbotを使用してSSL証明書を取得します。

$ sudo certbot --nginx -d hoge.com

メールアドレスの入力や、規約の確認を行います。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [メールアドレス]を入力します。
Please read the Terms of Service at

404 Page not found -  Let's Encrypt
 Let's Encrypt is a free, automated, and open certificate authority brought to you by the nonprofit Internet Security Research Group (ISRG). 

letsencrypt.org
 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]を入力します。certbotに関しての連絡にメールアドレスを使用。
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/default

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): [2]を入力します。HTTPにアクセスがあった場合でも自動でHTTPSに振り分ける設定になります。
Future versions of Certbot will automatically configure the webserver so that all requests redirect to secure HTTPS access. You can control this behavior and disable this warning with the --redirect and --no-redirect flags.

Congratulations! You have successfully enabled

Example Domain

www.example.com


You should test your configuration at:

SSL Server Test (Powered by Qualys SSL Labs)
A comprehensive free SSL test for your public web servers.

www.ssllabs.com
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.example.com/privkey.pem
Your cert will expire on 2021-09-11. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot 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

nginxに設定した以下のファイルを見ると、certbotが自動でファイルを編集してくれているのが分かります。これで、https接続ができるようになっています。

/etc/nginx/sites-available/default

コメントフォーム

サーバー証明書を自動更新する

サーバー証明書には有効期限があり、期限が切れると、SSL通信が出来なくなります。

certbotは、自動でサーバー証明書を更新してくれます。

$ sudo systemctl status certbot.timer

●certbot.timer – Run certbot twice daily

と表示されれば、自動更新をする設定となっています。

以下のコマンドで自動更新のテストが可能です。

$ sudo certbot renew --dry-run

「Congratulations, all renewals succeeded.」と表示されれば問題ありません。

certbotは凄く便利なパッケージですので、もし、サイトのSSL化が思うように進まない場合は、certbotを試されると良いです。

スカーレット

コメントフォーム

著者情報

名前:スカーレット
2010年からWEBサイトやWEBアプリを作成しています。最初は趣味でブログを書いていましたがSEOを勉強するのが楽しくなり、そのままブロガーとして独立しました。その後、記事を書くだけでは物足りなくなり自分でWEBアプリの作成をスタート。現在はブロガー兼プログラマーとして活動しています。このWEBアプリ(ブロトーク)もDjangoで自作しました。ブロトークはブログとSNSを合体させたようなWEBアプリです。ブログを読んで気づいた事や感想などあれば、気軽にメッセージを送って頂ければと思います。WEB技術を一緒に勉強していけたらと思います。

関連記事

ポート番号とは何か

投稿No.80

投稿日March 6, 2023