CentOs8 nodejs インストール

CentOs8 nodejs インストール

公式がまだCentOs8をサポート(2019/10/17時点)していないようなので、 標準リポジトリ からインストールする

CentOs8バージョン

#cat /etc/centos-release
CentOS Linux release 8.0.1905 (Core)

インストール

## 確認
yum info nodejs

<出力結果>
メタデータの期限切れの最終確認: 1:20:36 時間前の 2019年10月17日 10時10分14秒 に実施しました。
利用可能なパッケージ
名前         : nodejs
エポック     : 1
バージョン   : 10.16.3
リリース     : 2.module_el8.0.0+186+542b25fc
アーキテクチ : x86_64
サイズ       : 9.0 M
ソース       : nodejs-10.16.3-2.module_el8.0.0+186+542b25fc.src.rpm
Repo         : AppStream
概要         : JavaScript runtime
URL          : http://nodejs.org/
ライセンス   : MIT and ASL 2.0 and ISC and BSD
説明         : Node.js is a platform built on Chrome's JavaScript runtime
             : for easily building fast, scalable network applications.
             : Node.js uses an event-driven, non-blocking I/O model that
             : makes it lightweight and efficient, perfect for data-intensive
             : real-time applications that run across distributed devices.

## インストール
yum install -y nodejs

## 確認
node --version

<出力結果>
v10.16.3

## npm 確認
npm --version

<出力結果>
6.9.0

Hello Worldしてみる

外部からアクセスして確認できるようにします

## hello.js作成
vi hello.js

<編集>
var http = require('http');

http.createServer(
  function (req, res) {
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.write('Hello World!');
    res.end();
  }
).listen(81,"0.0.0.0");

firewall設定

## firewall設定
firewall-cmd --add-port=81/tcp --permanent
firewall-cmd --reload

## 実行
node hello.js

ブラウザから http://プライベートIP:81 にアクセスして

下記のように、Hello World!と表示されれば成功