LoginSignup
5
5

More than 3 years have passed since last update.

Lightsail+Nodejs+Express環境にHTTPSを導入(Let's Encrypt)

Last updated at Posted at 2019-01-13

今回は、先般導入したLightsail上のNodejs Express環境をHTTPS化します。
色々なドキュメントがあるものの、よくある「書いてある通りに実行してもエラーが出てしまう」が発生したためです。
誰かのトラブル解決になれば幸いです。

HTTPS化には、無料でSSL証明書が発行出来るLet's Encryptを使用します。(無料だからといって有料のものに劣る訳ではありません ※個人で使用する分には)

以下のサイトを参考にしました。
良い感じにHTTPS対応したexpress(Node.js)サーバを立てる

まずはopensslを最新に。
sudo apt upgrade openssl

参考サイトでは、鍵の生成時にデフォルトのアルゴリズムを変更し「強くて速い」アルゴリズムで鍵を生成しています。どちらにするかはお任せしますが、とにかく鍵を生成します。

sudo openssl ecparam -name prime256v1 -genkey -out server.key

続いてCSRを作成します。
openssl req -new -sha256 -key server.key -out server.csr
上記のコマンドを実行すると下記のようないくつかの質問が出てきます。

Country Name (2 letter code) [AU]:

内容の詳細は
Apache + OpenSSL CSR生成手順 (新規)
辺りを参照して頂くと良いと思います。
※私はCountry NameをJPにし、Common Nameに取得したドメイン名を入力した以外は全てblank(空欄)にしました。

ここまででCSRが生成できましたので、以下を参照してLet's Encryptクライアントのインストールをします。
Let's Encryptクライアントのインストール
私の環境のLightsailはubuntu16.04環境なのですが、
sudo apt-get install letsencrypt python-letsencrypt-apache
以下で進めていくと上手くいきませんでした。
理由は分かりませんでしたが、「その他の UNIX 系 OS環境のインストール」で上手く動作しましたので、そちらの方法を記載します。

※2019年11月8日追記
仕様変更により、下記の方法で進める事が出来ないようになっています。
現在はバグ修正などにより、
sudo apt-get install letsencrypt python-letsencrypt-apache
で進める事が出来るようになっていますので、そちらで進めましょう。
下記の方法でのインストールではエラーが出ます。
※2019年11月8日追記終わり

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
sudo ./certbot-auto

これでクライアントのインストールが完了しました。
次に、先ほど作成したserver.csrを使って証明書を発行します。
./certbot-auto certonly --csr ./server.csr
ここでもいくつか質問が飛んできます。

How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Spin up a temporary webserver (standalone)
2: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

1を選択します。環境によって選択肢が増えたり前後します。(standalone)と書かれている選択肢を選びます。

Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 

期限切れの通知が来ますので、普段使っているメールアドレスを入力

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

利用規約に同意(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: 

(n)を選択しました。

Problem binding to port 80: Could not bind to IPv4 or IPv6.

ここまで進めるとエラーが発生しました。
確認すると、他がポートを占有していると発生するエラーのようですので、
他プロセスがポートを占有してhttpdを再起動できない
を参考にプロセスをkillします。httpsデーモンが起動していると思います。

再度コマンド実行でエラーが発生しなければ以下が生成されます。
0000_cert.pem
0000_chain.pem
0001_chain.pem

あとは参考サイトの通りにファイルを移動し、expressを実行します。
これでHTTPS環境が構築できました。

私は更に、下記サイトを参照してhttpへのアクセスをhttpsにリダイレクトする処理を追加しました。
Node.js + Express で HTTP から HTTPS へ 自動リダイレクト させる方法

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5