This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Usually after finishing the project, I seldom deploy by myself, but I feel that as a developer, I can not get over it, so I asked my friends around me and made the following summary.

An overview,

This paper will be explained from the following three aspects:

Configuration oneinstack

Upload the NodeJS project to the server

Run the NodeJS project using PM2

2. Specific steps

1. Have your own server

First go to Ali Cloud to buy a student computer, after buying remember to reset the password console, password Settings as complex as possible;

2. Download Xshell

Then you need to download Xshell from the official website of Xshell.

Oneinstack configure the Web environment

Oneinstack is used to configure the Web environment. The specific configuration is as follows:

4. XShell connects to the remote host

After configuration, XShell connects to the remote host.

5. Update system software

After the connection is completed, the small red dot on the XShell console will turn into a small green dot. If a small green dot is displayed, it means that the connection is successful. If it is still a small red dot, it means that the connection has failed.

After a successful connection, the XShell console type yum update-y to update the system software. After the update, paste the installation command generated at the bottom of step 3 oneInstack into the console and continue pasting. After a while, the reset and login will be completed.

6. Install node on the server

Once logged in, we need to install a Node environment on the server. Yum install -y nodejs can be installed on the XShell console if the demand is not very high. However, the version in XShell is relatively low, such as 10.21.0 when I installed it. Node is actually released as version 14, so manual installation is recommended. Here’s how to manually install the latest nodeJS environment on the server:

You need to download the latest nodeJS version locally. You need to download the latest nodeJS version from the node official website. Since I use centOS, I choose Linux Binaries(X64).

PS: Don’t be silly like me and think you are installing a Node environment locally. The local node environment and the remote Node environment are completely different things.

(2) After downloading the installation package locally, you need to copy the package to the server. How to copy the package? You can upload the package to the server according to your personal requirements. (In Windows, winscp or Xftp is recommended for fast upload.)

③ After the upload, decompress the file on the server. You need to decompress the file in XShell using the command line. (Note: node-v14.15.4-linux-x64.tar.xz is the name of your Node installation package.) unzip the xz file into a tar file

Xz -d node-v14.15.4-linux-x64.tar.xz tar - XVF node-v14.15.4-linux-x64.tarCopy the code

4 After decompressing the node folder, copy all contents in the node folder to /usr/local/node and configure environment variables. To create a node file in /usr/local/node, run mkdir -p to create a folder.

5. Configure environment variables. The Linux environment variable file is /etc/profile. The executable file is in /usr/local/node/bin, so add this path to the environment variable file.

vi /etc/profile
Copy the code

Enter the command to enter the interface, then enter I to enter the editing mode, PageDown to the last line, without newline, add environment variables.

: /usr/local/node/bin
Copy the code

⑥ Save the Settings and exit.

ESC
Shift zz
Copy the code

⑦ Save the Settings and exit. Run the following command to make the environment variables take effect:

source /etc/profile
Copy the code

⑧ Check whether the node -v version corresponds to each other.

7. Deploy the project to the server

After installing the Node environment, we need to deploy our project to the server. (Here’s how to pull a project from Git onto a server)

Create a folder under /home with your own user name, for example, Monday, then /home/monday. Clone your project file to /home/monet/project-name.

② Go to your username folder, git clone your project address, and then NPM I.

③ After the NodeJS project is uploaded, configure the database.

Install pM2 and start the NodeJS project

NPM install pm2 -g;

/home/ Monday /project-name /project-name /home/ Monday /project-name /home/ Monday /project-name It depends on how you design your project to start. Mine is NPM Run PRD.

Check whether the nodeJS project is started: Enter pm2 list and press Enter. If the following list appears, it indicates that the nodeJS project has successfully established the service in the background.

At this point, we are ready to start the service!

Three, quick instructions

1. Common Linux commands

  • Rm -rf directory Deletes a directory

  • Cp -r conf.example conf copy conf.example (existing file) to conf (not existing)

  • Curl www.linux.com tests whether a server can reach a web site

  • ①yum install screen -y; ② Screen-s LNMP is used to resolve abnormal connection interruption and reconnect the session

2. Common PM2 commands

  • Pm2 list — View a list of processes

  • Pm2 restart 0: Restarts the service

  • Pm2 stop 0 — Stop the service

  • Pm2 delete 0 — The process is deleted after it is stopped

  • Pm2 info 0 — You can view all information about the current process, as well as some videos

  • Pm2 log 0 — View logs

  • Pm2 Monit – List of processes to monitor