Deploy the Node.js environment on the ECS instance

Keywords: node.js Front-end Alibaba Cloud

Introduction:   This article mainly introduces how to install Node.js and deploy the project on an ECS instance with CentOS 7.2 system installed.


For image download, domain name resolution and time synchronization, please click   Alibaba open source mirror station

1, Preconditions

  • PuTTY is installed on the machine used to connect ECS instances. Download address: PuTTY.
  • Registered alicloud account. If you have not registered, please complete it first Account registration.

2, Background information

Node.js is a JavaScript running environment based on Chrome V8 engine, which is used to easily and quickly build easy to expand network applications. Node.js uses an event driven, non blocking I/O model, which makes it lightweight and efficient. It is very suitable for data intensive real-time applications running in distributed devices. The package manager npm of node.js is the largest open source library ecosystem in the world. Typical application scenarios of node.js include:

  • Real time applications: such as online chat, real-time notification push, etc. (such as socket.io).
  • Distributed applications: use existing data through efficient parallel I/O.
  • Tool applications: a large number of tools, ranging from front-end compression deployment (such as grunt) to desktop graphical interface applications.
  • Game applications: the game field has high requirements for real-time and concurrency (such as Netease's pomelo framework).
  • Improving Web rendering capabilities with stable interfaces
  • Unified front-end and back-end programming language environment: front-end developers can quickly cut into server-side development (such as the famous pure Javascript full stack MEAN Architecture).

3, Operation steps

1. Create and connect ECS instances

Complete the following operations to create and connect ECS instances:

2. Deploy Node.js environment

Choose one of the following methods to deploy the Node.js environment.

  • Install using binaries. The installation package used in the deployment process is a compiled binary. After decompression, node and npm already exist in the bin folder without repeated compilation. Complete the following operations to deploy the Node.js environment using the binary file:

1) Download the Node.js installation package.

wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz

2) Unzip the file.

tar xvf node-v6.9.5-linux-x64.tar.xz

3) By creating soft links, you can directly use node and npm commands in any directory.

ln -s /root/node-v6.9.5-linux-x64/bin/node /usr/local/bin/node
ln -s /root/node-v6.9.5-linux-x64/bin/npm /usr/local/bin/npm

4) View node and npm versions.

node -v
npm -v

So far, the Node.js environment has been installed. The software is installed in the / root/node-v6.9.5-linux-x64 / directory by default.
5) If you need to install the software in another directory (for example: / opt/node /), please do the following:

mkdir -p /opt/node/
mv /root/node-v6.9.5-linux-x64/* /opt/node/
rm -f /usr/local/bin/node
rm -f /usr/local/bin/npm
ln -s /opt/node/bin/node /usr/local/bin/node
ln -s /opt/node/bin/npm /usr/local/bin/npm
  • Using NVM to install multi version NVM (Node Version Manager) is the version management software of Node.js, so that you can easily switch between various versions of Node.js. It is applicable to those who have been developing node for a long time or have the scenario of quickly updating and switching node versions. Complete the following operations to install multiple Node.js versions using NVM:

1) Use git to clone the source code to the local ~ /. nvm directory, and check the latest version.

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

2) Activate NVM.

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

3) Lists all versions of Node.js.

nvm list-remote

4) Install multiple versions of Node.js.

nvm install v6.9.5
nvm install v7.4.0

5) Run nvm ls to view the installed version of Node.js. The current version is v7.4.0. The returned results are as follows.

[root@iZXXXXZ .nvm]# nvm ls
      v6.9.5
->       v7.4.0
      system
stable -> 7.4 (-> v7.4.0) (default)
unstable -> 6.9 (-> v6.9.5) (default)

6) Run nvm use v7.4.0 to switch the version of Node.js to v7.4.0. The returned results are as follows.

[root@iZXXXXZ .nvm]# nvm use v7.4.0
Now using node v7.4.0

3. Deploy test projects

Complete the following operations to deploy the test project:
1) Create a new project file example.js.

cd ~
touch example.js

2) Modify the project file example.js.

  • Run the following command to open example.js.
vim example.js
  • Press i to enter edit mode.
  • Paste the following project file contents into the file.
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}/`);
});


explain   In this example, the port number of the project configuration is 3000. In the actual application process, you can configure the port number by yourself, but you must add a security group rule in the entry direction of the ECS instance security group to release the port number you configured. For details, see Add security group rule.

  • Press Esc to exit editing mode, then enter: wq and enter to save and close the file.

3) Run the project.

node ~/example.js &

4) Run the following command to see if you are listening on the project port.

netstat -tpln

In this example, the returned result list includes port 3000, indicating that the project is running normally.
5) Login ECS management console And add rules in the entry direction of the ECS instance security group to release the ports configured in the project (in this example, the port number is 3000). For specific steps to add a security group rule, see Add security group rule.
6) Enter http: / / < ECS instance public IP address >: port number in the browser of the local machine to access the item.

This article is transferred from:   Deploy Node.js environment on ECS instance - Alibaba cloud developer community

 

Posted by davidklonski on Fri, 03 Dec 2021 17:08:22 -0800