Help Center/ Elastic Cloud Server/ Best Practices/ Setting Up an Environment/ Manually Deploying Node.js (CentOS 7.2)
Updated on 2024-11-04 GMT+08:00

Manually Deploying Node.js (CentOS 7.2)

Overview

The best practices for Huawei Cloud ECS guide you through the manual deployment of Node.js on a Linux ECS.

Node.js is a JavaScript runtime environment based on the Google Chrome V8 engine for building fast and scalable network applications. Based on the event-driven and non-blocking I/O model, Node.js is lightweight and efficient. It is ideal for running data-intensive real-time applications on distributed devices.

For more information about Node.js, see https://nodejs.org.

This section uses CentOS 7.2 64bit (40 GB) and Node.js installation packages node-v10.14.1-linux-x64.tar and node-v10.14.2-linux-x64.tar as an example to describe how to deploy Node.js.

Prerequisites

Procedure

  1. Install the Node.js software packages.

    • Using the binary file
      1. Log in to the ECS.
      2. Run the following command to download the Node.js installation package:

        wget https://nodejs.org/dist/v10.14.1/node-v10.14.1-linux-x64.tar.xz

      3. Run the following command to decompress the file:

        tar xvJf node-v10.14.1-linux-x64.tar.xz

      4. Run the following commands in any directory to set up a soft connection for node and NPM, respectively:

        ln -s /root/node-v10.14.1-linux-x64/bin/node /usr/local/bin/node

        ln -s /root/node-v10.14.1-linux-x64/bin/npm /usr/local/bin/npm

      5. Run the following commands to check the node and NPM versions:

        node -v

        npm -v

    • Using the NVM version manager
      1. Log in to the ECS.
      2. Run the following command to install git:

        yum install git

      3. Run the following command to copy the source code to the local ~/.nvm directory using git and check the version:

        git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`

      4. Run the following command to activate NVM and add it to the profile file:

        echo ". ~/.nvm/nvm.sh" >> /etc/profile

      5. Run the following command for the environment variables to take effect:

        source /etc/profile

      6. Run the following command to list available Node.js versions:

        nvm ls-remote

      7. Run the following command to install multiple Node.js versions:

        nvm install v10.14.1

        nvm install v10.14.2

      8. Run the following command to check the installed versions:

        nvm ls

      9. Run the following command to switch the Node.js version to V10.14.2:

        nvm use v10.14.2

        • Run the nvm alias default v10.14.2 command to set the default version to 10.14.2.
        • Run the nvm help command to obtain more information about NVM.

  2. Verify the deployment.

    1. Run the following command to go to the home directory:

      cd

    2. Run the following command to create a test.js project file:

      touch test.js

    3. Use VIM to edit the test.js file.
      1. Run the following command to open the test.js file:

        vim test.js

      2. Press i to enter insert mode.
        Modify the file as follows:
        const http = require('http');
        const hostname = '0.0.0.0';
        const port = 3000;
        const server = http.createServer((req, res) => {
        	res.statusCode = 200;
        	res.setHeader('Content-Type', 'text/plain');
        	res.end('Hello World\n');
        });
        server.listen(port, hostname, () => {
        	console.log(`Server running at http://${hostname}:${port}/`);
        });
        The port number can be customized.
      3. Press Esc to exit insert mode. Then, enter :wq to save the settings and exit.
    4. Run the following command to view enabled port:

      netstat -lntp

      If the port is unavailable, log in to the ECS console and change the security group rule. For details, see Adding a Security Group Rule.

    5. Add exception ports in the firewall configuration.
      1. For example, to add port 3000, run the following command:

        firewall-cmd --zone=public --add-port=3000/tcp --permanent

        If the following information is displayed, the firewall is disabled. Then, go to step 2.f.
        [root@ecs-centos7 ~]# firewall-cmd --zone=public --add-port=3000/tcp --permanent
        FirewallD is not running
        If the following information is displayed, the firewall is enabled, and the exception port has been added:
        [root@ecs-centos7 ~]# firewall-cmd --zone=public --add-port=3000/tcp --permanent
        success
      2. Reload the policy configuration for the new configuration to take effect.

        firewall-cmd --reload

      3. Run the following command to view all enabled ports:

        firewall-cmd --list-ports

        [root@ecs-centos7 ~]# firewall-cmd --list-ports
        3000/tcp
    6. Run the following command to run the project:

      node ~/test.js

    7. Enter http://EIP:3000 in the address bar to access Node.js. If the following page is displayed, Node.js has been deployed.
      Figure 1 Deployment and testing