Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install Nodejs and deploy a project on a CVM ECS instance

2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the method of installing Nodejs and deploying the project on the CVM ECS instance. The article is very detailed and has a certain reference value. Friends who are interested must read it!

Deploy the Node.js project (CentOS)

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

Real-time applications: such as online chat, real-time notification push, etc. (such as socket.io)

Distributed applications: using existing data through efficient parallel Imax O

Tool applications: a large number of tools, ranging from front-end compressed 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)

Using stable Interface to improve Web rendering ability

Unified front-end programming environment: front-end developers can cut into server-side development very quickly (such as the famous pure Javascript full-stack MEAN architecture)

Applicable object

This document describes how to install Nodejs and deploy the project on the CVM ECS instance of Aliyun CentOS system.

Preparatory work

Before you deploy, do the following preparations:

Purchase ECS instance

The image your instance is running is CentOS7.2

Your instance can be connected to the public network

Tools for connecting to Linux instances, such as PuTTY, have been installed locally.

Basic process

The steps to install Nodejs and deploy the project using CVM ECS are as follows:

Purchase the ECS instance and connect to the instance.

Choose any of the following methods to deploy the Node.js environment:

Use binaries.

Install multiple versions using NVM.

Deploy the test project.

Operation steps

Step 1: create an ECS instance

Create an instance of ECS. Select the operating system as the public mirror CentOS7.2. Log in to the Linux instance using the root user.

Step 2: deploy the Node.js environment

Deploy the Node.js environment using any of the following methods.

Install using binaries

The installation package used in this deployment process is a compiled binary, and after unzipping, node and npm already exist in the bin folder, eliminating the need for manual compilation.

Installation steps:

The wget command downloads the Node.js installation package. The installation package is a compiled file, and after unzipping, node and npm already exist in the bin folder, and there is no need to repeat compilation.

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

Extract the file.

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

Create soft links to make the node and npm commands work globally. By creating soft links, you can use node and npm commands directly in any directory:

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

Check the node and npm versions.

Node-vnpm-v

At this point, the Node.js environment is installed. The software is installed in the / root/node-v6.9.5-linux-x64/ directory by default. If you need to install the software to a different directory, such as / opt/node/, do the following:

Mkdir-p / opt/node/mv / root/node-v6.9.5-linux-x64/* / opt/node/rm-f / usr/local/bin/noderm-f / usr/local/bin/npmln-s / opt/node/bin/node / usr/local/bin/nodeln-s / opt/node/bin/npm / usr/local/bin/npm

Install multiple versions using NVM

NVM (Node version manager) is the version management software of Node.js that makes it easy for users to switch between versions of Node.js. It is suitable for people who have been doing node development for a long time or users who need to update node version and switch node version quickly.

Installation steps:

Directly use git to clone the source code to the local ~ / .nvm directory and check for the latest version.

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

Activate NVM.

Echo ". ~ / .NVM / nvm.sh" > > / etc/profilesource / etc/profile

Lists all versions of Node.js.

Nvm list-remote

Install multiple versions of Node.js.

Nvm install v6.9.5nvm install v7.4.0

Run nvm ls to see if you have installed the Node.js version, which is currently v6.9.5. The returned result is as follows.

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

Run nvm use v7.4.0 to switch the Node.js version to v7.4.0. The returned result is as follows.

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

For more information on NVM, please refer to the help documentation:

Nvm help

Step 3: deploy the test project

Create a new project file example.js.

Cd ~ touch example.js

Open the project file example.js using the vim editor.

Yum install vimvim example.js

Enter I, enter edit mode, and paste the following project file contents into the file. Use the Esc button to exit edit mode, enter: wq, enter, save the file contents and exit.

Contents of the project file:

Const http = require ('http'); const hostname =' 0.0.0.0] Const port = 3000 res Const server = http.createServer ((req, res) = > {res.statusCode = 200 setHeader ('Content-Type',' text/plain'); res.end ('Hello World\ n');}); server.listen (port, hostname, () = > {console.log (`Server running at header)

Note:

3000 in the contents of the project file is the port number, which can be defined by yourself.

Run the project.

Node ~ / example.js

Note:

You can use the command node ~ / example.js & to run the project in the background.

Use the command to see if the project port exists.

Netstat-tpln

Log in to the ECS Management console and add the security group rule release port to the security group (as in this example, port TCP 3000).

(optional) if you have a firewall turned on in your instance, you must add an inbound rule for the port (as in this example, port TCP 3000).

Enter the public network IP address of the http:// instance: Port number to access the project in the browser of the local machine.

The above is all the contents of the method of installing Nodejs and deploying the project on the CVM ECS instance. Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report