Ubuntu 22.04に最新のnode.jsをインストールする
環境
OSバージョンを確認します
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS"
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS"
# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=22.04 DISTRIB_CODENAME=jammy DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS"
操作方法
1.システムパッケージを更新します
# sudo apt update
# sudo apt update
# sudo apt update
2.nodeとnpmをインストールします
# sudo apt install -y nodejs npm
# sudo apt install -y nodejs npm
# sudo apt install -y nodejs npm
3.nodeのバージョンを確認します
# node -v
v12.22.9
# node -v
v12.22.9
# node -v v12.22.9
4.npmのバージョンを確認します
# npm -v
8.5.1
# npm -v
8.5.1
# npm -v 8.5.1
5.動作確認
# nano hello.js
以下のコードを記述します
var http = require('http');
http.createServer(
function (req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write('Hello World! study node.js');
res.end();
}
).listen(81,"0.0.0.0");
var http = require('http');
http.createServer(
function (req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write('Hello World! study node.js');
res.end();
}
).listen(81,"0.0.0.0");
var http = require('http'); http.createServer( function (req, res) { res.writeHead(200, {"Content-Type": "text/plain"}); res.write('Hello World! study node.js'); res.end(); } ).listen(81,"0.0.0.0");
6.firewallを設定します
# ufw allow http
ルールをアップデートしました
ルールをアップデートしました(v6)
# ufw allow http
ルールをアップデートしました
ルールをアップデートしました(v6)
# ufw allow http ルールをアップデートしました ルールをアップデートしました(v6)
7.nodejsファイルを実行します
# sudo node hello.js
# sudo node hello.js
# sudo node hello.js
ブラウザから http://プライベートIP:81 にアクセスします
例 http://192.168.71.132:81/
「Hello World! study node.js」が表示されます。