MENU
カテゴリー
アーカイブ

SSL/TLS サーバー証明書

DDNSでもhttps://でアクセスしたいことはある。いつもGoogle Chromeアドレスバーの���キュリティ警告「保護されていない通信」は気にしていなかったけど、そろそろ対策しようか。

以前は自鯖で「なんちゃって認証局」を設置したこともあったけど、今回はLet's Encryptを導入することにした。

詳しくはこの辺りで。


目次

ルーターの設定

バッファローのAirStation WSR-2533DHP2の場合は次のとおり。
管理画面にログインして、

  • セキュリティーの「IPフィルター」で、Internetの全てのアドレスからの通信について、LAN内サーバー(のアドレス)に向けてTCPポート:443を通過とする。
  • セキュリティーの「ポート変換」で、エアステーションのInternet側IPアドレスの TCPポート:443について、LAN内サーバーのTCPポート:443にマッピングする。

続いて、Windows Defenderで内部向け通信に対するTCPポート:443を許可する。

Let's Encryptのインストール

aptでインストールできる。CertbotのApache用プラグインもインストールする。

$ sudo apt install letsencrypt python-certbot-apache

実は、最初はプラグインpython-certbot-apacheの存在を知らずインストールしなかったから、$ sudo certbotで証明書を取得してしまった。

以下は、やりなおし(2度目)以降の記録。

certbotコマンドを実行する。

例はexample1.jpとexample2.jpの2つを運用していた場合。
前もって、http//でアクセスするVirtualHostの設定ファイルは01-virtual.confと02-virtual.confとに分けていると後々都合がよろしい。

$ sudo certbot
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: example1.jp
2: example2.jp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<span class="bold-red">対象ドメインを選択</span>
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): <span class="bold-red">1</span>
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example1.jp
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/01-virtual-le-ssl.conf
Deploying Certificate to VirtualHost /etc/apache2/sites-available/01-virtual-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/01-virtual-le-ssl.conf

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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<strong><span class="bold-red">HTTP://example1.jpをhttps://example1.jpにリダイレクトする設定を01-virtual.confに追加する。</span></strong>
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): <span class="bold-red">2</span>
Redirecting vhost in /etc/apache2/sites-enabled/01-virtual.conf to ssl vhost in /etc/apache2/sites-available/01-virtual-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://example1.jp

You should test your configuration at:
!https://www.ssllabs.com/ssltest/analyze.html?d=example1.jp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example1.jp/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example1/privkey.pem
   Your cert will expire on 2020-02-01. 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"
 - 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

$

初回実行時にメールアドレスの入力箇所があったときの記録は以下のとおり。

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): <span class="bold-red">hogehoge@example.jp</span>

当然、同意する。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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:<span class="bold-red">A</span>

メールを受け取る。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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:<span class="bold-red">Y</span>

届いたメールで確認完了する。パッと見は迷惑メールっぽいので注意。

差出人:Aaron Jue membership@eff.org
件名:Please Confirm Your EFF Subscription


mod_sslの有効化

Apacheの初期設定では有効になってない。/etc/apache2/mods-enabled/の中に、次のコマンドでシンボリックを設定する。

$ sudo a2enmod ssl

念のため、ポート443に接続できるかtelnetで確認する。Connected to ドメインが表示されたらOK。

$ telnet example1.jp 443
Trying 192.*.*.*...
Connected to example1.jp.
Escape character is '^]'.

確認

/etc/apache2/sites-available/01-virtual.confには、リダイレクト用に次の行が追加されているハズ。

RewriteEngine on
 RewriteCond %{SERVER_NAME} =example1.jp
 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

最後に念のためApacheのリスタート。

$ sudo service apache2 restart

Search-Replace-DB-master

内部リンク(http://example.jp)を一括変換する。WordPressを引っ越しするときにいつもお世話になっているスクリプト。

途中、何度か試行錯誤したけど、example1.jpに関して最終的に有効化されるApache設定ファイルは2つ。

/etc/apache2/sites-enabled/01-virtual-le-ssl.conf <span class="bold-red">・・・ポート443用</span>
/etc/apache2/sites-enabled/01-virtual.conf <span class="bold-red">・・・ポート80用(自動でリダイレクト追加済み)</span>

example2.jp��同じように$ sudo certbotを実行してApacheを再起動すればOK。Apacheのconfファイルは自動で有効化される。2回目以降の起動だからメールアドレスは聞かれない。

これでhttps化は完了。あとはcronで証明書の更新を自動化するだけかと思ったら、すでに/etc/cron.d/certbotがあった。

至れり尽くせりだ。

コメント

コメントする

目次