Ali Cloud Services best Practices

Linux Distributed javascript HTTPS ECS Cloud server HTTP JS centos node Aliyun git

Abstract:

Node.js is a JavaScript runtime environment based on Chrome V8 engine, which is used to build fast and easily extensible web applications. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient, ideal for data-intensive real-time applications running on distributed devices.

Node.js is a JavaScript runtime environment based on Chrome V8 engine, which is used to build fast and easily extensible web applications. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and…

Typical application scenarios include:

  • Real-time applications: live chat, real-time notification push, etc. (socket.io)
  • Distributed applications: Use existing data through efficient parallel I/O
  • Utility applications: A vast array of tools, from small front-end compression deployments (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)
  • Improve Web rendering capabilities with stable interfaces
  • Unified front-end and back-end programming languages: Front-end developers can jump into server-side development very quickly (such as the famous pure Javascript full-stack MEAN architecture)

Next we will introduce how to deploy node.js project in Aliyun ECS.

Deployment process

Before deployment, make the following preparations:

  • Purchase an ECS instance
  • The image on which the instance is running is CentOS7.2
  • The instance can connect to the public network
  • Tools for connecting to Linux instances, such as Xshell, are already installed locally

To install Nodejs and deploy the project using cloud server ECS, perform the following steps:

  1. Purchase an ECS instance.
  2. Deploying the Node.js environment – binary installation.
  3. Deploy the Node.js environment – Install multiple versions using NVM.
  4. Deploy the test project.

Note: Step 2 and Step 3 should be selected according to actual application scenarios.

steps

Step 1: Create an ECS instance

Select the operating system as the public image CentOS7.2. Log in to the Linux instance as user root.

Step 2: Deploy the Node.js environment — binary installation

The installation package used in this deployment process is a compiled binary file. After decompression, node and NPM are already stored in the bin folder. Manual compilation is not required.

Installation steps:

1. Run the wget command to download the Node.js installation package.

The installation package is a compiled file. After decompression, node and NPM are already stored in the bin folder. You do not need to compile the package again.

Wget HTTP: / / https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xzCopy the code

2. Unzip the files.

The tar XVF node - v6.9.5 - Linux - x64. Tar. XzCopy the code

3. Create a soft link to make node and NPM commands globally valid.

Create soft links so that node and NPM commands can be used 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
Copy the code

4. View the node and NPM versions.

node -v
npm -v
Copy the code

5. The Node.js environment has been installed.

The software is installed in the /root/node-v6.9.5-linux-x64/ directory by default. To install the software in another directory, such as /opt/node/, perform the following operations:

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
Copy the code

Step 3: Deploy the Node.js environment – install multiple versions using NVM

Node Version Manager (NVM) is node. js version management software. Users can switch between different versions of Node.js easily. It is suitable for long-term node developers or users who need to quickly update node versions or switch node versions.

Installation steps:

1, directly 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`
Copy the code

2. Activate the NVM.

echo ". ~/.nvm/nvm.sh" >> /etc/profile
source /etc/profile
Copy the code

3. List all versions of Node.js.

nvm list-remote
Copy the code

4. Install multiple versions of Node.js.

NVM install v6.9.5 NVM install v7.4.0Copy the code

5. Check the installed Node.js version. The current version is V6.9.5.

[root@iZuf62didsxigy36d6kjtrZ .nvm]# nvm ls- > v6.9.5 v7.4.0Copy the code

6. Switch node.js to V7.4.0.

[root@iZuf62didsxigy36d6kjtrZ .nvm]# nvm use v7.4.0Now using the node v7.4.0Copy the code

See the help documentation for more operations on NVM:

nvm help
Copy the code

Step 4: Deploy the test project

1, create a new project file example.js.

cd ~
touch example.js
Copy the code

2. Open the project file example.js using the Vim editor.

yum install vim
vim example.jsCopy the code

Type “I” to enter the editing mode and paste the following contents of the project file into the file. Press Esc to exit the editing mode, enter :wq, and press Enter to save the file and exit.

Contents of project files:

const http = require('http');
const hostname = 'ECS public IP address ';
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}/ `); });Copy the code

Note: Enter the actual ECS public IP address in ‘ECS Public IP Address ‘in the project file. 3000 in the project file is the port number, which can be customized.

3. Run projects.

node ~/example.js
Copy the code

Note: You can run the project in the background using the command “node ~/example.js &”.

4. Run the command to check whether the project port exists.

netstat -tpln

5. Enter http://IP: port number in the browser to access the project.

At this point our Node.js project is deployed.

Founded in 2013, Cloud Technology is one of the few service companies in China whose business is entirely based on cloud computing technology. Based on public cloud computing technology, resident cloud helps enterprises choose cloud computing and big data products that truly suit their business needs.

As the ecological service partner of Aliyun technology, we will deposit the cases and best practices in the service here, welcome everyone to exchange

A link to the

Users can experience the operations in the above documents through the cloud sandbox platform, click here.

For more open source software in the cloud marketplace, click here.

This article is the original content of the cloud habitat community, shall not be reproduced without permission, if need to be reproduced, please send email to [email protected]; If you find any content suspected of plagiarism in our community, you are welcome to send an email to [email protected] to report and provide relevant evidence. Once verified, our community will immediately delete the content suspected of infringement.

The original link