Install Nodejs on Linux

Since the nodejs version installed directly with yum is too low, this article explains how to install the latest version of Node.js on Linux.

Installation environment

  • CentOS Linux release

Obtain the Node.js installation package

  • The node. js installation package and source code can be downloaded at nodejs.org/en/download… , you can choose the node.js installation package you want based on the platform. In this case, the option is [LTS] Linux Binaries (x64).

Install Node.js on Linux

Wget HTTP: / / https://nodejs.org/dist/v12.18.1/node-v12.18.1-linux-x64.tar.xz / / download the tar xf node - v12.18.1 - Linux - x64. Tar. Xz / / decompression CD node-v12.18.1-linux-x64 // Go to the decompressed directoryCopy the code

The bin directory contains node, NPM and other commands. We can modify the Linux environment variable (profile) to directly run the command:

Make it a habit to back up important documents before changing them.

cp /etc/profile /etc/profile.bak
Copy the code

Then vim /etc/profile, add export PATH=$PATH at the bottom: follow the PATH of the bin directory under node

The export PATH = $PATH: / root/node - v12.18.1 - Linux - x64 / binCopy the code

Effective immediately

source /etc/profile
Copy the code
[root@localhost ~]# node -v
v12.18.1
Copy the code

Installation successful!

First, install Docker is the simplest

Y install yum-utils device-mapper-persistent-data lvm2 Yum - config - manager - add - repo install the latest version of the Docker at https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo CE and containerd: yum install docker-ce docker-ce-cli containerd. IO Systemctl start docker -- start docker systemctl enable docker -- Start docker systemctl enable docker -- start dockerCopy the code

2. Download the Node image

Docker pull node: 12.20.1Copy the code

Run the Node container

Assume that the front-end APP is in the /home/demo directory on the host

  • Method 1: Run the image container to package the environment + program
Docker run --name node -itd -p 9000:9000 node:12.20.1Copy the code

Copy the first section of the host vUE framework APP to the container /home/demo directory

docker cp /home/demo/  214da50ea0eb:/home/demo

Copy the code

or

Method 2: Run the image container and hang it in the APP directory of the host for easy packaging of the environment

Docker run --name node -itd -v /home/demo:/home/demo -p 9000:9000 node:12.20.1Copy the code

Run the program in a container

1. Enter the node container:

docker exec -it node sh
Copy the code

2. Check node and NPM version numbers:

node  -v   
npm  -v
Copy the code

3. Go to the demo directory where the container APP resides

CD home/demo/ NPM I -g NRM NRM ls // View the NPM source NRM use Taobao NPM I NPM run devCopy the code

Docker command

Docker ps -a // View all container commandsCopy the code
  • Stopping a container
$docker stop < container ID>Copy the code
  • restart
$docker restart < container ID>Copy the code
  • Delete container using docker rm command:
$ docker rm -f 1e560fca3906
Copy the code
  • View the IP of the Docker container
Docker inspect Container IDCopy the code

The problem

  • Docker deployment Nodejs environment development VUE cannot be accessed from the outside