All Products
Search
Document Center

Elastic Compute Service:Deploy a Node.js environment on an Alibaba Cloud Linux 2 instance

Last Updated:Jan 25, 2024

This topic describes how to install Node.js and deploy a project on an Elastic Compute Service (ECS) instance that runs Alibaba Cloud Linux 2.

Background Information

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js is ideal for data-intensive real-time applications that run on distributed devices. The Node.js package manager (npm) is an ecosystem of open-source libraries. Node.js is suitable for the following scenarios:

  • Real-time applications: instant messaging and real-time push notification applications, such as Socket.IO.

  • Distributed applications: applications that perform efficient parallel I/O that consumes existing data.

  • Utilities: a variety of utilities from frontend compression and deployment applications such as grunt to graphical desktop applications.

  • Game applications: real-time and high-concurrency applications in the gaming field, such as the Pomelo framework of NetEase.

  • Web rendering applications: applications that use stable interfaces to improve the rendering performance of web pages.

  • Consistent frontend and backend programming environments: applications that allow frontend developers to take on server-side development, such as the full-stack JavaScript MongoDB, Express.js, AngularJS, and Node.js. (MEAN) framework.

Prerequisites

  • An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the Sign up to Alibaba Cloud page.

  • An ECS instance is created. For information about how to create an ECS instance, see Create an instance by using the wizard.

    Note

    In this topic, an ECS instance that uses the Alibaba Cloud Linux 2.1903 LTS 64-bit public image is used.

Step 1: Deploy a Node.js environment

  1. Connect to a created ECS instance.

  2. Deploy a Node.js environment.

    Use NVM to install multiple Node.js versions

    NVM is software that is used to manage Node.js versions. You can use NVM to switch between Node.js versions with ease. NVM is suitable for developers who are dedicated to Node.js or users who want to efficiently update or switch between Node.js versions. To install multiple Node.js versions by using NVM, perform the following steps:

    1. Install the distributed version management system Git.

      yum install git -y
    2. Use Git to clone the source code of NVM to the local ~/.nvm directory and check for the latest update.

      Note

      The source code of NVM may fail to be cloned due to network issues. If a failure occurs, we recommend that you clone the source code again.

      git clone https://gitee.com/mirrors/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
    3. Run the following commands in sequence to configure the environment variables of NVM:

      echo ". ~/.nvm/nvm.sh" >> /etc/profile
      source /etc/profile
    4. Run the following command to set the npm image repository to the Alibaba Cloud image repository to speed up the download of Node.js:

      export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
    5. Run the following command to check the version of Node.js:

      nvm list-remote
    6. Install multiple Node.js versions.

      1. Run the following command to install Node.js v14.0.0:

        nvm install v14.0.0
      2. Run the following command to install Node.js v16.0.0:

        nvm install v16.0.0
    7. Run the following command to check the installed Node.js versions:

      nvm ls

      The following command output indicates that v14.0.0 and v16.0.0 are installed and v16.0.0 is in use:

              v14.0.0
      ->      v16.0.0
      default -> v14.0.0
      iojs -> N/A (default)
      unstable -> N/A (default)
      node -> stable (-> v16.0.0) (default)
      stable -> 16.0 (-> v16.0.0) (default)
      lts/* -> lts/hydrogen (-> N/A)
      lts/argon -> v4.9.1 (-> N/A)
      lts/boron -> v6.17.1 (-> N/A)
      lts/carbon -> v8.17.0 (-> N/A)
      lts/dubnium -> v10.24.1 (-> N/A)
      lts/erbium -> v12.22.12 (-> N/A)
      lts/fermium -> v14.21.3 (-> N/A)
      lts/gallium -> v16.20.1 (-> N/A)
      lts/hydrogen -> v18.16.1 (-> N/A)
      Note

      You can run the nvm use <Version number> command to switch between the Node.js versions. For example, to switch to Node.js v18.0.0, run the nvm use v18.0.0 command.

    Use binary files to install a Node.js version

    The installation package used in this method is a compiled binary file. After you decompress the package, you can find node and npm in the bin folder. You do not need to recompile the files. In this example, Node.js v16.0.0 is installed. Perform the following operations:

    1. Run the following command to download the Node.js installation package:

      wget https://nodejs.org/dist/v16.0.0/node-v16.0.0-linux-x64.tar.xz
      Note

      To download the installation packages for other Node.js versions, visit the Node.js official website.

    2. Run the following command to decompress the Node.js installation package:

      tar xvf node-v16.0.0-linux-x64.tar.xz
    3. Run the following commands in sequence to create symbolic links to node and npm.

      After you create the symbolic links, you can directly run the node and npm commands in an arbitrary directory.

      ln -s /root/node-v16.0.0-linux-x64/bin/node /usr/local/bin/node
      ln -s /root/node-v16.0.0-linux-x64/bin/npm /usr/local/bin/npm
    4. Run the following commands in sequence to check the versions of node and npm:

      node -v
      npm -v

      Until now, the Node.js environment is installed. By default, the software is installed in the /root/node-v16.0.0-linux-x64/ directory.

      If you want to install the software to another directory such as /opt/node/, perform the following steps:

      1. Create the /opt/node/ directory.

        mkdir -p /opt/node/
      2. Move all files of Node.js to the /opt/node/ directory.

        mv /root/node-v16.0.0-linux-x64/* /opt/node/
      3. Remove the symbolic links to node and npm from the original directory.

        rm -f /usr/local/bin/node
        rm -f /usr/local/bin/npm
      4. Create symbolic links to node and npm in the /opt/node/ directory.

        ln -s /opt/node/bin/node /usr/local/bin/node
        ln -s /opt/node/bin/npm /usr/local/bin/npm

Step 2: Deploy a test project

  1. Create a test project file named example.js.

    1. Run the following command to go back to the /root directory:

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

      touch example.js
  2. Modify the example.js project file.

    1. Run the following command to open the example.js file:

      vim example.js
    2. Press the I key to enter Insert mode and add the following content to the example.js file.

      In this example, port 3000 is occupied by the project, and the command output is Hello World. You can configure the project content (res.end) and a port number (const port) based on your business requirements.

      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}/`);
      });
    3. After you add the preceding content, press the Esc key to exit Insert mode. Enter :wq and press the Enter key to save and close the file.

  3. Run the following command to run the project and obtain the port number of the project:

    node ~/example.js &
  4. Run the following command to list the ports on which the system is listening:

    netstat -tpln

    In this example, port 3000 is displayed in the command output, which indicates that the project is running as expected.

  5. Add an inbound rule to a security group of the ECS instance to open the specified port.

    In this example, port 3000 is used. For information about how to add rules to a security group, see Add a security group rule.

  6. On your Windows computer or a Windows computer that has Internet connectivity, open a browser and enter http://<Public IP address of the ECS instance>:<Port number> in the address bar.

    In this example, <Port number> is set to 3000. The following page is displayed when you access the project.