近年、https化されたWEBサイトが標準的になって来ています。
https化は、SEOの評価向上の効果があったり、jsのgetUserMedia()など、https化された限定のメソッドを使えたりと、大きなメリットがあります。
ただし、https化には従来、ssl証明書を数万円かけて購入し、自分のWEB空サイトを証明する必要がありました。
Let’s Encrypt は無料でオープンな認証局 (CA) で、これを利用することで無料で https化を実現できます。
今回、下記の環境で Let’s Encryptの証明書導入を試してみます。
OS: Ubuntu/16.0.4
WEBサーバ: nginx/1.15.8
letsencrypt をインストール
まずは必要なコマンドをインストールします。
他の記事では certbot コマンドをインストールしている例が多かったのですが、インストールの難易度がやや高いので、特別な場合を除いて letsencrypt コマンドを利用するほうが楽に進めると思います。
sudo apt-get install letsencrypt
nginxを停止
letsencrypt が一時的に使用するポート(80番)が競合するので、nginxを停止しておきます。
sudo systemctl stop nginx
次に letsencrypt コマンドを利用して証明書を発行します。
それぞれオプションの意味は下記になります。
- certonly: 証明書の生成のみ実行。(サーバ設定は行わない)
- standalone: WEBサーバとして letsencrypt に内包されるサーバを使用。(nginx や apacheを利用しない)
- -d <https化するドメイン>: https化したいドメインを任意に指定してください。
コマンド入力後、メールアドレスの入力を促されるので入力してください。
sudo letsencrypt certonly --standalone -d 《https化するドメイン》
完了後下記のメッセージが表示され、太字下線部分に証明書のパスが確認できます。
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/hoge.example.com/fullchain.pem. Your cert
will expire on 2016-11-11. 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 lose your account credentials, you can recover through
e-mails sent to hogehoge@example.com.
- 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の設定
上記で確認した 証明書のパスを nginxの設定に反映させます。
server { listen 80; server_name face.walker.co.jp; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name hoge.example.com ssl on; ssl_certificate /etc/letsencrypt/live/hoge.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/hoge.example.com/privkey.pem; charset utf-8; ..以下 DocumentRoot等、任意の設定を記載 }
Nginxの再起動
この後、nginxを再起動すれば設定は完了です。
sudo systemctl restart nginx
https://《ドメイン名》 にアクセスし、動作を確認してみましょう。
エラー画面が表示される場合は /var/log/nginx/error.log などを確認し、表記通りに修正していくと良いでしょう。
【参考】
https://qiita.com/k-yamada-github/items/7314003de7bdcbb2d39b
https://villesalonen.fi/2015/12/30/lets-encrypt/
https://qiita.com/HeRo/items/f9eb8d8a08d4d5b63ee9