Node.jsとはJavascript言語をブラウザ上ではなくサーバ上で動かせるようにしたものです。PHP同様Webサーバがなくてもその言語単体でサーバ上で動作するので、バッチ処理なども可能です。しかし、最も使われているのがWebサーバとして動作させる方法です。フロントエンドとバックエンドの両方で同じJavascript言語を使用することができたらとても便利ですよね。今回はそのNode.jsの設定について学習していきます。まずはインストール手順を確認し、それからNode.jsをWebサーバとして使用する方法を確認していきます。
通常はApacheやNginxサーバをフロントエンドサーバとしてプロキシ設定をしてNode.jsサーバをバックエンドに置いて連携するのが一般的ですが、ここでは練習としてNode.jsだけでHTTPS化までの手順を確認していきます。また、Node.jsサーバはバックエンドの場合、80番とと443番ポートは通常は使用しませんが、ここではフロントエンドサーバとするので80番とと443番ポートを使用していきます。
本記事はEC2インスタンスにSSH接続できていることを前提に解説をしていきます。学習がまだの方は先に以下の記事の学習を済ませてから戻ってきてください。

Node.jsのインストール手順
NVMのインストール
Node.jsをインストールする為には先にNode Version Manager(NVM)のインストールをしておきます。NVMを使うと複数のNode.jsのバージョンを管理することができ、バージョンのスイッチをすることができます。
【ec2-userで以下を実行】
【EC2ユーザのホームディレクトリに移動】
cd /home/ec2-user
【.bashrcファイルをコピーバックアップ】
cp -ip .bashrc .bashrc_bkup
【.nvmディレクトリがまだ作成されていないことを確認】
ll -a /home/ec2-user | grep .nvm
【NVMのインストール】
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
> 以下実行例
--
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 16555 100 16555 0 0 382k 0 --:--:-- --:--:-- --:--:-- 384k
=> Downloading nvm as script to '/home/ec2-user/.nvm'
=> Appending nvm source string to /home/ec2-user/.bashrc
=> Appending bash_completion source string to /home/ec2-user/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
--
> シェルのスタートアップスクリプト(.bashrsファイル)にNVMのセットアップコードが追記されます。
> また、ホームディレクトリに.nvm/ ディレクトリが作成されます。
> コマンドの表示通りになっているか確認してみます。
【.bashrcファイルの変更確認】
diff .bashrc .bashrc_bkup
--
28,31d27
<
< export NVM_DIR="$HOME/.nvm"
< [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
< [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
--
【.nvmディレクトリ作成の確認】
ll -a .nvm/
> 以下出力
total 152
drwxr-xr-x. 2 ec2-user ec2-user 59 Apr 15 04:12 .
drwx------. 5 ec2-user ec2-user 144 Apr 15 04:12 ..
-rw-r--r--. 1 ec2-user ec2-user 2299 Apr 15 04:12 bash_completion
-rwxr-xr-x. 1 ec2-user ec2-user 371 Apr 15 04:12 nvm-exec
-rw-r--r--. 1 ec2-user ec2-user 143991 Apr 15 04:12 nvm.sh
【.bashrcファイルをリロード】
source ~/.bashrc
これでNode.jsをインストールする準備が整いました。
Node.jsのインストール
【PATH環境変数の確認を先にしておきましょう。】
echo $PATH
> 以下出力
/home/ec2-user/.local/bin:/home/ec2-user/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
【最新のTLS(Long Term Support)バージョンのNode.jsをインストール】
nvm install --lts
> Node.jsをインストールするとNPM(Node Package Manager)も同時にインストールされます。
【Node.jsインストールの確認】
node -v
> v22.14.0
【NPMのインストール確認】
npm -v/--version
> 10.9.2
【PATH環境変数の確認】
echo $PATH
> 以下出力
/home/ec2-user/.nvm/versions/node/v22.14.0/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
参考URL
Node.jsの実行
試しのNode.jsのファイルを作成して実行してみましょう。
【プロジェクトディレクトリの作成と移動】
mkdir /home/ec2-user/server && cd /home/ec2-user/server
【.jsファイルを作成】
vi index.js
> 以下ファイルの中身
console.log('Hello Node!');
【.jsファイルの実行】
node index.js
Hello Node!と表示されれば成功です。
NPMとは
NPM(Node Package Manager)はノードで使用するパッケージ(ライブラリ)の管理を行うコマンドラインツールです。NPMののコマンドでプロジェクトの設定ファイルを作成したり、パッケージのインストール・更新・削除や、設定ファイル実行などを行います。
設定ファイルの作成
【プロジェクトディレクトリに移動】
cd /home/ec2-user/server
【プロジェクトの設定フィアルを作成】
npm init --yes
> --yesオプションをつけるとデフォルトのセットアップになります。
> --yesオプションを付けない場合はプロジェクト名などを入力するプロンプトが表示されます。
【設定ファイル作成の確認】
ls -la
> package.jsonという設定ファイルが作成されたのが分かります。
パッケージのインストール
後述のNode Webサーバを作成する為に、Expressパッケージをインストールします。
【Expressパッケージのインストール】
npm install express@latest -l --save
> -l オプションはローカルディレクトリにパッケージを保存(デフォルトなので省略可)
> -g オプションはグローバルディレクトリにパッケージを保存
> --saveオプションは変更をpackage.json設定ファイルに反映します。
【ローカルのモジュールディレクトリ作成の確認】
ll | grep node_modules
【設定フィアルの変更内容の確認】
cat package.json
> 以下出力
{
"name": "server",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^5.1.0"
}
}
その他コマンド
【パッケージの更新】
npm update express
【パッケージの削除】
npm uninstall -l --save express
npm uninstall -g --save express
> --save オプションで削除をpackage.jsonに反映します。
【パッケージの検索】
npm search express
【設定ファイルの実行】
npm run package.json
【npm自体をアップデートする】
npm install npm -g
npm installコマンドでグローバルオプション(-g)をつけた場合のモジュールの保存先は以下のコマンで確認できます。
npm root -g
> 以下出力
/home/ec2-user/.nvm/versions/node/v22.14.0/lib/node_modules
ルートユーザでnode/npmコマンドを使えるようにする
ここまで、上記の通りec2-userでNode.jsとNPMのインストールを行いましたが、この状態でルートユーザとしてのsudoコマンドでnodeコマンド(sudo node -v)を実行しようとするとnodeコマンドが存在しないエラーが表示されます。これはNodeとNPMのインストールがコマンド実行ユーザのものとして行われたからです。
以下のコマンドでnode/npmコマンドがどこにインストールされたのかが確認できます。
which node
> 以下出力
~/.nvm/versions/node/v22.14.0/bin/node
which npm
> 以下出力
~/.nvm/versions/node/v22.14.0/bin/npm
以下の様にnodeコマンドへのフルパスを指定して実行することは可能ですが、ちょっと面倒ですよね。
sudo /home/ec2-user/.nvm/versions/node/v22.14.0/bin/node -v
rootユーザにコマンドパスが見えるようにする
rootユーザがnode/npmコマンドのパスにアクセスする2つの方法を紹介します。
シムリンクを作成する
容易で安全な方法はrootユーザがアクセスするできるパスにec2-userのホームディレクトリ内にあるnode/npmコマンドへのシムリンクを作成する。コマンドは以下の通り。
sudo ln -s $(which node) /usr/local/bin/node
sudo ln -s $(which npm) /usr/local/bin/npm
【実行確認】
sudo node -v
sudo npm -v
sudoerファイルでrootがアクセスできるパスを追記する
/etc/sudoers
【以下の行をその次の行にコピペして変更前はコメントアウト】
#Defaults secure_path = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/var/lib/snapd/snap/bin
Defaults secure_path = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/var/lib/snapd/snap/bin:/home/ec2-user/.nvm/versions/node/v22.14.0/bin
> セミコロン含む太線部分を追記
sudoerファイルは構文ミスなどするとログインできなくなるリスクがあるので編集時は注意しましょう。
HTTPプロトコルのNode.js Webサーバの設定
セキュリティーグループの設定
Node.jsをWebサーバとして使用するので以下のセキュリティーグループの作成と設定をしてください。
- 80番ポートと443番ポートを使用するセキュリティーグループ(www)
セキュリティーグループの作成については以下の記事を参考に設定してください。

HTTPプロトコルのNodeサーバプログラムの作成
【プロジェクトディレクトリに移動】
cd /home/ec2-user/server
【Webサーバ用のファイルを作成】
server_http.js
> 以下内容
"use strict";
const express = require("express");
const PORT = 80;
const app = express();
const path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.get("/", (req, res) => {
res.send("Hello from server on HTTP");
});
app.listen(PORT, () => {
console.log(`Node server is running on port:${PORT}`);
}).on('error', (err) => {
console.error('Error starting server:', err.message);
});
process.on("SIGINT", function () {
console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");
process.exit(0);
});
静的ファイルの作成
【公開フォルダを作成】
mkdir /home/ec2-user/server/public
【テストファイルの作成】
./public/express.html
> 以下を記述
Hello from express.html
Nodeサーバの起動と確認
【ディレクトリの移動】
cd /home/ec2-user/server/
【サーバを起動】
sudo node server_http.js
【ブラウザでの表示確認】
http://サーバのドメイン又はIP/
http://サーバのドメイン又はIP/express.html
NodeサーバのHTTPS化に向けた準備
/.well-known/acme-challenge/ディレクトリを理解する
ドット始まりのディレクトリは隠しディレクトリでなので通常は公開ディレクトリ配下にあるべきものでもなく、公開状態にするべきものでもありません。ただし、Webサーバの設定によって隠しフォルダに対するアクセス制限は変更可能です。
後述の様に、Let’s Encryptを使ってSSL/TLS証明書を発行するのにcertbotコマンドを使用します。その際に公開ディレクトリ直下の以下の隠しディレクトリが使用されます。
/.well-known/acme-challenge/
.well-known ディレクトリは標準仕様(RFC 5785)で定められている公開情報用のディレクトリで特別なディレクトリとなっています。そして、acme-challenge ディレクトリはACMEプロトコル(Let’s Encryptが作成したプロトコル)専用のサブパスです。証明書の発行時にはこのパスを使ってトークンファイルのやり取りが行われることになり、ます。なので、このパスはアクセス可能な状態でなければなりません。
/.well-known/acme-challenge/のディレクトリは作成しておく必要はありません。自動で作成され、自動で削除されます。
サーバプログラムの変更
Nodeサーバを停止し、server.jsファイルのリネームコピーバックアップをしてserver.jsを編集します。ここではLet’s EncryptがSSL/TLS証明書の発行ができるように調整します。太字部分が変更箇所になります。
【プロジェクトディレクトリに移動】
cd /home/ec2-user/server
【サーバプログラムのコピー】
cp -ip server_http.js server_http.js_bkup
【サーバプログラムの編集】
server_http.js
> 以下内容
"use strict";
const express = require("express");
const PORT = 80
const app = express();
const path = require('path');
// .well-known ディレクトリへのアクセスを許可
app.use('/.well-known', express.static(path.join(__dirname, 'public', '.well-known'), {
dotfiles: 'allow',
index: false,
}));
// 前回の状態はコメントアウト
// app.use(express.static(path.join(__dirname, 'public')));
// public ディレクトリの公開。ただし 隠しディレクトリへのアクセスは許可しない。
app.use(express.static(path.join(__dirname, 'public'), {
dotfiles: 'deny',
index: false,
}));
app.get("/", (req, res) => {
res.send("Hello from server on HTTP");
});
app.listen(PORT, () => {
console.log(`Node server is running on port:${PORT}`);
}).on('error', (err) => {
console.error('Error starting server:', err.message);
});
process.on("SIGINT", function () {
console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");
process.exit(0);
});
編集が終わったら再度Nodeサーバを起動させておきます。
【サーバの起動】
sudo node server_http.js
【ブラウザでの表示確認】
http://サーバのドメイン又はIP/
http://サーバのドメイン又はIP/express.html
express.staticミドルウェアには以下の様なパラメータの指定が可能です。
dotfiles・・・隠しディレクトリ・ファイルへのアクセス
allow(アクセス許可)
deny(アクセス拒否=403 Forbidden)
ignore(無視=404 Not Found=デフォルト)
index・・・インデックスファイルの表示
index.html(デフォルト=ファイルがあれば表示)
false(インデックスファイルがあっても自動表示しない)
参考URL:https://expressjs.com/en/5x/api.html
NodeサーバをHTTPS化する
Let’s Encryptを使ったHTTPS化
ここではWebサーバとの通信を暗号化してより安全にサーバにアクセスできるHTTPS化の設定について解説していきます。HTTPS化にはSSL/TLS証明書というものが必要になりますが、無料でこの証明書を発行してくれる Let’s Encrypt というサービスを使用してHTTPS化をしていきます。
Let’s Encrypt 用にCertbotのインストールと実行
Certbotのインストール
Let’s Encryptの証明書を発行してくれるcertbotというプログラムはPython言語でできているので、まずはPythonのパッケージ管理ツールをインストールであるPIPをインストールする必要があります。
【Pythonと関連パッケージをインストール】
sudo dnf install python3 augeas-libs -y
【Pythonの仮想環境の設定とPIPのインストール】
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
【PIPコマンドを使ってcertbotをインストール】
sudo /opt/certbot/bin/pip install certbot
【certbotコマンドの使用準備】
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot
Certbotの実行
ここまでで、Certbotを実行する準備が整いました。次のコマンドで証明書の発行をしてみましょう。
sudo certbot certonly
プロンプトに従って質問内容に答えていきます。
1.認証方法を聞かれるので、.well-knownディレクトリをを使用した方法の「2」を選択。
2.メールアドレスの入力。
2.利用条件への同意(Y)
3.関連組織へのメールアドレスの共有とニュースなどの情報を受け取りたいか(N)
->ここはNでよいと思います。
4.SSL/TLSを適用したいドメイン名を入力します。
5.公開ディレクトリのパスを聞かれるので「/home/ec2-user/server/public」と入力。
6.証明書の発行が成功した旨のメッセージが表示されて完了になります。
以下はcertbotコマンド実行時の出力例になります。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Runs an HTTP server locally which serves the necessary validation files under
the /.well-known/acme-challenge/ request path. Suitable if there is no HTTP
server already running. HTTP challenge only (wildcards not supported).
(standalone)
2: Saves the necessary validation files to a .well-known/acme-challenge/
directory within the nominated webroot path. A separate HTTP server must be
running and serving files from the webroot path. HTTP challenge only (wildcards
not supported). (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Enter email address or hit Enter to skip.
(Enter 'c' to cancel): ******@gmail.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at:
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf
You must agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, 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
Account registered.
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel): mydomain.example.com
Requesting a certificate for mydomain.example.com
Input the webroot for mydomain.example.com: (Enter 'c' to cancel): /home/ec2-user/server/public
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/mydomain.example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/mydomain.example.com/privkey.pem
This certificate expires on 2025-07-17.
These files will be updated when the certificate renews.
NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NodeサーバのファイルをHTTPS化用に書き換える
サーバのファイルをリネームコピーします(*_https.jsと’s’を付けます)。以下の様に編集をして。SSL/TLS証明書を読み込むように変更します。太字箇所が変更箇所になります。
【プロジェクトディレクトリに移動】
cd /home/ec2-user/server
【サーバプログラムのリネームコピー】
cp -ip server_http.js server_https.js
【サーバプログラムの編集】
server_https.js
> 以下内容
"use strict";
const express = require("express");
//const PORT = 80;
const PORT = 443;
const app = express();
const path = require('path');
const fs = require("fs");
const https = require("https");
const options = {
key: fs.readFileSync("/etc/letsencrypt/live/mydomain.example.com/privkey.pem"),
cert: fs.readFileSync("/etc/letsencrypt/live/mydomain.example.com/cert.pem"),
};
// .well-known ディレクトリへのアクセスを許可
app.use('/.well-known', express.static(path.join(__dirname, 'public', '.well-known'), {
dotfiles: 'allow',
index: false,
}));
// 前回の状態はコメントアウト
// app.use(express.static(path.join(__dirname, 'public')));
// public ディレクトリの公開。ただし 隠しディレクトリへのアクセスは許可しない。
app.use(express.static(path.join(__dirname, 'public'), {
dotfiles: 'deny',
index: false,
}));
app.get("/", (req, res) => {
res.send("Hello from server on HTTP");
});
const httpsServer = https.createServer(options, app);
//app.listen(PORT, () => {
httpsServer.listen(PORT, () => {
console.log(`Node server is running on port:${PORT}`);
}).on('error', (err) => {
console.error('Error starting server:', err.message);
});
process.on("SIGINT", function () {
console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");
process.exit(0);
});
編集が終わったらNodeサーバを起動します。
【サーバの起動】
sudo node server_https.js
【ブラウザでの表示確認】
https://サーバのドメイン又はIP/
https://サーバのドメイン又はIP/express.html
ULR欄の左側に鍵マークが表示されたのが確認できるはずです。これでNodeサーバのHTTPS化が完了しました。
Let’s Encryptの証明書の更新を自動化する
Cronieのインストール
新しいAmazon Linxu 2023のEC2インスタンスは、旧来のAmazon Linux 2のインスタンスと違って、定期実行プログラムのcrondパッケージはインストールされていません。代わりに Amazon Linxu 2023 のインスタンスではcronieというパッケージをインストールする必要があります。
【cronieがインストール済みかを確認】
sudo dnf list installed cronie
【cronieパッケージが存在するか確認】
sudo dnf info cronie
【cronieをインストール】
sudo dnf install cronie -y
【システムブート時にcoronieも起動するように設定】
sudo systemctl enable crond.service
【cronieプログラムw再起動】
sudo systemctl restart crond
【cronieプログラムの実行状態を確認】
sudo systemctl status crond | grep Active
sudo systemctl status crond.service
->上記共にActiveと表示されればOKです。
Crontabファイルの設定
まずは、以下のファイルのバックアップを取りましょう。
/etc/crontab
ファイル内には以下を追記しましょう。以下の例では毎日0時 10分 にcertbotコマンドを使用してSSL証明書の更新を行います。証明書の期限の 30 日より前は、コマンドは実行されますが期限よりだいぶ前なので実際の更新は行われませんが、更新期限から 30 日を切ると実際に更新処理が行われます。
10 1,15 * * * root certbot renew > /etc/crontab.log 2>&1
定期実行処理については「Linux|定期処理(Cronジョブ)実行の設定方法」の記事を参考にして下さい。

NodeサーバのHTTPS化が行われても証明書の更新はcertbotによって引き続き80番ポートが使用されるので、80番ポートと443番ポートでアクセスできる状態にしておかなければなりません。
80番ポートは証明書の更新のみに使用して他はHTTPSにリダイレクト
サーバプログラムをひとつにまとめて、80番ポートと443番ポートを同時にListenするように変更します。
【プロジェクトディレクトリに移動】
cd /home/ec2-user/server
【HTTPSサーバプログラムのコピーバックアップ】
cp -ip server_http.js server_https.js_bkup
【サーバプログラムの編集】
server_https.js
> 以下内容
"use strict";
const express = require("express");
//const PORT = 80;
const PORT = 443;
const app = express();
const path = require('path');
const fs = require("fs");
const https = require("https");
const options = {
key: fs.readFileSync("/etc/letsencrypt/live/mydomain.example.com/privkey.pem"),
cert: fs.readFileSync("/etc/letsencrypt/live/mydomain.example.com/cert.pem"),
};
// .well-known ディレクトリへのアクセスを許可
app.use('/.well-known', express.static(path.join(__dirname, 'public', '.well-known'), {
dotfiles: 'allow',
index: false,
}));
// 前回の状態はコメントアウト
// app.use(express.static(path.join(__dirname, 'public')));
// public ディレクトリの公開。ただし 隠しディレクトリへのアクセスは許可しない。
app.use(express.static(path.join(__dirname, 'public'), {
dotfiles: 'deny',
index: false,
}));
app.get("/", (req, res) => {
res.send("Hello from server on HTTP");
});
const httpsServer = https.createServer(options, app);
//app.listen(PORT, () => {
httpsServer.listen(PORT, () => {
console.log(`Node server is running on port:${PORT}`);
}).on('error', (err) => {
console.error('Error starting server:', err.message);
});
const http = require('http');
// HTTPサーバー
http.createServer((req, res) => {
// 証明書更新用
if (req.url.startsWith('/.well-known/acme-challenge')) {
// 特定の場合だけExpressで処理
app(req, res);
} else {
// 以外はHTTPSへリダイレクト
res.writeHead(301, { Location: `https://${req.headers.host}${req.url}` });
res.end();
}
}).listen(80, () => {
console.log(`Node server is running on port:80`);
}).on('error', (err) => {
console.error('Error starting server:', err.message);
});
process.on("SIGINT", function () {
console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");
process.exit(0);
});
編集後、サーバを起動します。
【サーバの起動】
sudo node server_https.js
【ブラウザでの表示確認】
> http://でアクセスしてhttps://に変われば成功です。
http://サーバのドメイン又はIP/
http://サーバのドメイン又はIP/express.html
certbot の –try-run オプションを使用すると実際の証明書の更新はせずに処理プロセスが正しく行われるかどうかを試す模擬更新処理ができます。
sudo certbot renew –dry-run
sudo certbot renew --dry-run
> 以下処理結果
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/mydomain.example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for mydomain.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/mydomain.example.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
上記の様に表示されればSSL/TLS証明書の発行に80番ポートが使用されたことになります。
まとめ
今回はNode.jsのインストールの手順を確認しました。NVMをインストールしてからNode.jsをインストールしましたね。すると同時にNPMもインストールされました。また、基本的なNPMコマンドの内容も確認してきました。それからルートユーザでnode/npmコマンドを使えるように設定しました。続いて、Node Webサーバを立ててから /.well-known/acme-challenge/ パスの理解をし、certbotを使ってLet’s EncryptのSSL/TLS証明書の発行を行い、NodeサーバをHTTPS化しました。dotfilesオプションの設定が必要でしたね。最後に証明書の更新のクロン設定をして80番ポートと443番ポートを同じサーバスクリプト内にまとめました。certbotの証明書更新は80番ポートが必要なことを覚えておきましょう。