This topic describes how to install Node.js and deploy a project on an ECS instance that runs Alibaba Cloud Linux 2.
Prerequisites
An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the account registration page.
Background information
- Real-time applications: instant messaging and real-time notifications, such as Socket.IO.
- Distributed applications: efficient parallel I/O to consume existing data.
- Utilities: a variety of utilities from frontend compression and deployment applications such as grunt to desktop graphical user interface applications.
- Game applications: real-time and high-concurrency applications in the gaming field, such as the Pomelo framework of NetEase.
- Web rendering applications: applications with stable APIs 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.
Step 1: Create and connect to an ECS instance
- Use the Alibaba Cloud Linux 2.1903 LTS 64-bit public image to create an ECS instance. For more information, see Create an ECS instance.
- Connect to the ECS instance. For more information, see Connect to a Linux instance by using password authentication.
Step 2: Deploy the Node.js environment
- NVM
Node Version Manager (NVM) is the software used to manage Node.js versions. You can use NVM to switch among 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 among Node.js versions.
To install multiple Node.js versions by using NVM, perform the following steps:- Use Git to clone source code to the local ~/.nvm directory, and check for the latest update.
yum install git git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
- Activate NVM.
echo ". ~/.nvm/nvm.sh" >> /etc/profile source /etc/profile
- Retrieve a list of all Node.js versions.
nvm list-remote
- Install multiple Node.js versions.
nvm install v6.9.5 nvm install v7.4.0
- Run the
nvm ls
command to check the installed Node.js versions.v7.4.0 is used in this example. The following command output is displayed:
[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)
- Run the
nvm use <Version number>
command to switch among the Node.js versions.The Node.js version is switched to v7.4.0 in this example. The following command output is displayed:
[root@iZXXXXZ .nvm]# nvm use v7.4.0 Now using node v7.4.0
- Use Git to clone source code to the local ~/.nvm directory, and check for the latest update.
- Binary file
The installation package used in the deployment is a compiled binary file. After you decompress the package, the node and npm files already exist in the bin folder. Therefore, you do not need to recompile the binary file.
To deploy the Node.js environment by using the binary file, perform the following steps:- Download the Node.js installation package.
wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz
- Decompress the package.
tar xvf node-v6.9.5-linux-x64.tar.xz
- After you create a soft link, you can run node and npm commands directly 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
- Check the versions of node and npm.
node -v npm -v
At this point, the Node.js environment is installed. By default, the software is installed in the /root/node-v6.9.5-linux-x64/ directory.
- To install the software in another directory such as /opt/node/, run the following commands in sequence:
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
- Download the Node.js installation package.