ICT Diary

Network(主にCisco系)、Server(RedHat系)、Program(適当)を気まぐれにUPしていく。

CentOS7 Node.js [Express]フレームワークを使用したweb(http)サーバ構築

目的



CentOS7をNode.jsのフレームワークExpressを使用したWebサーバを構築し「Hello World!」を表示する

環境



  • CentOS7(64bit) CentOS Linux release 7.5.1804 (Core)
  • VirtualBox環境で構築
  • Node.js 3.10.10
  • NPM v6.14.3

目次



Node.jsのインストール



  • EPELリポジトリを導入

      sudo yum install -y epel-release
    
  • yumのアップデート

      sudo yum -y update
    
  • node.jsのインストール

      sudo yum install -y nodejs
    
  • バージョンの確認

      node -v
      3.10.10
    
      npm -v
      v6.14.3
    

フレームワーク(Express)のインストール



  • http(80)ポートでのアクセス許可(firewalld)

      sudo firewall-cmd --add-service=http --zone=public --permanent
      success
    
  • ファイアーウォール(firewalld)の設定の更新

      sudo firewall-cmd --reload
      success
    
  • ファイアーウォール(firewalld)の再起動

      sudo systemctl restart firewalld
    
  • Expressのインストール

      sudo npm install express
    
  • インストール確認

      ls
      node_modules
    

Web(HTTP)サーバ起動



サーバが起動すると、起動メッセージ「Web server start of port 80」が出力される

  • webサーバを起動する為のファイルを作成(app.js) ```javascript var express = require('express'); var app = express();

      app.get('/', function (req, res) {
      res.send('Hello World!');
      });
    
      app.listen(80, function () {
      console.log("Web server start of port 80");
      });
    

    ```

  • app.jsを実行してWeb(http)サーバを起動

      sudo node app.js
      Web server start of port 80
    

Web(HTTP)サーバ動作確認



ブラウザで今回使用している機器のIPでブラウザにアクセス「Hello World!」が表示されれば成功 - ブラウザに打ち込むURL

http://[IP アドレス]/

web hello world