The content of this article is about how to install Nodejs and deploy projects on cloud server ECS instances. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Deploying Node.js project (CentOS)
Node.js is a JavaScript running environment based on the Chrome V8 engine, used to easily build fast and easy-to-expand network applications. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient, and very suitable for data-intensive real-time applications running on distributed devices. npm, the Node.js package manager, is the world's largest open source library ecosystem. Typical application scenarios include:
Real-time applications: such as online chat, real-time notification push, etc. (such as socket.io)
Distributed applications: using existing applications through efficient parallel I/O Data
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 real-time and concurrency requirements Very high requirements (such as NetEase's pomelo framework)
Use stable interfaces to improve Web rendering capabilities
Unified front-end and back-end programming language environments: front-end developers can quickly switch to server-side development ( Such as the famous pure Javascript full-stack MEAN architecture)
Applicable objects
This document introduces how to use cloud server ECS in Alibaba Cloud CentOS system On the instance, install Nodejs and deploy the project.
Preparation
Before deployment, please make the following preparations:
Purchase ECS instance
The image your instance runs is CentOS7.2
Your instance can connect 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 cloud server ECS are as follows:
Purchase an ECS instance, and connect to the instance.
Choose any of the following methods to deploy the Node.js environment:
Use binary files.
Use NVM to install multiple versions.
Deploy the test project.
Operation steps
Step 1: Create an ECS instance
Create an ECS instance. Select the operating system as the public image CentOS7.2. Log in to the Linux instance as the root user.
Step 2: Deploy the Node.js environment
Use any of the following methods to deploy the Node.js environment.
Installation using binary files
The installation package used in this deployment process is a compiled binary file. After decompression, node and npm already exist in the bin folder, no manual compilation is required.
Installation steps:
wget command to download the Node.js installation package. The installation package is a compiled file. After decompression, node and npm already exist in the bin folder, so there is no need to recompile.
wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz
Unzip the file.
tar xvf node-v6.9.5-linux-x64.tar.xz
Create soft links to make node and npm commands globally valid. 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
View node and npm versions.
node -v npm -v
At this point, the Node.js environment has been 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 other directories (such as: /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
Use NVM to install multiple versions
NVM (Node version manager) is Node.js version management software allows users to easily switch between various versions of Node.js. It is suitable for people who have been doing node development for a long time or users who need to quickly update node versions and switch node versions quickly.
Installation steps:
Use git directly 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`
Activate NVM.
echo ". ~/.nvm/nvm.sh" >> /etc/profile source /etc/profile
List all versions of Node.js.
nvm list-remote
Install multiple Node.js versions.
nvm install v6.9.5 nvm install v7.4.0
Run nvm ls to check the installed Node.js version. The current version is v6.9.5. 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)
Run nvm use v7.4.0 to switch the Node.js version to v7.4.0. The returned results are as follows.
[root@iZXXXXZ .nvm]# nvm use v7.4.0 Now using node v7.4.0
For more operations on NVM, please refer to the help document:
nvm help
Step 3: Deploy the test project
Create a new project file example.js.
cd ~ touch example.js
Use vim editor to open the project file example.js.
yum install vim vim example.js
Enter i to enter editing mode and paste the following project file contents into the file. Use the Esc button to exit editing mode, enter: wq, press Enter, save the file content and exit.
Project file content:
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}/`); });
Note:
3000 in the project file content 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 put the project in the background.
Use the command to check whether the project port exists.
netstat -tpln
登录ECS管理控制台,并在安全组中 添加安全组规则 放行端口(如本示例中为TCP 3000端口)。
(可选)如果您的实例中开启了防火墙,必须添加端口的入站规则(如本示例中为TCP 3000端口)。
在本地机器的浏览器中输入 http://实例公网IP地址:端口号 访问项目。
The above is the detailed content of How to install Nodejs and deploy projects on cloud server ECS instances. For more information, please follow other related articles on the PHP Chinese website!